Skip to content

Commit 89f9697

Browse files
committed
chore: refactor
1 parent 06061b1 commit 89f9697

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

packages/fireworks-js/src/fireworks.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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,16 +15,17 @@ export class Fireworks {
1515
private ctx: CanvasRenderingContext2D
1616
private width: number
1717
private height: number
18-
private opts: Options
19-
private sound: Sound
20-
private resize: Resize
21-
private mouse: Mouse
2218
private traces: Trace[] = []
2319
private explosions: Explosion[] = []
2420
private waitStopRaf: (() => void) | null
25-
private raf: RequestAnimationFrame
2621
private running = false
2722

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+
2829
constructor(
2930
container: Element | HTMLCanvasElement,
3031
options: FireworksOptions = {}
@@ -37,10 +38,10 @@ export class Fireworks {
3738
this.updateOptions(options)
3839
this.createCanvas(this.target)
3940

40-
this.sound = new Sound(this)
41-
this.resize = new Resize(this)
42-
this.mouse = new Mouse(this, this.canvas)
43-
this.raf = new RequestAnimationFrame(this, 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))
4445
}
4546

4647
get isRunning(): boolean {
@@ -51,7 +52,7 @@ export class Fireworks {
5152
return __VERSION__
5253
}
5354

54-
get options(): Options {
55+
get currentOptions(): Options {
5556
return this.opts
5657
}
5758

packages/fireworks-js/src/mouse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Fireworks } from './fireworks.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

88
constructor(
9-
private readonly fw: Fireworks,
9+
private readonly options: Options,
1010
private readonly canvas: HTMLCanvasElement
1111
) {
1212
this.pointerDown = this.pointerDown.bind(this)
@@ -15,7 +15,7 @@ export class Mouse {
1515
}
1616

1717
private get mouseOptions() {
18-
return this.fw.options.mouse
18+
return this.options.mouse
1919
}
2020

2121
mount(): void {

packages/fireworks-js/src/raf.ts

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

33
export class RequestAnimationFrame {
44
public tick = 0
@@ -9,7 +9,7 @@ export class RequestAnimationFrame {
99
private now: number
1010

1111
constructor(
12-
private readonly fw: Fireworks,
12+
private readonly options: Options,
1313
private readonly render: () => void
1414
) {}
1515

@@ -24,7 +24,7 @@ export class RequestAnimationFrame {
2424
if (delta >= interval - this.tolerance) {
2525
this.render()
2626
this.now = timestamp - (delta % interval)
27-
this.tick += (delta * (this.fw.options.intensity * Math.PI)) / 1000
27+
this.tick += (delta * (this.options.intensity * Math.PI)) / 1000
2828
}
2929
}
3030

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
import type { Fireworks } from './fireworks.js'
1+
import type { Options } from './options.js'
22

33
export class Resize {
4-
constructor(private readonly fw: Fireworks) {
4+
constructor(
5+
private readonly options: Options,
6+
private readonly updateSize: () => void
7+
) {
58
this.handleResize = this.handleResize.bind(this)
69
}
710

811
mount(): void {
9-
if (this.fw.options.autoresize) {
12+
if (this.options.autoresize) {
1013
window.addEventListener('resize', this.handleResize)
1114
}
1215
}
1316

1417
unmount(): void {
15-
if (this.fw.options.autoresize) {
18+
if (this.options.autoresize) {
1619
window.removeEventListener('resize', this.handleResize)
1720
}
1821
}
1922

2023
private handleResize(): void {
21-
this.fw.updateSize()
24+
this.updateSize()
2225
}
2326
}

packages/fireworks-js/src/sound.ts

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

44
declare global {
55
interface Window {
@@ -8,20 +8,20 @@ declare global {
88
}
99

1010
export class Sound {
11-
private sounds: AudioBuffer[] = []
11+
private buffers: AudioBuffer[] = []
1212
private audioContext: AudioContext
1313
private onInit = false
1414

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

1919
private get isEnabled() {
20-
return this.fw.options.sound.enabled
20+
return this.options.sound.enabled
2121
}
2222

2323
private get soundOptions() {
24-
return this.fw.options.sound
24+
return this.options.sound
2525
}
2626

2727
private init(): void {
@@ -40,7 +40,7 @@ export class Sound {
4040
this.audioContext
4141
.decodeAudioData(response)
4242
.then((buffer) => {
43-
this.sounds.push(buffer)
43+
this.buffers.push(buffer)
4444
})
4545
.catch((err) => {
4646
throw err
@@ -49,19 +49,19 @@ export class Sound {
4949
}
5050

5151
play(): void {
52-
if (this.isEnabled && this.sounds.length) {
53-
const source = this.audioContext.createBufferSource()
54-
const sound = this.sounds[randomInt(0, this.sounds.length - 1)]!
55-
const vol = this.audioContext.createGain()
52+
if (this.isEnabled && this.buffers.length) {
53+
const bufferSource = this.audioContext.createBufferSource()
54+
const soundBuffer = this.buffers[randomInt(0, this.buffers.length - 1)]!
55+
const volume = this.audioContext.createGain()
5656

57-
source.buffer = sound
58-
vol.gain.value = randomFloat(
57+
bufferSource.buffer = soundBuffer
58+
volume.gain.value = randomFloat(
5959
this.soundOptions.volume.min / 100,
6060
this.soundOptions.volume.max / 100
6161
)
62-
vol.connect(this.audioContext.destination)
63-
source.connect(vol)
64-
source.start(0)
62+
volume.connect(this.audioContext.destination)
63+
bufferSource.connect(volume)
64+
bufferSource.start(0)
6565
} else {
6666
this.init()
6767
}

0 commit comments

Comments
 (0)