Skip to content

Instantly share code, notes, and snippets.

@lean8086
Forked from paulirish/rAF.js
Created August 18, 2012 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lean8086/3389553 to your computer and use it in GitHub Desktop.
Save lean8086/3389553 to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill
// requestAnimationFrame polyfill by Erik Möller
// Fixes from Paul Irish and Tino Zijdel
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
(function (w) {
"use strict";
var lastTime = 0,
vendors = ["ms", "moz", "webkit", "o"],
len = vendors.length,
x = 0,
v;
for (x; x < len && !w.requestAnimationFrame; x += 1) {
v = vendors[x];
w.requestAnimationFrame = w[v + "RequestAnimationFrame"];
w.cancelAnimationFrame = w[v + "CancelAnimationFrame"] || w[v + "CancelRequestAnimationFrame"];
}
if (!w.requestAnimationFrame) {
w.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
id = w.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!w.cancelAnimationFrame) {
w.cancelAnimationFrame = function (id) {
w.clearTimeout(id);
};
}
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment