Created
August 5, 2013 23:15
-
-
Save RoryDuncan/6160519 to your computer and use it in GitHub Desktop.
Paul Irish's requestAnimationFrame shim in coffeescript.
This file contains 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
lastTime = 0 | |
vendors = ['ms', 'moz', 'webkit', 'o'] | |
for x in vendors and not window.requestAnimationFrame | |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'] | |
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] or | |
window[vendors[x]+'CancelRequestAnimationFrame'] | |
if not window.requestAnimationFrame | |
window.requestAnimationFrame = (callback, element) -> | |
currTime = new Date().getTime() | |
timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ) | |
id = window.setTimeout -> | |
callback(currTime + timeToCall) | |
, timeToCall | |
lastTime = currTime + timeToCall | |
return id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment