-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodules-RenderLoop.js
242 lines (208 loc) · 6.27 KB
/
modules-RenderLoop.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function() {
/**
* window.force.modules.RenderLoop()
*/
/**
* TODO:
* optionally the possibility to set multiple draw methods
* investigate to see if a differenciation between draw and logic loops would be an improvement*
* (draw is called when possible, logic would be called everytime)
* add a method to enable/disable the native requestFrameAnimation override
*/
var mod = function (options) {
options = options || {};
var that = this;
var framerate = 60;
var drawMethod;
var then, interval;
var useSimpleLoop = false;
var drawTimer;
var stopRequest = false;
var requestAnimationFrameCbList = null;
var requestAnimationFrameSupported = false;
var limitMaximumSpeed = false;
var TWEEN;
this.SMALLER_FRAME_INTERVAL = 1000 / 60;
// -------------------------
// save original native requestAnimationFrame (will be overrided after)
// -------------------------
var nativeCode = {
requestAnimationFrame : window.requestAnimationFrame,
webkitRequestAnimationFrame : window.webkitRequestAnimationFrame,
mozRequestAnimationFrame : window.mozRequestAnimationFrame,
oRequestAnimationFrame : window.oRequestAnimationFrame
};
requestAnimationFrameSupported = (
nativeCode.requestAnimationFrame ||
nativeCode.webkitRequestAnimationFrame ||
nativeCode.mozRequestAnimationFrame ||
nativeCode.oRequestAnimationFrame
) ? true : false;
// requestAnimFrame is set to the right function (the following function is executed right now and never again)
var requestAnimFrame = (function() {
return nativeCode.requestAnimationFrame ||
nativeCode.webkitRequestAnimationFrame ||
nativeCode.mozRequestAnimationFrame ||
nativeCode.oRequestAnimationFrame ||
function (cb){
window.setTimeout(cb, interval);
};
})();
// -------------------------
// native requestAnimationFrame override
// -------------------------
window.requestAnimationFrame = function(cb) {
if(!requestAnimationFrameCbList) {
requestAnimationFrameCbList = [];
}
requestAnimationFrameCbList.push(cb);
};
var rafVariants = ['webkitRequestAnimationFrame', 'mozRequestAnimationFrame', 'oRequestAnimationFrame'];
for (var i = 0; i < rafVariants.length; i++) {
if (window[rafVariants[i]]) {
window[rafVariants[i]] = window.requestAnimationFrame;
}
}
function execRequestAnimationFrameFnList() {
// native requestAnimationFrame override cb list
if (requestAnimationFrameCbList) {
var l = requestAnimationFrameCbList.length;
for (var i = 0; i < l; i++) {
var f = requestAnimationFrameCbList.shift();
if (f) {
f();
}
}
if (requestAnimationFrameCbList.length === 0) {
requestAnimationFrameCbList = null;
}
}
};
// -------------------------
// standard exposed methods
// -------------------------
this.setFramerate = function (fps) {
framerate = Math.min(60, Math.max(1, Math.round(fps)));
interval = 1000 / framerate;
};
this.getFramerate = function() {
return Math.round(1000 / interval);
};
this.setDrawMethod = function (fn) {
if (typeof fn == 'function') {
drawMethod = fn;
}
};
this.forceSimpleLoop = function (bool) {
if (bool === true || bool === false) {
useSimpleLoop = bool;
}
};
this.limitMaximumFps = function (bool) {
if (bool === true || bool === false) {
limitMaximumSpeed = bool;
}
};
this.start = function () {
if (window.TWEEN) {
TWEEN = window.TWEEN;
}
if (!then) { // if it's not already running
then = Date.now();
this.lastFrameInterval = this.SMALLER_FRAME_INTERVAL;
if (drawMethod) {
// setInterval(function() { console.log((1000 / that._fpsTool.getAverageDelta()) + ' fps'); }, 3000);
if (useSimpleLoop) {
drawTimer = setInterval(function() {
coreDraw();
}, Math.floor(interval));
} else {
draw();
}
}
}
};
this.stop = function (fn) {
if (drawTimer) {
clearInterval(drawTimer);
} else if (then) {
stopRequest = true;
}
then = null;
};
this.forceDrawNow = function() {
then = Date.now();
coreDraw();
};
// -------------------------
// fps debug tool
// -------------------------
/*this._fpsTool = {};
this._fpsTool.stats = [];
this._fpsTool.setLastDelta = function(d) {
that._fpsTool.stats.push(d);
that._fpsTool.stats.splice(0, that._fpsTool.stats.length - 60 * 3);
}
this._fpsTool.getAverageDelta = function() {
var t = 0;
var f = 0;
var max = 60 * 3;
for (var i = 0, l = that._fpsTool.stats.length; i < l; i++) {
t += that._fpsTool.stats[l - i - 1];
f++;
if (t >= max) break;
}
t = t / f;
return t;
}*/
// -------------------------
// render loop
// -------------------------
function draw() {
if (!stopRequest) {
var now, delta;
if (!requestAnimationFrameSupported || limitMaximumSpeed) {
now = Date.now();
delta = now - then;
that.lastFrameInterval = delta;
that.now = now;
}
if ((!requestAnimationFrameSupported || limitMaximumSpeed) && delta < interval) {
setTimeout(draw, 1);
} else {
if (!requestAnimationFrameSupported || limitMaximumSpeed) {
//that._fpsTool.setLastDelta(delta);
then = now;
requestAnimFrame(draw);
}
coreDraw();
if (requestAnimationFrameSupported && !limitMaximumSpeed) {
now = Date.now();
delta = now - then;
that.lastFrameInterval = delta;
that.now = now;
//that._fpsTool.setLastDelta(delta);
then = now;
requestAnimFrame(draw);
}
}
} else {
stopRequest = false;
}
}
// -------------------------
// core draw, executed either
// by simple loop or framerequest
// -------------------------
function coreDraw() {
drawMethod();
execRequestAnimationFrameFnList();
// if the tween component exists let's update it
if (TWEEN) {
TWEEN.update();
}
};
};
// expose module
window.force.expose('window.force.modules.RenderLoop', mod);
})();