File tree 1 file changed +14
-6
lines changed
1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ export class VFXPlayer {
32
32
#canvas: HTMLCanvasElement ;
33
33
#renderer: THREE . WebGLRenderer ;
34
34
#camera: THREE . Camera ;
35
- #isPlaying = false ;
35
+ #playRequest: number | undefined = undefined ;
36
36
#pixelRatio = 2 ;
37
37
#elements: VFXElement [ ] = [ ] ;
38
38
@@ -300,13 +300,21 @@ export class VFXPlayer {
300
300
return Promise . resolve ( ) ;
301
301
}
302
302
303
+ isPlaying ( ) : boolean {
304
+ return this . #playRequest !== undefined ;
305
+ }
306
+
303
307
play ( ) : void {
304
- this . #isPlaying = true ;
305
- this . #playLoop( ) ;
308
+ if ( ! this . isPlaying ( ) ) {
309
+ this . #playRequest = requestAnimationFrame ( this . #playLoop) ;
310
+ }
306
311
}
307
312
308
313
stop ( ) : void {
309
- this . #isPlaying = false ;
314
+ if ( this . #playRequest !== undefined ) {
315
+ cancelAnimationFrame ( this . #playRequest) ;
316
+ this . #playRequest = undefined ;
317
+ }
310
318
}
311
319
312
320
#playLoop = ( ) : void => {
@@ -398,8 +406,8 @@ export class VFXPlayer {
398
406
}
399
407
}
400
408
401
- if ( this . # isPlaying) {
402
- requestAnimationFrame ( this . #playLoop) ;
409
+ if ( this . isPlaying ( ) ) {
410
+ this . #playRequest = requestAnimationFrame ( this . #playLoop) ;
403
411
}
404
412
} ;
405
413
You can’t perform that action at this time.
0 commit comments