@@ -2,6 +2,7 @@ import { Explosion } from './explosion.js'
22import { randomFloat , randomInt } from './helpers.js'
33import { Mouse } from './mouse.js'
44import { opts } from './options.js'
5+ import { RequestAnimationFrame } from './raf.js'
56import { Resize } from './resize.js'
67import { Sound } from './sound.js'
78import { Trace } from './trace.js'
@@ -14,17 +15,14 @@ export class Fireworks {
1415 private ctx : CanvasRenderingContext2D
1516 private width : number
1617 private height : number
17-
18- private tick = 0
19- private timestamp = performance . now ( )
20- private running = false
21-
2218 private sound : Sound
2319 private resize : Resize
2420 private mouse : Mouse
2521 private traces : Trace [ ] = [ ]
2622 private explosions : Explosion [ ] = [ ]
2723 private waitStopRaf : ( ( ) => void ) | null
24+ private raf : RequestAnimationFrame
25+ private running = false
2826
2927 constructor (
3028 container : Element | HTMLCanvasElement ,
@@ -39,6 +37,7 @@ export class Fireworks {
3937 this . sound = new Sound ( )
4038 this . resize = new Resize ( this )
4139 this . mouse = new Mouse ( this . canvas )
40+ this . raf = new RequestAnimationFrame ( this . render . bind ( this ) )
4241 }
4342
4443 get isRunning ( ) : boolean {
@@ -59,8 +58,7 @@ export class Fireworks {
5958 this . running = true
6059 this . resize . subscribe ( )
6160 this . mouse . subscribe ( )
62- this . clear ( )
63- this . render ( )
61+ this . raf . start ( )
6462 }
6563
6664 stop ( dispose = false ) : void {
@@ -69,6 +67,7 @@ export class Fireworks {
6967 this . running = false
7068 this . resize . unsubscribe ( )
7169 this . mouse . unsubscribe ( )
70+ this . raf . stop ( )
7271 this . clear ( )
7372
7473 if ( dispose ) {
@@ -97,7 +96,9 @@ export class Fireworks {
9796 pause ( ) : void {
9897 this . running = ! this . running
9998 if ( this . running ) {
100- this . render ( )
99+ this . raf . start ( )
100+ } else {
101+ this . raf . stop ( )
101102 }
102103 }
103104
@@ -150,11 +151,9 @@ export class Fireworks {
150151 this . updateSize ( )
151152 }
152153
153- private render ( timestamp = this . timestamp ) : void {
154+ private render ( ) : void {
154155 if ( ! this . ctx || ! this . running ) return
155156
156- requestAnimationFrame ( ( timestamp ) => this . render ( timestamp ) )
157-
158157 this . ctx . globalCompositeOperation = 'destination-out'
159158 this . ctx . fillStyle = `rgba(0, 0, 0, ${ opts . opacity } )`
160159 this . ctx . fillRect ( 0 , 0 , this . width , this . height )
@@ -165,10 +164,6 @@ export class Fireworks {
165164 this . initTrace ( )
166165 this . drawTrace ( )
167166 this . drawExplosion ( )
168-
169- const timeDiff = timestamp - this . timestamp
170- this . timestamp = timestamp
171- this . tick += ( timeDiff * ( opts . intensity * Math . PI ) ) / 1000
172167 }
173168
174169 private initTrace ( ) : void {
@@ -186,7 +181,7 @@ export class Fireworks {
186181 } = opts
187182
188183 if (
189- this . tick > randomInt ( delay . min , delay . max ) ||
184+ this . raf . tick > randomInt ( delay . min , delay . max ) ||
190185 ( this . mouse . active && mouse . max > this . traces . length )
191186 ) {
192187 this . traces . push (
@@ -209,7 +204,7 @@ export class Fireworks {
209204 } )
210205 )
211206
212- this . tick = 0
207+ this . raf . tick = 0
213208 }
214209 }
215210
0 commit comments