32 lines
862 B
JavaScript
32 lines
862 B
JavaScript
// modules/core/ticker.js — Global Animation Clock
|
|
// Single requestAnimationFrame loop. No module may call RAF directly.
|
|
// All modules subscribe their update(elapsed, delta) function here.
|
|
import * as THREE from 'three';
|
|
|
|
const _clock = new THREE.Clock();
|
|
const _subs = [];
|
|
|
|
/** Register an update function: fn(elapsed, delta) */
|
|
export function subscribe(fn) {
|
|
if (!_subs.includes(fn)) _subs.push(fn);
|
|
}
|
|
|
|
/** Remove a previously registered update function */
|
|
export function unsubscribe(fn) {
|
|
const i = _subs.indexOf(fn);
|
|
if (i !== -1) _subs.splice(i, 1);
|
|
}
|
|
|
|
function _tick() {
|
|
requestAnimationFrame(_tick);
|
|
const delta = _clock.getDelta();
|
|
const elapsed = _clock.getElapsedTime();
|
|
for (const fn of _subs) fn(elapsed, delta);
|
|
}
|
|
|
|
/** Start the single RAF loop. Call once from app.js. */
|
|
export function start() {
|
|
_clock.start();
|
|
_tick();
|
|
}
|