Skip to content

Commit a8ca204

Browse files
committed
Implement rAF polyfill - closes gnarf#1
1 parent 0cbb24d commit a8ca204

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

dist/jquery.requestAnimationFrame.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
/*! jQuery requestAnimationFrame - v0.0.0 - 2012-08-11
1+
/*! jQuery requestAnimationFrame - v0.0.0 - 2012-08-31
22
* https://github.com/gnarf37/jquery-requestAnimationFrame
33
* Copyright (c) 2012 Corey Frang; Licensed MIT */
44

55
(function( $ ) {
66

7+
// requestAnimationFrame polyfill adapted from Erik Möller
8+
// fixes from Paul Irish and Tino Zijdel
9+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
10+
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
11+
12+
713
var animating,
8-
requestAnimationFrame = window.requestAnimationFrame ||
9-
window.webkitRequestAnimationFrame ||
10-
window.mozRequestAnimationFrame ||
11-
window.oRequestAnimationFrame ||
12-
window.msRequestAnimationFrame;
14+
lastTime = 0,
15+
vendors = ['ms', 'moz', 'webkit', 'o'],
16+
requestAnimationFrame = window.requestAnimationFrame,
17+
cancelAnimationFrame = window.cancelAnimationFrame;
18+
19+
for(; lastTime < vendors.length && !requestAnimationFrame; lastTime++) {
20+
requestAnimationFrame = window[ vendors[lastTime] + "RequestAnimationFrame" ];
21+
cancelAnimationFrame = cancelAnimationFrame ||
22+
window[ vendors[lastTime] + "CancelAnimationFrame" ] ||
23+
window[ vendors[lastTime] + "CancelRequestAnimationFrame" ];
24+
}
1325

1426
function raf() {
1527
if ( animating ) {
@@ -19,7 +31,9 @@ function raf() {
1931
}
2032

2133
if ( requestAnimationFrame ) {
22-
34+
// use rAF
35+
window.requestAnimationFrame = requestAnimationFrame;
36+
window.cancelAnimationFrame = cancelAnimationFrame;
2337
jQuery.fx.timer = function( timer ) {
2438
if ( timer() && jQuery.timers.push( timer ) && !animating ) {
2539
animating = true;
@@ -30,7 +44,22 @@ if ( requestAnimationFrame ) {
3044
jQuery.fx.stop = function() {
3145
animating = false;
3246
};
47+
} else {
48+
// polyfill
49+
window.requestAnimationFrame = function( callback, element ) {
50+
var currTime = new Date().getTime(),
51+
timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ),
52+
id = window.setTimeout( function() {
53+
callback( currTime + timeToCall );
54+
}, timeToCall );
55+
lastTime = currTime + timeToCall;
56+
return id;
57+
};
3358

59+
window.cancelAnimationFrame = function(id) {
60+
clearTimeout(id);
61+
};
62+
3463
}
3564

3665
}( jQuery ));

dist/jquery.requestAnimationFrame.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.requestAnimationFrame.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99

1010
(function( $ ) {
1111

12+
// requestAnimationFrame polyfill adapted from Erik Möller
13+
// fixes from Paul Irish and Tino Zijdel
14+
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
15+
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
16+
17+
1218
var animating,
13-
requestAnimationFrame = window.requestAnimationFrame ||
14-
window.webkitRequestAnimationFrame ||
15-
window.mozRequestAnimationFrame ||
16-
window.oRequestAnimationFrame ||
17-
window.msRequestAnimationFrame;
19+
lastTime = 0,
20+
vendors = ['ms', 'moz', 'webkit', 'o'],
21+
requestAnimationFrame = window.requestAnimationFrame,
22+
cancelAnimationFrame = window.cancelAnimationFrame;
23+
24+
for(; lastTime < vendors.length && !requestAnimationFrame; lastTime++) {
25+
requestAnimationFrame = window[ vendors[lastTime] + "RequestAnimationFrame" ];
26+
cancelAnimationFrame = cancelAnimationFrame ||
27+
window[ vendors[lastTime] + "CancelAnimationFrame" ] ||
28+
window[ vendors[lastTime] + "CancelRequestAnimationFrame" ];
29+
}
1830

1931
function raf() {
2032
if ( animating ) {
@@ -24,7 +36,9 @@ function raf() {
2436
}
2537

2638
if ( requestAnimationFrame ) {
27-
39+
// use rAF
40+
window.requestAnimationFrame = requestAnimationFrame;
41+
window.cancelAnimationFrame = cancelAnimationFrame;
2842
jQuery.fx.timer = function( timer ) {
2943
if ( timer() && jQuery.timers.push( timer ) && !animating ) {
3044
animating = true;
@@ -35,7 +49,22 @@ if ( requestAnimationFrame ) {
3549
jQuery.fx.stop = function() {
3650
animating = false;
3751
};
52+
} else {
53+
// polyfill
54+
window.requestAnimationFrame = function( callback, element ) {
55+
var currTime = new Date().getTime(),
56+
timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ),
57+
id = window.setTimeout( function() {
58+
callback( currTime + timeToCall );
59+
}, timeToCall );
60+
lastTime = currTime + timeToCall;
61+
return id;
62+
};
3863

64+
window.cancelAnimationFrame = function(id) {
65+
clearTimeout(id);
66+
};
67+
3968
}
4069

4170
}( jQuery ));

0 commit comments

Comments
 (0)