mirror of
https://github.com/seigler/webgl-threejs-hello
synced 2025-07-27 01:36:14 +00:00
54 lines
994 B
JavaScript
54 lines
994 B
JavaScript
class Loop {
|
|
|
|
constructor() {
|
|
this._idRAF = -1;
|
|
this._count = 0;
|
|
this._time = (new Date()).getTime();
|
|
|
|
this._listeners = [];
|
|
|
|
this._binds = {};
|
|
this._binds.update = this._update.bind(this);
|
|
}
|
|
|
|
_update() {
|
|
let listener = null;
|
|
let i = this._count;
|
|
this._time = (new Date()).getTime()
|
|
while (--i >= 0) {
|
|
listener = this._listeners[i];
|
|
if (listener) {
|
|
listener.apply(this, [this._time]);
|
|
}
|
|
}
|
|
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();
|