raf.js 739 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * requestAnimationFrame polyfill
  3. */
  4. import { isServer } from '..';
  5. var prev = Date.now();
  6. /* istanbul ignore next */
  7. function fallback(fn) {
  8. var curr = Date.now();
  9. var ms = Math.max(0, 16 - (curr - prev));
  10. var id = setTimeout(fn, ms);
  11. prev = curr + ms;
  12. return id;
  13. }
  14. /* istanbul ignore next */
  15. var root = isServer ? global : window;
  16. /* istanbul ignore next */
  17. var iRaf = root.requestAnimationFrame || fallback;
  18. /* istanbul ignore next */
  19. var iCancel = root.cancelAnimationFrame || root.clearTimeout;
  20. export function raf(fn) {
  21. return iRaf.call(root, fn);
  22. } // double raf for animation
  23. export function doubleRaf(fn) {
  24. raf(function () {
  25. raf(fn);
  26. });
  27. }
  28. export function cancelRaf(id) {
  29. iCancel.call(root, id);
  30. }