Created
August 16, 2012 16:02
-
-
Save re1ro/3371337 to your computer and use it in GitHub Desktop.
requestAnimationFrame polyfill
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
// requestAnimationFrame polyfill by @rma4ok | |
!function (window) { | |
var | |
equestAnimationFrame = 'equestAnimationFrame', | |
requestAnimationFrame = 'r' + equestAnimationFrame, | |
ancelAnimationFrame = 'ancelAnimationFrame', | |
cancelAnimationFrame = 'c' + ancelAnimationFrame, | |
expectedTime = 0, | |
vendors = ['moz', 'ms', 'o', 'webkit'], | |
vendor; | |
while (!window[requestAnimationFrame] && (vendor = vendors.pop())) { | |
window[requestAnimationFrame] = window[vendor + 'R' + equestAnimationFrame]; | |
window[cancelAnimationFrame] = | |
window[vendor + 'C' + ancelAnimationFrame] || | |
window[vendor + 'CancelR' + equestAnimationFrame]; | |
} | |
if (!window[requestAnimationFrame]) { | |
window[requestAnimationFrame] = function (callback) { | |
var | |
currentTime = +new Date, | |
adjustedDelay = 16 - (currentTime - expectedTime), | |
delay = adjustedDelay > 0 ? adjustedDelay : 0; | |
expectedTime = currentTime + delay; | |
return setTimeout(function () { | |
callback(expectedTime); | |
}, delay); | |
}; | |
window[cancelAnimationFrame] = clearTimeout; | |
} | |
}(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@arindamnayak
Since 'requestAnimationFrame' starts with capital 'R' in vendor-prefixed funtion names like 'webkitRequestAnimationFrame' and it is used multiple times, it's better to use in that way.