Repository navigation
Expand file tree
/
Copy pathFrameloop.js
More file actions
131 lines (115 loc) · 3.05 KB
/
Copy pathFrameloop.js
File metadata and controls
131 lines (115 loc) · 3.05 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
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
/**
* Run game loops within a frame-based system.
*
* @author Vicente G. (@SharkPool-SP)
*
* @version 2026.1.0.0
*/
const REFRESH_RATE = 1000 / 60;
const _requestAnimationFrame =
typeof requestAnimationFrame === "function"
? requestAnimationFrame
: (f) => setTimeout(f, REFRESH_RATE);
const _cancelAnimationFrame =
typeof requestAnimationFrame === "function"
? cancelAnimationFrame
: clearTimeout;
class FrameLoop {
/**
* Constructs a FrameLoop object.
*
* @param {Number} framerate (optional) determines what framerate to run this loop at
*/
constructor(framerate = 60) {
this.running = false;
this.listeners = [];
this.framerate = null;
this.isAnimationLoop = null;
this._frameListener = null;
this.callback = this.callback.bind(this);
this.setFramerate(framerate);
}
/**
* Callback function used by the loop.
*
* @private
*/
callback() {
if (this.listeners.length) {
for (const func of this.listeners) func();
}
if (this.isAnimationLoop && this.running) {
this._frameListener = _requestAnimationFrame(this.callback);
}
}
/**
* Starts the frame loop.
*/
start() {
if (this.running) return;
this.running = true;
if (this.isAnimationLoop) {
this._frameListener = _requestAnimationFrame(this.callback);
} else {
this._frameListener = setInterval(this.callback, this.framerate);
}
}
/**
* Stops the frame loop.
*/
stop() {
if (!this.running) return;
this.running = false;
if (this.isAnimationLoop) {
_cancelAnimationFrame(this._frameListener);
} else {
clearInterval(this._frameListener);
}
}
/**
* Sets the framerate of this frame loop
*
* @param {Number} fps The new framerate. Use '0' to for the user screen-refresh rate
*/
setFramerate(fps) {
const normalized = fps ? 1000 / Math.max(0, Math.round(fps)) : 0;
if (normalized === this.framerate) return;
this.framerate = normalized;
if (this.running) {
if (this.isAnimationLoop) {
// we know the new framerate cannot be less than or equal to 0
_cancelAnimationFrame(this._frameListener);
this._frameListener = setInterval(this.callback, normalized);
} else {
clearInterval(this._frameListener);
this._frameListener =
normalized > 0
? setInterval(this.callback, normalized)
: _requestAnimationFrame(this.callback);
}
}
this.isAnimationLoop = this.framerate === 0;
}
/**
* Add a custom listener to the frame loop.
*
* @param {Function} func function that is called on every frame
*/
onFrame(func) {
if (typeof func === "function") {
this.listeners.push(func);
}
}
/**
* Removes a custom listener from the frame loop.
*
* @param {Function} func function to remove from the loop
*/
offFrame(func) {
if (typeof func === "function") {
const index = this.listeners.indexOf(func);
if (index > -1) this.listeners.splice(index, 1);
}
}
}
export { FrameLoop };