This fireworks show is made only from the code below. Tap the sky to launch one, or open Firework controls at the bottom of it. Tap ⛶ Big to see it full screen. Tap the Try buttons to change the code, or type in it yourself.
The code HTML, CSS and JavaScript
The big picture
The page does three jobs, about 60 times every second:
Update: move everything a tiny bit.
Draw: paint everything where it is now.
Repeat.
Everything on screen lives in a list: rockets, parts (the sparks), and flashes. Each item in a list is a small object holding its own numbers, like x, y, and life.
CFG is a box of numbers. The sliders change these numbers while the page runs, and the code reads them fresh every frame, which is why dragging a slider changes the show instantly.
Try: change sparks: 90 to 200 and reload.
Part 2: The canvas
var canvas = document.getElementById('sky');
var ctx = canvas.getContext('2d');
canvas is the drawing board.
ctx is the pen. Every ctx.fillRect(...) or ctx.arc(...) is the pen drawing something.
W and H are the screen width and height.
u is a size unit (Math.min(W, H) / 500). Sizes are written as 6.5 * u instead of 6.5 so the fireworks look the same size on a phone and a laptop.
dpr is the screen's sharpness. Phones pack extra pixels in, so the canvas is made bigger and scaled back down to look crisp.
skyline is a list of rectangles of random width and height, drawn as the black buildings.
Part 3: Colors
'hsl(' + h + ',100%,60%)'
HSL means hue, saturation, lightness. Hue is a color wheel from 0 to 360 (0 red, 120 green, 240 blue). Math.random() * 360 picks a random color. For "Two colors," the second color is h + 150, which is far across the wheel.
Try: in pickColors, change 100% to 40% and the colors turn pastel.
Part 4: Rockets and sparks
A rocket is an object:
{ x, y, vx, vy, ty }
x, y is its position.
vx, vy is its velocity, how far it moves each frame. On screens, y grows going down, so a rocket going up has a negative vy.
ty is the height where it will explode.
launch builds one rocket at the bottom of the screen with a random speed and target height.
explode is where the shapes come from. It runs a loop, once per spark, and gives each spark a direction and speed:
Circle: a random angle, then cos(angle) for sideways speed and sin(angle) for up and down. Random speeds fill the circle.
Ring: the same, but every spark gets the same speed, and angles are evenly spaced (i / n * 6.2832, where 6.2832 is a full circle in radians).
Heart: a heart equation turns each angle into a position, and that position becomes the spark's speed, so the burst grows into a heart.
Willow: slow, gold, and long-lived, so they hang and drip.
This changes shape: 'Circle' in the settings, so every burst starts with that shape.
Try: in the Ring branch, change sp = R to sp = R * (i % 2 ? 1 : 0.5) and you get a ring inside a ring.
Pick Ring above first, so the bursts are rings.
Part 5: The update
p.x += p.vx * step;
p.y += p.vy * step;
Moving something is just adding its velocity to its position every frame.
Friction:p.vx *= fr where fr is 0.968. Each frame the spark keeps 96.8% of its speed, so it slows down like it's pushing through air.
Gravity:p.vy = p.vy * fr + 0.09 * u * CFG.gravity * step adds a little downward speed every frame. Slow plus a downward pull makes the arc.
step:dt * 60. On a 60 Hz screen it's about 1, and on a 120 Hz screen it's about 0.5. Because movement is multiplied by it, the show runs at the same speed on any phone.
Rockets explode when p.y <= p.ty (they reached the target height) or when they run out of speed.
Cleaning up: each frame, the code builds a new list of only the survivors (aliveP.push(p)) and drops anything whose life passed maxLife. That's how sparks disappear.
Try: change 0.968 to 0.99. Sparks slide much farther.
It doesn't erase the screen. It paints the sky color on top at partial strength, so last frame's sparks are still faintly visible. That leftover glow is the trail. The Trail slider changes how strong that fade is.
Stars: tiny squares whose alpha follows Math.sin(time ...), so they twinkle.
Switch to light mode:ctx.globalCompositeOperation = 'lighter'. Now overlapping colors add up and get brighter, like real light.
Flashes: a big soft circle of color that fades in 14 frames, which lights up the sky for a moment.
Rockets and sparks: each spark is a small circle. Its fade is Math.pow(1 - t, 1.4), where t goes from 0 (new) to 1 (dead). The sparkle is a Math.sin(...) that makes the brightness wobble.
Skyline last: switch back to normal mode and draw the black buildings on top, so sparks look like they're behind the city.
Part 7: The loop and the controls
requestAnimationFrame(loop);
This asks the browser to call loop again right before the next screen refresh. Inside loop:
acc += CFG.rate * dt is a timer. When it reaches 1, the code launches a rocket and subtracts 1. That's how "1.2 launches per second" works.
Then it runs update(...) and draw().
Tapping:canvas.addEventListener('pointerdown', ...) runs when you touch the screen, and calls launch with the spot you tapped.
The sliders:KNOBS is a list where each entry is [name, label, min, max, step]. A loop builds one slider for each entry.
To add a new slider, you do three things:
Add a number to CFG, for example wobble: 0.
Add a line to KNOBS.
Use CFG.wobble somewhere in the code, like p.x += Math.sin(p.life) * CFG.wobble.
This does all three steps for you. Open Firework controls on the picture and drag the new Wobble slider.
Five experiments to learn from
1. Change 0.09 in the gravity line to -0.05. Sparks float up.
2. Change p.size * (1 - t * 0.4) to p.size * (1 + t). Sparks grow as they fade.
3. In explode, change life = 70 + ... to life = 30 + .... Short, snappy bursts.
4. In launch, change H * (0.14 + Math.random() * 0.3) to H * 0.15. Every firework bursts at the same height.
5. Change '#ffe9b0' (the rocket color) to '#ff0000'.