Skip to content

Commit 9c37638

Browse files
author
Amir Tocker
committed
Add requestAnimationFrame.js
1 parent a7a05ac commit 9c37638

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Util/requestAnimationFrame.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
2+
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
3+
// https://gist.github.com/paulirish/1579671
4+
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
5+
6+
// MIT license
7+
8+
(function() {
9+
var lastTime = 0;
10+
var vendors = ['ms', 'moz', 'webkit', 'o'];
11+
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
12+
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
13+
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
14+
|| window[vendors[x]+'CancelRequestAnimationFrame'];
15+
}
16+
17+
if (!window.requestAnimationFrame)
18+
window.requestAnimationFrame = function(callback, element) {
19+
var currTime = new Date().getTime();
20+
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
21+
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
22+
timeToCall);
23+
lastTime = currTime + timeToCall;
24+
return id;
25+
};
26+
27+
if (!window.cancelAnimationFrame)
28+
window.cancelAnimationFrame = function(id) {
29+
clearTimeout(id);
30+
};
31+
}());

0 commit comments

Comments
 (0)