Skip to content

Commit 4286b38

Browse files
authored
Merge pull request #94 from crashmax-dev/drop-singleton-options
feat: options are no longer singleton
2 parents 326a706 + 746430b commit 4286b38

File tree

11 files changed

+108
-68
lines changed

11 files changed

+108
-68
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ Start/stop fireworks.
286286
#### `.clear()`
287287
Cleaning the canvas from fireworks.
288288

289+
#### `.currentOptions`
290+
Getting current fireworks options.
291+
289292
#### `.updateOptions(options)`
290293
Force update fireworks options.\
291294
Type: [`options`](https://github.com/crashmax-dev/fireworks-js/blob/6819ec8456ecb97140a8e1f41959ca2da5c17ddf/packages/fireworks-js/src/types.ts#L3-L25)

packages/fireworks-js/src/fireworks.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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'
88
import { Trace } from './trace.js'
9-
import { FireworksOptions, IBoundaries, Sizes } from './types.js'
9+
import type { FireworksOptions, IBoundaries, Sizes } from './types.js'
1010

1111
export 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--) {

packages/fireworks-js/src/mouse.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
1-
import { opts } from './options.js'
1+
import type { Options } from './options.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 options: Options,
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

14-
subscribe(): void {
17+
private get mouseOptions() {
18+
return this.options.mouse
19+
}
20+
21+
mount(): void {
1522
this.canvas.addEventListener('pointerdown', this.pointerDown)
1623
this.canvas.addEventListener('pointerup', this.pointerUp)
1724
this.canvas.addEventListener('pointermove', this.pointerMove)
1825
}
1926

20-
unsubscribe(): void {
27+
unmount(): void {
2128
this.canvas.removeEventListener('pointerdown', this.pointerDown)
2229
this.canvas.removeEventListener('pointerup', this.pointerUp)
2330
this.canvas.removeEventListener('pointermove', this.pointerMove)
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { opts } from './options.js'
1+
import type { Options } from './options.js'
22

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

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

13-
start(): void {
16+
mount(): void {
1417
this.now = performance.now()
1518
const interval = 1000 / this.fps
1619

@@ -21,14 +24,14 @@ 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.options.intensity * Math.PI)) / 1000
2528
}
2629
}
2730

2831
this.rafId = requestAnimationFrame(raf)
2932
}
3033

31-
stop() {
34+
unmount() {
3235
cancelAnimationFrame(this.rafId)
3336
}
3437
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import type { Fireworks } from './index.js'
2-
import { opts } from './options.js'
1+
import type { Options } from './options.js'
32

43
export class Resize {
5-
constructor(private readonly fireworks: Fireworks) {
4+
constructor(
5+
private readonly options: Options,
6+
private readonly updateSize: () => void
7+
) {
68
this.handleResize = this.handleResize.bind(this)
79
}
810

9-
subscribe(): void {
10-
if (opts.autoresize) {
11+
mount(): void {
12+
if (this.options.autoresize) {
1113
window.addEventListener('resize', this.handleResize)
1214
}
1315
}
1416

15-
unsubscribe(): void {
16-
if (opts.autoresize) {
17+
unmount(): void {
18+
if (this.options.autoresize) {
1719
window.removeEventListener('resize', this.handleResize)
1820
}
1921
}
2022

2123
private handleResize(): void {
22-
this.fireworks.updateSize()
24+
this.updateSize()
2325
}
2426
}

0 commit comments

Comments
 (0)