/
Frame.js
82 lines (66 loc) · 1.63 KB
/
Frame.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
/*
---
name: "Frame"
description: "Provides cross-browser interface for requestAnimationFrame"
license:
- "[GNU Lesser General Public License](http://opensource.org/licenses/lgpl-license.php)"
- "[MIT License](http://opensource.org/licenses/mit-license.php)"
requires:
- Core
provides: frame
...
*/
(function () {
var previous,
started = false,
callbacks = [],
remove = [],
frameTime = 16, // 62 fps
// we'll switch to real `requestAnimationFrame` here
// when all browsers will be ready
requestAnimationFrame = function (callback) {
window.setTimeout(callback, frameTime);
};
function startAnimation () {
if (!started) {
previous = Date.now();
requestAnimationFrame(frame);
started = true;
}
}
function invokeFrame () {
var fn, i, l,
now = Date.now(),
// 1 sec is max time for frame to avoid some bugs with too large time
delta = Math.min(now - previous, 1000);
for (i = 0, l = remove.length; i < l; i++) {
coreEraseOne(callbacks, remove[i]);
}
remove.length = 0;
for (i = 0, l = callbacks.length; i < l; i++) {
fn = callbacks[i];
// one of previous calls can remove our fn
if (remove.indexOf(fn) == -1) {
fn.call(null, delta);
}
}
previous = now;
}
function frame() {
requestAnimationFrame(frame);
if (callbacks.length == 0) {
remove.length = 0;
previous = Date.now();
} else invokeFrame();
}
atom.frame = {
add: function (fn) {
startAnimation();
includeUnique(callbacks, fn);
},
// we dont want to fragmentate callbacks, so remove only before frame started
remove: function (fn) {
if (started) includeUnique(remove, fn);
}
};
}());