11import { Explosion } from './explosion.js'
22import { floor , randomFloat , randomInt } from './helpers.js'
33import { Mouse } from './mouse.js'
4- import { opts } from './options.js'
4+ import { Options } from './options.js'
55import { RequestAnimationFrame } from './raf.js'
66import { Resize } from './resize.js'
77import { Sound } from './sound.js'
88import { Trace } from './trace.js'
9- import { FireworksOptions , IBoundaries , Sizes } from './types.js'
9+ import type { FireworksOptions , IBoundaries , Sizes } from './types.js'
1010
1111export class Fireworks {
1212 private target : Element | HTMLCanvasElement
@@ -15,29 +15,33 @@ export class Fireworks {
1515 private ctx : CanvasRenderingContext2D
1616 private width : number
1717 private height : number
18- private sound : Sound
19- private resize : Resize
20- private mouse : Mouse
2118 private traces : Trace [ ] = [ ]
2219 private explosions : Explosion [ ] = [ ]
2320 private waitStopRaf : ( ( ) => void ) | null
24- private raf : RequestAnimationFrame
2521 private running = false
2622
23+ private readonly opts : Options
24+ private readonly sound : Sound
25+ private readonly resize : Resize
26+ private readonly mouse : Mouse
27+ private readonly raf : RequestAnimationFrame
28+
2729 constructor (
2830 container : Element | HTMLCanvasElement ,
2931 options : FireworksOptions = { }
3032 ) {
3133 this . target = container
3234 this . container = container
3335
34- this . createCanvas ( this . target )
36+ this . opts = new Options ( )
37+
3538 this . updateOptions ( options )
39+ this . createCanvas ( this . target )
3640
37- this . sound = new Sound ( )
38- this . resize = new Resize ( this )
39- this . mouse = new Mouse ( this . canvas )
40- this . raf = new RequestAnimationFrame ( this . render . bind ( this ) )
41+ this . sound = new Sound ( this . opts )
42+ this . resize = new Resize ( this . opts , this . updateSize . bind ( this ) )
43+ this . mouse = new Mouse ( this . opts , this . canvas )
44+ this . raf = new RequestAnimationFrame ( this . opts , this . render . bind ( this ) )
4145 }
4246
4347 get isRunning ( ) : boolean {
@@ -48,6 +52,10 @@ export class Fireworks {
4852 return __VERSION__
4953 }
5054
55+ get currentOptions ( ) : Options {
56+ return this . opts
57+ }
58+
5159 start ( ) : void {
5260 if ( this . running ) return
5361
@@ -56,18 +64,18 @@ export class Fireworks {
5664 }
5765
5866 this . running = true
59- this . resize . subscribe ( )
60- this . mouse . subscribe ( )
61- this . raf . start ( )
67+ this . resize . mount ( )
68+ this . mouse . mount ( )
69+ this . raf . mount ( )
6270 }
6371
6472 stop ( dispose = false ) : void {
6573 if ( ! this . running ) return
6674
6775 this . running = false
68- this . resize . unsubscribe ( )
69- this . mouse . unsubscribe ( )
70- this . raf . stop ( )
76+ this . resize . unmount ( )
77+ this . mouse . unmount ( )
78+ this . raf . unmount ( )
7179 this . clear ( )
7280
7381 if ( dispose ) {
@@ -96,9 +104,9 @@ export class Fireworks {
96104 pause ( ) : void {
97105 this . running = ! this . running
98106 if ( this . running ) {
99- this . raf . start ( )
107+ this . raf . mount ( )
100108 } else {
101- this . raf . stop ( )
109+ this . raf . unmount ( )
102110 }
103111 }
104112
@@ -122,7 +130,7 @@ export class Fireworks {
122130 }
123131
124132 updateOptions ( options : FireworksOptions ) : void {
125- opts . updateOptions ( options )
133+ this . opts . update ( options )
126134 }
127135
128136 updateSize ( {
@@ -136,7 +144,7 @@ export class Fireworks {
136144 this . canvas . height = height
137145
138146 this . updateBoundaries ( {
139- ...opts . boundaries ,
147+ ...this . opts . boundaries ,
140148 width,
141149 height
142150 } )
@@ -165,7 +173,7 @@ export class Fireworks {
165173 private render ( ) : void {
166174 if ( ! this . ctx || ! this . running ) return
167175
168- const { opacity, lineStyle, lineWidth } = opts
176+ const { opacity, lineStyle, lineWidth } = this . opts
169177 this . ctx . globalCompositeOperation = 'destination-out'
170178 this . ctx . fillStyle = `rgba(0, 0, 0, ${ opacity } )`
171179 this . ctx . fillRect ( 0 , 0 , this . width , this . height )
@@ -188,7 +196,7 @@ export class Fireworks {
188196 traceSpeed,
189197 acceleration,
190198 mouse
191- } = opts
199+ } = this . opts
192200
193201 this . traces . push (
194202 new Trace ( {
@@ -214,9 +222,10 @@ export class Fireworks {
214222 private initTrace ( ) : void {
215223 if ( this . waitStopRaf ) return
216224
225+ const { delay, mouse } = this . opts
217226 if (
218- this . raf . tick > randomInt ( opts . delay . min , opts . delay . max ) ||
219- ( this . mouse . active && opts . mouse . max > this . traces . length )
227+ this . raf . tick > randomInt ( delay . min , delay . max ) ||
228+ ( this . mouse . active && mouse . max > this . traces . length )
220229 ) {
221230 this . createTrace ( )
222231 this . raf . tick = 0
@@ -245,7 +254,7 @@ export class Fireworks {
245254 friction,
246255 gravity,
247256 decay
248- } = opts
257+ } = this . opts
249258
250259 let particlesLength = floor ( particles )
251260 while ( particlesLength -- ) {
0 commit comments