Skip to content

Commit 41c8869

Browse files
committed
feat: added launch rockets method
1 parent 2c5a8b9 commit 41c8869

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

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.waitStop()
120+
this.start()
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()

0 commit comments

Comments
 (0)