From f3c0bb2a35386870961e51fe952c331e32871749 Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 28 Nov 2023 10:50:26 +0100 Subject: [PATCH] Specify the number of fireworks to start This is different than launch() because it respects the delay (whereas launch() just does everything at once). Feel free to not accept this or change it however you like. I haven't tested it at all as the changes I made were in JavaScript and I can't get the project to build/work with typescript. --- packages/fireworks-js/src/fireworks.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/fireworks-js/src/fireworks.ts b/packages/fireworks-js/src/fireworks.ts index 7f52014..41a0c94 100644 --- a/packages/fireworks-js/src/fireworks.ts +++ b/packages/fireworks-js/src/fireworks.ts @@ -21,6 +21,8 @@ export class Fireworks { private explosions: Explosion[] = [] private waitStopRaf: (() => void) | null private running = false + private maxFireworks: number + private startedSoFar: number private readonly opts: Options private readonly sound: Sound @@ -62,7 +64,7 @@ export class Fireworks { return this.opts } - start(): void { + start(maxFireworks = -1): void { if (this.running) return if (!this.canvas.isConnected) { @@ -70,6 +72,8 @@ export class Fireworks { } this.running = true + this.maxFireworks = maxFireworks; + this.startedSoFar = 0; this.resize.mount() this.mouse.mount() this.raf.mount() @@ -226,7 +230,12 @@ export class Fireworks { } private initTrace(): void { - if (this.waitStopRaf) return + let maxReached = (this.maxFireworks >= 0 && this.startedSoFar >= this.maxFireworks); + if (maxReached) { + this.waitStop(); + } + + if (this.waitStopRaf || maxReached) return const { delay, mouse } = this.opts if ( @@ -235,6 +244,7 @@ export class Fireworks { ) { this.createTrace() this.raf.tick = 0 + this.startedSoFar++; } }