-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraf-polyfill.js
113 lines (98 loc) · 2.71 KB
/
raf-polyfill.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//requestAnimationFrame polyfill | Milos Djakonovic ( @Miloshio ) | MIT | https://github.com/milosdjakonovic/requestAnimationFrame-polyfill
(function(w){
/**
*
* How many times should polyfill call
* update callback? By canon, it should
* be 60 times per second, so that ideal
* framerate 60fps could be reached.
*
* However, even native implementations
* of requestAnimationFrame often cannot
* do 60fps, but, unlike any polyfill,
* they can synchronise achievable fps
* rate with screen refresh rate.
*
* So, leave this value 1000/60 unless
* you target specific browser on spec
* ific device that is going to work
* better with custom value. I think
* that this is the longest comment I've
* written on single variable so far.
**/
var FRAME_RATE_INTERVAL = 1000/60,
/**
* All queued callbacks in given cycle.
**/
allCallbacks = [],
executeAllScheduled = false,
shouldCheckCancelRaf = false,
/**
* Callbacks queued for cancellation.
**/
callbacksForCancellation = [],
/**
* Should callback be cancelled?
* @param cb - callback
**/
isToBeCancelled = function(cb){
for(var i=0;i<callbacksForCancellation.length;i++){
if(callbacksForCancellation[i] === cb ){
callbacksForCancellation.splice(i,1);
return true;
}
}
},
/**
*
* Executes all (surprise) callbacks in
* and removes them from callback queue.
*
**/
executeAll = function(){
executeAllScheduled = false;
var _allCallbacks = allCallbacks;
allCallbacks = [];
for(var i=0;i<_allCallbacks.length;i++){
if(shouldCheckCancelRaf===true){
if (isToBeCancelled(_allCallbacks[i])){
shouldCheckCancelRaf = false;
return;
}
}
_allCallbacks[i].apply(w, [ new Date().getTime() ] );
}
},
/**
*
* requestAnimationFrame polyfill
* @param callback - callback to be queued & executed | executed
* @return callback
*
**/
raf = function(callback){
allCallbacks.push(callback);
if(executeAllScheduled===false){
w.setTimeout(executeAll, FRAME_RATE_INTERVAL);
executeAllScheduled = true;
}
return callback;
},
/**
*
* Cancels raf.
**/
cancelRaf = function(callback){
callbacksForCancellation.push(callback);
shouldCheckCancelRaf = true;
},
//https://gist.github.com/paulirish/1579671
vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !w.requestAnimationFrame; ++x) {
w.requestAnimationFrame = w[vendors[x]+'RequestAnimationFrame'];
w.cancelAnimationFrame = w[vendors[x]+'CancelAnimationFrame']
|| w[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!w.requestAnimationFrame) w.requestAnimationFrame = raf;
if (!w.cancelAnimationFrame) w.cancelAnimationFrame = cancelRaf;
})(window);