Skip to content

Commit 9078261

Browse files
committed
feat: options are no longer singleton
1 parent 326a706 commit 9078261

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

packages/fireworks-js/src/fireworks.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Explosion } from './explosion.js'
22
import { floor, randomFloat, randomInt } from './helpers.js'
33
import { Mouse } from './mouse.js'
4-
import { opts } from './options.js'
4+
import { Options } from './options.js'
55
import { RequestAnimationFrame } from './raf.js'
66
import { Resize } from './resize.js'
77
import { Sound } from './sound.js'
@@ -15,6 +15,7 @@ export class Fireworks {
1515
private ctx: CanvasRenderingContext2D
1616
private width: number
1717
private height: number
18+
private opts: Options
1819
private sound: Sound
1920
private resize: Resize
2021
private mouse: Mouse
@@ -31,13 +32,15 @@ export class Fireworks {
3132
this.target = container
3233
this.container = container
3334

34-
this.createCanvas(this.target)
35+
this.opts = new Options()
36+
3537
this.updateOptions(options)
38+
this.createCanvas(this.target)
3639

37-
this.sound = new Sound()
40+
this.sound = new Sound(this)
3841
this.resize = new Resize(this)
39-
this.mouse = new Mouse(this.canvas)
40-
this.raf = new RequestAnimationFrame(this.render.bind(this))
42+
this.mouse = new Mouse(this, this.canvas)
43+
this.raf = new RequestAnimationFrame(this, this.render.bind(this))
4144
}
4245

4346
get isRunning(): boolean {
@@ -48,6 +51,10 @@ export class Fireworks {
4851
return __VERSION__
4952
}
5053

54+
get options(): Options {
55+
return this.opts
56+
}
57+
5158
start(): void {
5259
if (this.running) return
5360

@@ -122,7 +129,7 @@ export class Fireworks {
122129
}
123130

124131
updateOptions(options: FireworksOptions): void {
125-
opts.updateOptions(options)
132+
this.opts.update(options)
126133
}
127134

128135
updateSize({
@@ -136,7 +143,7 @@ export class Fireworks {
136143
this.canvas.height = height
137144

138145
this.updateBoundaries({
139-
...opts.boundaries,
146+
...this.opts.boundaries,
140147
width,
141148
height
142149
})
@@ -165,7 +172,7 @@ export class Fireworks {
165172
private render(): void {
166173
if (!this.ctx || !this.running) return
167174

168-
const { opacity, lineStyle, lineWidth } = opts
175+
const { opacity, lineStyle, lineWidth } = this.opts
169176
this.ctx.globalCompositeOperation = 'destination-out'
170177
this.ctx.fillStyle = `rgba(0, 0, 0, ${opacity})`
171178
this.ctx.fillRect(0, 0, this.width, this.height)
@@ -188,7 +195,7 @@ export class Fireworks {
188195
traceSpeed,
189196
acceleration,
190197
mouse
191-
} = opts
198+
} = this.opts
192199

193200
this.traces.push(
194201
new Trace({
@@ -214,9 +221,10 @@ export class Fireworks {
214221
private initTrace(): void {
215222
if (this.waitStopRaf) return
216223

224+
const { delay, mouse } = this.opts
217225
if (
218-
this.raf.tick > randomInt(opts.delay.min, opts.delay.max) ||
219-
(this.mouse.active && opts.mouse.max > this.traces.length)
226+
this.raf.tick > randomInt(delay.min, delay.max) ||
227+
(this.mouse.active && mouse.max > this.traces.length)
220228
) {
221229
this.createTrace()
222230
this.raf.tick = 0
@@ -245,7 +253,7 @@ export class Fireworks {
245253
friction,
246254
gravity,
247255
decay
248-
} = opts
256+
} = this.opts
249257

250258
let particlesLength = floor(particles)
251259
while (particlesLength--) {

packages/fireworks-js/src/mouse.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { opts } from './options.js'
1+
import type { Fireworks } from './fireworks.js'
22

33
export class Mouse {
44
public active = false
55
public x: number
66
public y: number
77

8-
constructor(private readonly canvas: HTMLCanvasElement) {
8+
constructor(
9+
private readonly fw: Fireworks,
10+
private readonly canvas: HTMLCanvasElement
11+
) {
912
this.pointerDown = this.pointerDown.bind(this)
1013
this.pointerUp = this.pointerUp.bind(this)
1114
this.pointerMove = this.pointerMove.bind(this)
1215
}
1316

17+
private get mouseOptions() {
18+
return this.fw.options.mouse
19+
}
20+
1421
subscribe(): void {
1522
this.canvas.addEventListener('pointerdown', this.pointerDown)
1623
this.canvas.addEventListener('pointerup', this.pointerUp)
@@ -24,15 +31,16 @@ export class Mouse {
2431
}
2532

2633
private usePointer(event: PointerEvent, active: boolean): void {
27-
if (opts.mouse.click || opts.mouse.move) {
34+
const { click, move } = this.mouseOptions
35+
if (click || move) {
2836
this.x = event.pageX - this.canvas.offsetLeft
2937
this.y = event.pageY - this.canvas.offsetTop
3038
this.active = active
3139
}
3240
}
3341

3442
private pointerDown(event: PointerEvent): void {
35-
this.usePointer(event, opts.mouse.click)
43+
this.usePointer(event, this.mouseOptions.click)
3644
}
3745

3846
private pointerUp(event: PointerEvent): void {

packages/fireworks-js/src/options.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
MinMax
1010
} from './types.js'
1111

12-
class Options implements FireworksOptions {
12+
export class Options implements FireworksOptions {
1313
public hue: MinMax
1414
public rocketsPoint: MinMax
1515
public opacity: number
@@ -109,10 +109,7 @@ class Options implements FireworksOptions {
109109
}
110110
}
111111

112-
updateOptions<T extends FireworksOptions>(options: T): void {
112+
update<T extends FireworksOptions>(options: T): void {
113113
Object.assign(this, deepMerge(this, options))
114114
}
115115
}
116-
117-
const opts = new Options()
118-
export { opts }

packages/fireworks-js/src/raf.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { opts } from './options.js'
1+
import type { Fireworks } from './fireworks.js'
22

33
export class RequestAnimationFrame {
44
public tick = 0
@@ -8,7 +8,10 @@ export class RequestAnimationFrame {
88
private tolerance = 0.1
99
private now: number
1010

11-
constructor(private readonly render: () => void) {}
11+
constructor(
12+
private readonly fw: Fireworks,
13+
private readonly render: () => void
14+
) {}
1215

1316
start(): void {
1417
this.now = performance.now()
@@ -21,7 +24,7 @@ export class RequestAnimationFrame {
2124
if (delta >= interval - this.tolerance) {
2225
this.render()
2326
this.now = timestamp - (delta % interval)
24-
this.tick += (delta * (opts.intensity * Math.PI)) / 1000
27+
this.tick += (delta * (this.fw.options.intensity * Math.PI)) / 1000
2528
}
2629
}
2730

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import type { Fireworks } from './index.js'
2-
import { opts } from './options.js'
1+
import type { Fireworks } from './fireworks.js'
32

43
export class Resize {
5-
constructor(private readonly fireworks: Fireworks) {
4+
constructor(private readonly fw: Fireworks) {
65
this.handleResize = this.handleResize.bind(this)
76
}
87

98
subscribe(): void {
10-
if (opts.autoresize) {
9+
if (this.fw.options.autoresize) {
1110
window.addEventListener('resize', this.handleResize)
1211
}
1312
}
1413

1514
unsubscribe(): void {
16-
if (opts.autoresize) {
15+
if (this.fw.options.autoresize) {
1716
window.removeEventListener('resize', this.handleResize)
1817
}
1918
}
2019

2120
private handleResize(): void {
22-
this.fireworks.updateSize()
21+
this.fw.updateSize()
2322
}
2423
}

packages/fireworks-js/src/sound.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { Fireworks } from './fireworks.js'
12
import { randomFloat, randomInt } from './helpers.js'
2-
import { opts } from './options.js'
33

44
declare global {
55
interface Window {
@@ -12,12 +12,20 @@ export class Sound {
1212
private audioContext: AudioContext
1313
private onInit = false
1414

15-
constructor() {
15+
constructor(private readonly fw: Fireworks) {
1616
this.init()
1717
}
1818

19+
private get isEnabled() {
20+
return this.fw.options.sound.enabled
21+
}
22+
23+
private get soundOptions() {
24+
return this.fw.options.sound
25+
}
26+
1927
private init(): void {
20-
if (!this.onInit && opts.sound.enabled) {
28+
if (!this.onInit && this.isEnabled) {
2129
this.onInit = true
2230
this.audioContext = new (window.AudioContext ||
2331
window.webkitAudioContext)()
@@ -26,7 +34,7 @@ export class Sound {
2634
}
2735

2836
private async loadSounds(): Promise<void> {
29-
for (const file of opts.sound.files) {
37+
for (const file of this.soundOptions.files) {
3038
const response = await (await fetch(file)).arrayBuffer()
3139

3240
this.audioContext
@@ -41,15 +49,15 @@ export class Sound {
4149
}
4250

4351
play(): void {
44-
if (opts.sound.enabled && this.sounds.length) {
52+
if (this.isEnabled && this.sounds.length) {
4553
const source = this.audioContext.createBufferSource()
4654
const sound = this.sounds[randomInt(0, this.sounds.length - 1)]!
4755
const vol = this.audioContext.createGain()
4856

4957
source.buffer = sound
5058
vol.gain.value = randomFloat(
51-
opts.sound.volume.min / 100,
52-
opts.sound.volume.max / 100
59+
this.soundOptions.volume.min / 100,
60+
this.soundOptions.volume.max / 100
5361
)
5462
vol.connect(this.audioContext.destination)
5563
source.connect(vol)

0 commit comments

Comments
 (0)