-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequestAnimationFrame.js
More file actions
99 lines (80 loc) · 2.56 KB
/
requestAnimationFrame.js
File metadata and controls
99 lines (80 loc) · 2.56 KB
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
/*
* requestAnimationFrame.js
* JavaScript implementation of the requestAnimationFrame API.
*
* @author Cedric Vivier (@neonux)
* @license MIT
*/
//window.requestAnimationFrameJS = "force";
(function (window) {
var FRAME_DELAY = 1000/60;
var _interval = null;
var _animationTime = 0;
var _id = 0;
var _requests = {};
function animationTime() {
return _animationTime;
}
function requestAnimationFrame(callback, element) {
if (_interval === null) {
_interval = setInterval(trigger, FRAME_DELAY);
}
_requests[++_id] = {
element: element,
callback: callback //TODO: wrap callback to optimize reuse case by shadowing rAF
}
return _id;
}
function cancelRequestAnimationFrame(id) {
delete _requests[id];
}
function isElementWithinViewport(element) {
//TODO:element.offsetTop window.pageYOffset..
return true;
}
function trigger() {
var id;
var request;
_animationTime = Date.now();
for (id in _requests) {
request = _requests[id];
if (isElementWithinViewport(request.element)) {
delete _requests[id];
request.callback.call(request.element); //TODO: spec!!
}
}
id = -1;
for (id in _requests) {};
if (id === -1) { // no active request
clearInterval(_interval);
_interval = null;
}
}
// setup/normalize shims as necessary
switch (window.requestAnimationFrameJS) {
case "force":
window.animationTime = animationTime;
window.requestAnimationFrame = requestAnimationFrame;
window.cancelRequestAnimationFrame = cancelRequestAnimationFrame;
break;
case "auto":
default:
window.animationTime = window.mozAnimationTime || window.webkitAnimationTime || animationTime;
window.requestAnimationFrame = window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || requestAnimationFrame;
window.cancelRequestAnimationFrame = window.mozCancelRequestAnimationFrame || window.webkitCancelRequestAnimationFrame || cancelRequestAnimationFrame;
break;
}
/* FIXME: this is a bit too aggressve
if (window.requestAnimationFrame === requestAnimationFrame)
// shim set! pause on window blur (user switched tab or minimized window)
window.onblur = function () { clearInterval(_interval); _interval = null; };
window.onfocus = function () { _interval = setInterval(trigger, FRAME_DELAY); };
}*/
// wraps current implementations so that they always bind `this' to element when calling the callback
if (window.requestAnimationFrame !== requestAnimationFrame) {
var impl = window.requestAnimationFrame;
window.requestAnimationFrame = function requestAnimationFrame(callback, element) {
impl(function wrappedCallback() { callback.call(element); }, element);
}
}
})(window);