Skip to content

Commit 5bf0dc8

Browse files
authored
Merge pull request #91 from crashmax-dev/launch-fire
feat: added launch rockets method
2 parents 2c5a8b9 + ffb9486 commit 5bf0dc8

File tree

4 files changed

+111
-47
lines changed

4 files changed

+111
-47
lines changed

examples/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"preview": "vite preview"
99
},
1010
"dependencies": {
11+
"@zero-dependency/dom": "^0.10.0",
1112
"fireworks-js": "workspace:*"
1213
},
1314
"devDependencies": {

examples/basic/src/index.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
import { Fireworks } from 'fireworks-js'
2+
import { el } from '@zero-dependency/dom'
23
import './style.css'
34

45
const app = document.querySelector<HTMLDivElement>('#app')!
56
const fireworks = new Fireworks(app)
67

7-
const button = document.createElement('button')
8-
button.textContent = 'Start'
9-
button.style.position = 'absolute'
10-
button.style.zIndex = '999'
11-
button.addEventListener('click', () => {
12-
fireworks.start()
13-
stopFireworks(fireworks)
8+
const start = el(
9+
'button',
10+
{
11+
onclick: () => {
12+
fireworks.start()
13+
}
14+
},
15+
'Start'
16+
)
17+
18+
const stop = el(
19+
'button',
20+
{
21+
onclick: () => {
22+
fireworks.waitStop()
23+
}
24+
},
25+
'Stop'
26+
)
27+
28+
const launch = el(
29+
'button',
30+
{
31+
onclick: () => {
32+
fireworks.launch(Number(count.value))
33+
}
34+
},
35+
'Launch'
36+
)
37+
38+
const count = el('input', {
39+
value: '1',
40+
min: '1',
41+
max: '15',
42+
type: 'number',
43+
placeholder: 'count',
44+
style: {
45+
width: '2rem'
46+
}
1447
})
1548

16-
document.body.appendChild(button)
49+
const buttons = el(
50+
'div',
51+
{
52+
style: {
53+
position: 'absolute',
54+
display: 'flex',
55+
gap: '4px'
56+
}
57+
},
58+
start,
59+
stop,
60+
launch,
61+
count
62+
)
1763

18-
async function stopFireworks(fireworks: Fireworks) {
19-
console.time()
20-
await new Promise((resolve) => setTimeout(resolve, 2000))
21-
await fireworks.waitStop()
22-
console.timeEnd()
23-
}
64+
document.body.appendChild(buttons)

packages/fireworks-js/src/fireworks.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ export class Fireworks {
110110
this.ctx.clearRect(0, 0, this.width, this.height)
111111
}
112112

113+
launch(count = 1): void {
114+
for (let i = 0; i < count; i++) {
115+
this.createTrace()
116+
}
117+
118+
if (!this.waitStopRaf) {
119+
this.start()
120+
this.waitStop()
121+
}
122+
}
123+
113124
updateOptions(options: FireworksOptions): void {
114125
opts.updateOptions(options)
115126
}
@@ -154,24 +165,23 @@ export class Fireworks {
154165
private render(): void {
155166
if (!this.ctx || !this.running) return
156167

168+
const { opacity, lineStyle, lineWidth } = opts
157169
this.ctx.globalCompositeOperation = 'destination-out'
158-
this.ctx.fillStyle = `rgba(0, 0, 0, ${opts.opacity})`
170+
this.ctx.fillStyle = `rgba(0, 0, 0, ${opacity})`
159171
this.ctx.fillRect(0, 0, this.width, this.height)
160172
this.ctx.globalCompositeOperation = 'lighter'
161-
this.ctx.lineCap = opts.lineStyle
173+
this.ctx.lineCap = lineStyle
162174
this.ctx.lineJoin = 'round'
175+
this.ctx.lineWidth = randomFloat(lineWidth.trace.min, lineWidth.trace.max)
163176

164177
this.initTrace()
165178
this.drawTrace()
166179
this.drawExplosion()
167180
}
168181

169-
private initTrace(): void {
170-
if (this.waitStopRaf) return
171-
182+
private createTrace(): void {
172183
const {
173184
hue,
174-
delay,
175185
rocketsPoint,
176186
boundaries,
177187
traceLength,
@@ -180,40 +190,40 @@ export class Fireworks {
180190
mouse
181191
} = opts
182192

193+
this.traces.push(
194+
new Trace({
195+
x: (this.width * randomInt(rocketsPoint.min, rocketsPoint.max)) / 100,
196+
y: this.height,
197+
dx:
198+
(this.mouse.x && mouse.move) || this.mouse.active
199+
? this.mouse.x
200+
: randomInt(boundaries.x, boundaries.width - boundaries.x * 2),
201+
dy:
202+
(this.mouse.y && mouse.move) || this.mouse.active
203+
? this.mouse.y
204+
: randomInt(boundaries.y, boundaries.height * 0.5),
205+
ctx: this.ctx,
206+
hue: randomInt(hue.min, hue.max),
207+
speed: traceSpeed,
208+
acceleration,
209+
traceLength: floor(traceLength)
210+
})
211+
)
212+
}
213+
214+
private initTrace(): void {
215+
if (this.waitStopRaf) return
216+
183217
if (
184-
this.raf.tick > randomInt(delay.min, delay.max) ||
185-
(this.mouse.active && mouse.max > this.traces.length)
218+
this.raf.tick > randomInt(opts.delay.min, opts.delay.max) ||
219+
(this.mouse.active && opts.mouse.max > this.traces.length)
186220
) {
187-
this.traces.push(
188-
new Trace({
189-
x: (this.width * randomInt(rocketsPoint.min, rocketsPoint.max)) / 100,
190-
y: this.height,
191-
dx:
192-
(this.mouse.x && mouse.move) || this.mouse.active
193-
? this.mouse.x
194-
: randomInt(boundaries.x, boundaries.width - boundaries.x * 2),
195-
dy:
196-
(this.mouse.y && mouse.move) || this.mouse.active
197-
? this.mouse.y
198-
: randomInt(boundaries.y, boundaries.height * 0.5),
199-
ctx: this.ctx,
200-
hue: randomInt(hue.min, hue.max),
201-
speed: traceSpeed,
202-
acceleration,
203-
traceLength: floor(traceLength)
204-
})
205-
)
206-
221+
this.createTrace()
207222
this.raf.tick = 0
208223
}
209224
}
210225

211226
private drawTrace(): void {
212-
this.ctx.lineWidth = randomFloat(
213-
opts.lineWidth.trace.min,
214-
opts.lineWidth.trace.max
215-
)
216-
217227
let traceLength = this.traces.length
218228
while (traceLength--) {
219229
this.traces[traceLength]!.draw()

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)