diff options
| author | yo mama <pepper@scannerjammer.com> | 2015-02-12 19:33:18 -0800 |
|---|---|---|
| committer | yo mama <pepper@scannerjammer.com> | 2015-02-12 19:33:18 -0800 |
| commit | 57ad852b6e7e337b7a86d2f85ac95b480898b057 (patch) | |
| tree | e5192eb68c0e929ca276f821756b4952727c2787 /js/RequestAnimationFrame.js | |
| parent | 7270ca308cad6dd87744472a8bfe37ab9d342bf0 (diff) | |
working
Diffstat (limited to 'js/RequestAnimationFrame.js')
| -rw-r--r-- | js/RequestAnimationFrame.js | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/js/RequestAnimationFrame.js b/js/RequestAnimationFrame.js index 77f85c5..b1940ae 100644 --- a/js/RequestAnimationFrame.js +++ b/js/RequestAnimationFrame.js @@ -1,22 +1,24 @@ -/** - * Provides requestAnimationFrame in a cross browser way. - * http://paulirish.com/2011/requestanimationframe-for-smart-animating/ - */ +(function() { + var lastTime = 0; + var vendors = ['webkit', 'moz']; + 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 ) { + 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; + }; - window.requestAnimationFrame = ( function() { - - return window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.oRequestAnimationFrame || - window.msRequestAnimationFrame || - function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) { - - window.setTimeout( callback, 1000 / 60 ); - - }; - - } )(); - -} + if (!window.cancelAnimationFrame) + window.cancelAnimationFrame = function(id) { + clearTimeout(id); + }; +}()); |
