Skip to content

Commit

Permalink
Merge pull request #46 from crashmax-dev/render-speed
Browse files Browse the repository at this point in the history
intensity (option)
  • Loading branch information
crashmax-dev committed Feb 25, 2022
2 parents 2c70dc4 + 021c7fb commit 2366a68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
22 changes: 12 additions & 10 deletions public/main.js
Expand Up @@ -17,13 +17,15 @@ const fireworksConfig = {
max: 50
},
opacity: 0.5, // fillStyle
speed: 10,
acceleration: 1.02,
friction: 0.97,
gravity: 1.5,
particles: 90,
trace: 3,
traceSpeed: 10,
explosion: 6,
intensity: 30,
flickering: 50,
lineStyle: 'round',
lineWidth: {
explosion: {
Expand Down Expand Up @@ -217,10 +219,6 @@ folders.fireworks.add(fireworksConfig, 'opacity', 0.1, 1).step(0.1).onChange(()
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworksConfig, 'speed', 1, 100).step(1).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworksConfig, 'acceleration', 1, 2).step(0.01).onChange(() => {
fireworks.setOptions(fireworksConfig)
})
Expand All @@ -241,11 +239,19 @@ folders.fireworks.add(fireworksConfig, 'trace', 1, 10).step(1).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworksConfig, 'traceSpeed', 1, 100).step(1).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworksConfig, 'explosion', 1, 10).step(1).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworks, 'flickering', 0, 100).step(1).onChange(() => {
folders.fireworks.add(fireworksConfig, 'flickering', 0, 100).step(1).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

folders.fireworks.add(fireworksConfig, 'intensity', 1, 60).onChange(() => {
fireworks.setOptions(fireworksConfig)
})

Expand All @@ -254,10 +260,6 @@ folders.fireworks.add(fireworks, 'lineStyle', ['round', 'square']).onChange((lin
fireworks.setOptions(fireworksConfig)
})

// folders.fireworks.add(fireworks, '_experimental', false).name('experimental').onChange(() => {
// fireworks.setOptions(fireworksConfig)
// })

folders.fireworks.add(fireworks, '_running', true).name('enabled').onChange(() => {
fireworks.render()
})
Expand Down
20 changes: 12 additions & 8 deletions src/fireworks.ts
Expand Up @@ -11,7 +11,6 @@ export interface FireworksOptions {
hue?: MinMaxOptions
rocketsPoint?: MinMaxOptions
opacity?: number
speed?: number
acceleration?: number
friction?: number
gravity?: number
Expand All @@ -25,6 +24,7 @@ export interface FireworksOptions {
delay?: MinMaxOptions
brightness?: BrightnessOptions
flickering?: number
intensity?: number
lineWidth?: LineWidthOptions
lineStyle?: LineStyle
}
Expand Down Expand Up @@ -82,13 +82,13 @@ export class Fireworks {
private hue: MinMaxOptions
private rocketsPoint: MinMaxOptions
private opacity: number
private speed: number
private acceleration: number
private friction: number
private gravity: number
private particles: number
private trace: number
private flickering: number
private intensity: number
private explosion: number
private autoresize: boolean
private boundaries: Required<BoundariesOptions>
Expand All @@ -99,6 +99,7 @@ export class Fireworks {
private lineStyle: LineStyle

private _tick = 0
private _timestamp = performance.now()
private _version = version
private _running = false
private _m = false
Expand All @@ -123,7 +124,8 @@ export class Fireworks {
lineStyle = 'round',
flickering = 50,
trace = 3,
speed = 2,
traceSpeed = 10,
intensity = 30,
explosion = 5,
gravity = 1.5,
opacity = 0.5,
Expand Down Expand Up @@ -152,14 +154,14 @@ export class Fireworks {

this.autoresize = autoresize
this.trace = trace
this.speed = speed
this.explosion = explosion
this.gravity = gravity
this.opacity = opacity
this.particles = particles
this.friction = friction
this.acceleration = acceleration
this.flickering = flickering
this.intensity = intensity
this.lineStyle = lineStyle

this.hue = {
Expand Down Expand Up @@ -332,10 +334,10 @@ export class Fireworks {
this.useMouse(event, this._m)
}

private render(): void {
private render(timestamp = this._timestamp): void {
if (!this._ctx || !this._running) return

requestAnimationFrame(() => this.render())
requestAnimationFrame((timestamp) => this.render(timestamp))

this._ctx.globalCompositeOperation = 'destination-out'
this._ctx.fillStyle = `rgba(0, 0, 0, ${this.opacity})`
Expand All @@ -354,7 +356,9 @@ export class Fireworks {
this.drawTrace()
this.drawExplosion()

this._tick++
const timeDiff = timestamp - this._timestamp
this._timestamp = timestamp
this._tick += timeDiff * (this.intensity * Math.PI) / 1000
}

private drawBoundaries() {
Expand Down Expand Up @@ -391,7 +395,7 @@ export class Fireworks {
randomInt(this.boundaries.y, this.boundaries.height * 0.5),
ctx: this._ctx,
hue: randomInt(this.hue.min, this.hue.max),
speed: this.speed,
speed: this.traceSpeed,
acceleration: this.acceleration,
traceLength: this.trace
})
Expand Down

0 comments on commit 2366a68

Please sign in to comment.