forked from gnarf/jquery-requestAnimationFrame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.requestAnimationFrame.js
65 lines (54 loc) · 1.79 KB
/
jquery.requestAnimationFrame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*! jQuery requestAnimationFrame - v0.1.1pre - 2013-01-16
* https://github.com/gnarf37/jquery-requestAnimationFrame
* Copyright (c) 2013 Corey Frang; Licensed MIT */
(function( $ ) {
// requestAnimationFrame polyfill adapted from Erik Möller
// fixes from Paul Irish and Tino Zijdel
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
var animating,
lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o'],
requestAnimationFrame = window.requestAnimationFrame,
cancelAnimationFrame = window.cancelAnimationFrame;
for(; lastTime < vendors.length && !requestAnimationFrame; lastTime++) {
requestAnimationFrame = window[ vendors[lastTime] + "RequestAnimationFrame" ];
cancelAnimationFrame = cancelAnimationFrame ||
window[ vendors[lastTime] + "CancelAnimationFrame" ] ||
window[ vendors[lastTime] + "CancelRequestAnimationFrame" ];
}
function raf() {
if ( animating ) {
requestAnimationFrame( raf );
jQuery.fx.tick();
}
}
if ( requestAnimationFrame ) {
// use rAF
window.requestAnimationFrame = requestAnimationFrame;
window.cancelAnimationFrame = cancelAnimationFrame;
jQuery.fx.timer = function( timer ) {
if ( timer() && jQuery.timers.push( timer ) && !animating ) {
animating = true;
raf();
}
};
jQuery.fx.stop = function() {
animating = false;
};
} else {
// polyfill
window.requestAnimationFrame = function( callback, element ) {
var currTime = new Date().getTime(),
timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ),
id = window.setTimeout( function() {
callback( currTime + timeToCall );
}, timeToCall );
lastTime = currTime + timeToCall;
return id;
};
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}( jQuery ));