Skip to content

Instantly share code, notes, and snippets.

@fojas
Forked from ethertank/rAF.js
Created May 23, 2012 14:37
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 fojas/2775570 to your computer and use it in GitHub Desktop.
Save fojas/2775570 to your computer and use it in GitHub Desktop.
requestAnimationFrame / cancelAnimationFrame
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
// little saving...
(function(w, a, b, af, x, lastTime, v) {
var c = "c" + a,
C = "C" + a,
r = "r" + b,
R = "R" + b;
for (; x < 4 && !w[r + af]; ++x) {
w[r + af] = w[v[x] + R + af];
w[c + af] = w[v[x] + C + af] || w[v[x] + C + R + af];
}
if (!w[r + af]) {
w[r + af] = function(callback, element) {
var currTime = +new Date(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
id = w.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!w[c + af]) {
w[c + af] = function(id) {
clearTimeout(id);
};
}
})(window, "ancel", "equest", "AnimationFrame", 0, 0, ["ms", "moz", "webkit", "o"]);
// test code
/*
var i = 0,
db = document.body;
function render() { db.innerHTML = i++; }
function animationLoop(){
render();
requestAnimationFrame(animationLoop);
}
animationLoop();
*/​
// Compressed (YUI : Preserve unnecessary semicolons) = 393B
// Erik Möller's requestAnimationFrame polyfill
// Fix : Paul Irish and Tino Zijdel
// Minify : ethertank.jp with YUI Compressor
(function(m,j,i,l,k,f,n){var h="c"+j,e="C"+j,d="r"+i,g="R"+i;for(;k<4&&!m[d+l];++k){m[d+l]=m[n[k]+g+l];m[h+l]=m[n[k]+e+l]||m[n[k]+e+g+l];}if(!m[d+l]){m[d+l]=function(p,b){var a=+new Date(),c=Math.max(0,16-(a-f)),o=m.setTimeout(function(){p(a+c);},c);f=a+c;return o;};}if(!m[h+l]){m[h+l]=function(a){clearTimeout(a);};}})(window,"ancel","equest","AnimationFrame",0,0,["ms","moz","webkit","o"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment