Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the maximum number of fireworks to start #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/fireworks-js/src/fireworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,14 +64,16 @@ export class Fireworks {
return this.opts
}

start(): void {
start(maxFireworks = -1): void {
if (this.running) return

if (!this.canvas.isConnected) {
this.createCanvas(this.target)
}

this.running = true
this.maxFireworks = maxFireworks;
this.startedSoFar = 0;
this.resize.mount()
this.mouse.mount()
this.raf.mount()
Expand Down Expand Up @@ -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 (
Expand All @@ -235,6 +244,7 @@ export class Fireworks {
) {
this.createTrace()
this.raf.tick = 0
this.startedSoFar++;
}
}

Expand Down