-
-
Save ethertank/1836249 to your computer and use it in GitHub Desktop.
requestAnimationFrame / cancelAnimationFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
(function() { | |
var lastTime = 0; | |
var vendors = ['ms', 'moz', 'webkit', 'o']; | |
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { | |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; | |
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] | |
|| window[vendors[x]+'CancelRequestAnimationFrame']; | |
} | |
if (!window.requestAnimationFrame) | |
window.requestAnimationFrame = function(callback, element) { | |
var currTime = new Date().getTime(); | |
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, | |
timeToCall); | |
lastTime = currTime + timeToCall; | |
return id; | |
}; | |
if (!window.cancelAnimationFrame) | |
window.cancelAnimationFrame = function(id) { | |
clearTimeout(id); | |
}; | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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<3&&!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"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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...Good bye Opera... | |
(function(w, a, b, af, x, lastTime, v) { | |
var c = "c" + a, | |
C = "C" + a, | |
r = "r" + b, | |
R = "R" + b; | |
for (; x < 3 && !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"]); | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment