Skip to content

Commit 7e92e1a

Browse files
authored
fix: prevent main loop from running more than once (#75)
* tmp: add FPS logging * fix: prevent requestAnimationFrame from running more than once * Revert "tmp: add FPS logging" This reverts commit dbc7d40.
1 parent 12cbb00 commit 7e92e1a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/vfx-js/src/vfx-player.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class VFXPlayer {
3232
#canvas: HTMLCanvasElement;
3333
#renderer: THREE.WebGLRenderer;
3434
#camera: THREE.Camera;
35-
#isPlaying = false;
35+
#playRequest: number | undefined = undefined;
3636
#pixelRatio = 2;
3737
#elements: VFXElement[] = [];
3838

@@ -300,13 +300,21 @@ export class VFXPlayer {
300300
return Promise.resolve();
301301
}
302302

303+
isPlaying(): boolean {
304+
return this.#playRequest !== undefined;
305+
}
306+
303307
play(): void {
304-
this.#isPlaying = true;
305-
this.#playLoop();
308+
if (!this.isPlaying()) {
309+
this.#playRequest = requestAnimationFrame(this.#playLoop);
310+
}
306311
}
307312

308313
stop(): void {
309-
this.#isPlaying = false;
314+
if (this.#playRequest !== undefined) {
315+
cancelAnimationFrame(this.#playRequest);
316+
this.#playRequest = undefined;
317+
}
310318
}
311319

312320
#playLoop = (): void => {
@@ -398,8 +406,8 @@ export class VFXPlayer {
398406
}
399407
}
400408

401-
if (this.#isPlaying) {
402-
requestAnimationFrame(this.#playLoop);
409+
if (this.isPlaying()) {
410+
this.#playRequest = requestAnimationFrame(this.#playLoop);
403411
}
404412
};
405413

0 commit comments

Comments
 (0)