mirror of
https://github.com/seigler/webgl-threejs-hello
synced 2025-07-27 09:46:13 +00:00
:chick: initial commit
This commit is contained in:
parent
321e7e5612
commit
2eeebe06e8
17 changed files with 8470 additions and 0 deletions
52
app/js/core/Loop.js
Normal file
52
app/js/core/Loop.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
class Loop {
|
||||
|
||||
constructor() {
|
||||
this._idRAF = -1;
|
||||
this._count = 0;
|
||||
|
||||
this._listeners = [];
|
||||
|
||||
this._binds = {};
|
||||
this._binds.update = this._update.bind(this);
|
||||
}
|
||||
|
||||
_update() {
|
||||
let listener = null;
|
||||
let i = this._count;
|
||||
while (--i >= 0) {
|
||||
listener = this._listeners[i];
|
||||
if (listener) {
|
||||
listener.apply(this, null);
|
||||
}
|
||||
}
|
||||
this._idRAF = requestAnimationFrame(this._binds.update);
|
||||
}
|
||||
|
||||
start() {
|
||||
this._update();
|
||||
}
|
||||
|
||||
stop() {
|
||||
cancelAnimationFrame(this._idRAF);
|
||||
}
|
||||
|
||||
add(listener) {
|
||||
const idx = this._listeners.indexOf(listener);
|
||||
if (idx >= 0) {
|
||||
return;
|
||||
}
|
||||
this._listeners.push(listener);
|
||||
this._count++;
|
||||
}
|
||||
|
||||
remove(listener) {
|
||||
const idx = this._listeners.indexOf(listener);
|
||||
if (idx < 0) {
|
||||
return;
|
||||
}
|
||||
this._listeners.splice(idx, 1);
|
||||
this._count--;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new Loop();
|
Loading…
Add table
Add a link
Reference in a new issue