cancelAnimationFrame.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import defined from "./defined.js";
  2. let implementation;
  3. if (typeof cancelAnimationFrame !== "undefined") {
  4. implementation = cancelAnimationFrame;
  5. }
  6. (function () {
  7. // look for vendor prefixed function
  8. if (!defined(implementation) && typeof window !== "undefined") {
  9. const vendors = ["webkit", "moz", "ms", "o"];
  10. let i = 0;
  11. const len = vendors.length;
  12. while (i < len && !defined(implementation)) {
  13. implementation = window[`${vendors[i]}CancelAnimationFrame`];
  14. if (!defined(implementation)) {
  15. implementation = window[`${vendors[i]}CancelRequestAnimationFrame`];
  16. }
  17. ++i;
  18. }
  19. }
  20. // otherwise, assume requestAnimationFrame is based on setTimeout, so use clearTimeout
  21. if (!defined(implementation)) {
  22. implementation = clearTimeout;
  23. }
  24. })();
  25. /**
  26. * A browser-independent function to cancel an animation frame requested using {@link requestAnimationFrame}.
  27. *
  28. * @function cancelAnimationFrame
  29. *
  30. * @param {Number} requestID The value returned by {@link requestAnimationFrame}.
  31. *
  32. * @see {@link http://www.w3.org/TR/animation-timing/#the-WindowAnimationTiming-interface|The WindowAnimationTiming interface}
  33. */
  34. function cancelAnimationFramePolyfill(requestID) {
  35. // we need this extra wrapper function because the native cancelAnimationFrame
  36. // functions must be invoked on the global scope (window), which is not the case
  37. // if invoked as Cesium.cancelAnimationFrame(requestID)
  38. implementation(requestID);
  39. }
  40. export default cancelAnimationFramePolyfill;