Skip to content

Commit

Permalink
fix: prevent main loop from running more than once (#75)
Browse files Browse the repository at this point in the history
* tmp: add FPS logging

* fix: prevent requestAnimationFrame from running more than once

* Revert "tmp: add FPS logging"

This reverts commit dbc7d40.
  • Loading branch information
fand authored Jul 4, 2024
1 parent 12cbb00 commit 7e92e1a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/vfx-js/src/vfx-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class VFXPlayer {
#canvas: HTMLCanvasElement;
#renderer: THREE.WebGLRenderer;
#camera: THREE.Camera;
#isPlaying = false;
#playRequest: number | undefined = undefined;
#pixelRatio = 2;
#elements: VFXElement[] = [];

Expand Down Expand Up @@ -300,13 +300,21 @@ export class VFXPlayer {
return Promise.resolve();
}

isPlaying(): boolean {
return this.#playRequest !== undefined;
}

play(): void {
this.#isPlaying = true;
this.#playLoop();
if (!this.isPlaying()) {
this.#playRequest = requestAnimationFrame(this.#playLoop);
}
}

stop(): void {
this.#isPlaying = false;
if (this.#playRequest !== undefined) {
cancelAnimationFrame(this.#playRequest);
this.#playRequest = undefined;
}
}

#playLoop = (): void => {
Expand Down Expand Up @@ -398,8 +406,8 @@ export class VFXPlayer {
}
}

if (this.#isPlaying) {
requestAnimationFrame(this.#playLoop);
if (this.isPlaying()) {
this.#playRequest = requestAnimationFrame(this.#playLoop);
}
};

Expand Down

0 comments on commit 7e92e1a

Please sign in to comment.