summaryrefslogtreecommitdiffstats
path: root/request-animation-frame.js
diff options
context:
space:
mode:
Diffstat (limited to 'request-animation-frame.js')
-rw-r--r--request-animation-frame.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/request-animation-frame.js b/request-animation-frame.js
new file mode 100644
index 0000000..661e15d
--- /dev/null
+++ b/request-animation-frame.js
@@ -0,0 +1,37 @@
+// -------------------------- requestAnimationFrame -------------------------- //
+
+// https://gist.github.com/1866474
+
+var lastTime = 0;
+var prefixes = 'webkit moz ms o'.split(' ');
+// get unprefixed rAF and cAF, if present
+var rAF = window.requestAnimationFrame;
+var cAF = window.cancelAnimationFrame;
+// loop through vendor prefixes and get prefixed rAF and cAF
+var prefix;
+for( var i = 0; i < prefixes.length; i++ ) {
+ if ( rAF && cAF ) {
+ break;
+ }
+ prefix = prefixes[i];
+ rAF = rAF || window[ prefix + 'RequestAnimationFrame' ];
+ cAF = cAF || window[ prefix + 'CancelAnimationFrame' ] ||
+ window[ prefix + 'CancelRequestAnimationFrame' ];
+}
+
+// fallback to setTimeout and clearTimeout if either request/cancel is not supported
+if ( !rAF || !cAF ) {
+ rAF = function( callback ) {
+ 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;
+ };
+
+ cAF = function( id ) {
+ window.clearTimeout( id );
+ };
+} \ No newline at end of file