requestAnimationFrame.polyfill.js 1.0 KB

1234567891011121314151617181920212223242526
  1. // make sure requestAnimationFrame and cancelAnimationFrame are defined
  2. // polyfill for browsers without native support
  3. // by Opera engineer Erik Möller
  4. (function () {
  5. var lastTime = 0;
  6. var vendors = ['webkit', 'moz', 'ms', 'o'];
  7. for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  8. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  9. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||
  10. window[vendors[x] + 'CancelRequestAnimationFrame'];
  11. }
  12. if (!window.requestAnimationFrame) {
  13. window.requestAnimationFrame = function (callback) {
  14. var currTime = new Date().getTime();
  15. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  16. var id = window.setTimeout(function () { return callback(currTime + timeToCall); }, timeToCall);
  17. lastTime = currTime + timeToCall;
  18. return id;
  19. };
  20. }
  21. if (!window.cancelAnimationFrame) {
  22. window.cancelAnimationFrame = function (id) {
  23. clearTimeout(id);
  24. };
  25. }
  26. })();