simplified autoplayer.

This commit is contained in:
David Allemang
2026-07-06 12:21:41 -04:00
parent 71e29b92b2
commit e57b1d9b24

47
autoplayer.js Normal file
View File

@@ -0,0 +1,47 @@
// autoplayer.js
export class AutoPlayer {
constructor(diorama, speed = 1.0) {
this.frame = diorama.frame;
this.speed = speed;
this.lastFrameTime = performance.now();
this.playheadTime = 0;
this.isRunning = true;
this._loop = this.loop.bind(this);
requestAnimationFrame(this._loop);
}
getMaxTime() {
const evs = this.frame.world.events;
return evs.length > 0 ? evs[evs.length - 1].time : 0;
}
loop(time) {
if (!this.isRunning) return;
const dt = (time - this.lastFrameTime) / 1000;
this.lastFrameTime = time;
const maxTime = this.getMaxTime();
// Only animate if there are actually forward events in the timeline
if (maxTime > 0) {
this.playheadTime += dt * 20 * this.speed;
// Loop wrap-around
if (this.playheadTime >= maxTime) {
this.playheadTime = this.playheadTime % maxTime;
}
this.frame.seek(this.playheadTime);
}
requestAnimationFrame(this._loop);
}
// Call this if you ever need to destroy the diorama and stop the memory leak
stop() {
this.isRunning = false;
}
}