1
1
import { Explosion } from './explosion.js'
2
2
import { floor , randomFloat , randomInt } from './helpers.js'
3
3
import { Mouse } from './mouse.js'
4
- import { opts } from './options.js'
4
+ import { Options } from './options.js'
5
5
import { RequestAnimationFrame } from './raf.js'
6
6
import { Resize } from './resize.js'
7
7
import { Sound } from './sound.js'
8
8
import { Trace } from './trace.js'
9
- import { FireworksOptions , IBoundaries , Sizes } from './types.js'
9
+ import type { FireworksOptions , IBoundaries , Sizes } from './types.js'
10
10
11
11
export class Fireworks {
12
12
private target : Element | HTMLCanvasElement
@@ -15,29 +15,33 @@ export class Fireworks {
15
15
private ctx : CanvasRenderingContext2D
16
16
private width : number
17
17
private height : number
18
- private sound : Sound
19
- private resize : Resize
20
- private mouse : Mouse
21
18
private traces : Trace [ ] = [ ]
22
19
private explosions : Explosion [ ] = [ ]
23
20
private waitStopRaf : ( ( ) => void ) | null
24
- private raf : RequestAnimationFrame
25
21
private running = false
26
22
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
+
27
29
constructor (
28
30
container : Element | HTMLCanvasElement ,
29
31
options : FireworksOptions = { }
30
32
) {
31
33
this . target = container
32
34
this . container = container
33
35
34
- this . createCanvas ( this . target )
36
+ this . opts = new Options ( )
37
+
35
38
this . updateOptions ( options )
39
+ this . createCanvas ( this . target )
36
40
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 ) )
41
45
}
42
46
43
47
get isRunning ( ) : boolean {
@@ -48,6 +52,10 @@ export class Fireworks {
48
52
return __VERSION__
49
53
}
50
54
55
+ get currentOptions ( ) : Options {
56
+ return this . opts
57
+ }
58
+
51
59
start ( ) : void {
52
60
if ( this . running ) return
53
61
@@ -56,18 +64,18 @@ export class Fireworks {
56
64
}
57
65
58
66
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 ( )
62
70
}
63
71
64
72
stop ( dispose = false ) : void {
65
73
if ( ! this . running ) return
66
74
67
75
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 ( )
71
79
this . clear ( )
72
80
73
81
if ( dispose ) {
@@ -96,9 +104,9 @@ export class Fireworks {
96
104
pause ( ) : void {
97
105
this . running = ! this . running
98
106
if ( this . running ) {
99
- this . raf . start ( )
107
+ this . raf . mount ( )
100
108
} else {
101
- this . raf . stop ( )
109
+ this . raf . unmount ( )
102
110
}
103
111
}
104
112
@@ -122,7 +130,7 @@ export class Fireworks {
122
130
}
123
131
124
132
updateOptions ( options : FireworksOptions ) : void {
125
- opts . updateOptions ( options )
133
+ this . opts . update ( options )
126
134
}
127
135
128
136
updateSize ( {
@@ -136,7 +144,7 @@ export class Fireworks {
136
144
this . canvas . height = height
137
145
138
146
this . updateBoundaries ( {
139
- ...opts . boundaries ,
147
+ ...this . opts . boundaries ,
140
148
width,
141
149
height
142
150
} )
@@ -165,7 +173,7 @@ export class Fireworks {
165
173
private render ( ) : void {
166
174
if ( ! this . ctx || ! this . running ) return
167
175
168
- const { opacity, lineStyle, lineWidth } = opts
176
+ const { opacity, lineStyle, lineWidth } = this . opts
169
177
this . ctx . globalCompositeOperation = 'destination-out'
170
178
this . ctx . fillStyle = `rgba(0, 0, 0, ${ opacity } )`
171
179
this . ctx . fillRect ( 0 , 0 , this . width , this . height )
@@ -188,7 +196,7 @@ export class Fireworks {
188
196
traceSpeed,
189
197
acceleration,
190
198
mouse
191
- } = opts
199
+ } = this . opts
192
200
193
201
this . traces . push (
194
202
new Trace ( {
@@ -214,9 +222,10 @@ export class Fireworks {
214
222
private initTrace ( ) : void {
215
223
if ( this . waitStopRaf ) return
216
224
225
+ const { delay, mouse } = this . opts
217
226
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 )
220
229
) {
221
230
this . createTrace ( )
222
231
this . raf . tick = 0
@@ -245,7 +254,7 @@ export class Fireworks {
245
254
friction,
246
255
gravity,
247
256
decay
248
- } = opts
257
+ } = this . opts
249
258
250
259
let particlesLength = floor ( particles )
251
260
while ( particlesLength -- ) {
0 commit comments