rAF.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  2. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  3. // requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
  4. // MIT license
  5. (function () {
  6. var lastTime = 0;
  7. var vendors = ['ms', 'moz', 'webkit', 'o'];
  8. for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  9. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  10. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
  11. || window[vendors[x] + 'CancelRequestAnimationFrame'];
  12. }
  13. if (!window.requestAnimationFrame)
  14. window.requestAnimationFrame = function (callback, element) {
  15. var currTime = new Date().getTime();
  16. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  17. var id = window.setTimeout(function () { callback(currTime + timeToCall); },
  18. timeToCall);
  19. lastTime = currTime + timeToCall;
  20. return id;
  21. };
  22. if (!window.cancelAnimationFrame)
  23. window.cancelAnimationFrame = function (id) {
  24. clearTimeout(id);
  25. };
  26. }());