176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fireworks — Animation Handbook</title>
<meta name="description" content="Rockets rise and burst into glowing sparks that fall and fade. Best for celebrations.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/fireworks/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
<link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Fireworks — Animation Handbook">
<meta property="og:description" content="Rockets rise and burst into glowing sparks that fall and fade. Best for celebrations.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/fireworks/">
<meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Fireworks — Animation Handbook">
<meta name="twitter:description" content="Rockets rise and burst into glowing sparks that fall and fade. Best for celebrations.">
<meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Fireworks", "name": "Fireworks", "description": "Rockets rise and burst into glowing sparks that fall and fade. Best for celebrations.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/fireworks/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Fireworks"}</script>
<style>
:root{--ui-accent:#ffce5a}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
/* The sky is the stage's background, drawn from under its see-through border; the canvas trails fade to it, not to a color */
.stage{position:relative;overflow:hidden;background:linear-gradient(#02030a,#0b0c22 55%,#221640);background-origin:border-box}
canvas{position:absolute;inset:0;display:block;width:100%;height:100%}
.city{position:absolute;left:0;bottom:0;width:100%;height:24%;display:block}
/* See inside: a second canvas over the fireworks and a key at the bottom right. Compare: the skyline once in each half, a
thin divider and a name over each half. Normal shows none of them. */
.over,.twins,.key,.half{display:none}
.over,.twins{pointer-events:none}
.twins{position:absolute;inset:0}
.twins::after{content:"";position:absolute;top:0;bottom:0;left:calc(50% - 1px);width:2px;background:rgba(244,244,242,.45)}
.twin{position:absolute;left:0;bottom:0;width:50%;height:24%;display:block}
.twin+.twin{left:50%}
.cap{color:#f4f4f2;font:600 13px/17px var(--hb-font);white-space:nowrap}
.half{position:absolute;top:8px;padding:4px 9px;border-radius:999px;background:rgba(2,3,10,.86)}
.half.first{left:8px}
.half.second{left:calc(50% + 8px)}
.key{position:absolute;right:8px;bottom:8px;padding:5px 10px;border-radius:10px;background:rgba(2,3,10,.86)}
.key .cap{display:flex;align-items:center;gap:7px}
.key svg{flex:none;width:16px;height:16px;fill:none;stroke:#fff;stroke-width:1.6;stroke-linecap:round}
.stage[data-hb-mode="inside"] .over,.stage[data-hb-mode="inside"] .key,.stage[data-hb-mode="compare"] .twins,.stage[data-hb-mode="compare"] .half{display:block}
.stage[data-hb-mode="compare"] .city{display:none}
@media(max-width:600px){.cap{font-size:12px;line-height:16px}.key{padding:4px 8px}}
</style>
<link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
<script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="loop">
<nav class="hb-bar" aria-label="Animation Handbook">
<a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
<span class="hb-pager"><a href="../snow-rain/" rel="prev" aria-label="Previous: Snow / Rain"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Snow / Rain</span></a><a href="../topographic-lines/" rel="next" aria-label="Next: Topographic Lines"><span><span class="hb-dir">Next: </span>Topographic Lines</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
<a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
<header class="hb-head">
<p class="hb-cat">07.20 · Ambient & Background</p>
<h1>Fireworks</h1>
<p class="hb-lede">Rockets rise and burst into glowing sparks that fall and fade. Best for celebrations.</p>
</header>
<section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">1</span>
<div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>
</div>
<div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
<button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
<button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
<button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
</div>
<div class="stage">
<canvas id="cvs" aria-hidden="true"></canvas>
<svg class="city" viewBox="0 0 1000 60" preserveAspectRatio="xMidYMax slice" aria-hidden="true">
<path fill="#05060d" d="M0 60V46H12V28H35V39H65V26H78V30H93V37H122V30H136V45H166V33H193V20H216V37H235V24H265V41H279V41H292V21H306V30H333V18H345V33H365V20H384V39H401V27H415V22H431V39H459V42H479V42H508V46H525V32H538V42H559V39H590V22H602V14H604V22H615V28H643V42H664V44H689V33H709V23H732V22H750V48H758V40H760V48H768V40H798V33H825V24H849V46H879V27H892V18H919V28H934V43H950V18H972V29H988V30H1000V60Z"/>
<path fill="#f3c26a" fill-opacity=".6" d="M20 44h2v2h-2zM68 42h2v2h-2zM159 49h2v2h-2zM149 55h2v2h-2zM179 43h2v2h-2zM174 49h2v2h-2zM211 24h2v2h-2zM201 36h2v2h-2zM243 46h2v2h-2zM282 51h2v2h-2zM295 25h2v2h-2zM309 34h2v2h-2zM404 31h2v2h-2zM409 37h2v2h-2zM418 26h2v2h-2zM444 43h2v2h-2zM449 55h2v2h-2zM497 52h2v2h-2zM528 48h2v2h-2zM533 48h2v2h-2zM572 43h2v2h-2zM562 55h2v2h-2zM593 26h2v2h-2zM608 26h2v2h-2zM598 38h2v2h-2zM603 44h2v2h-2zM646 46h2v2h-2zM646 52h2v2h-2zM722 39h2v2h-2zM791 50h2v2h-2zM811 37h2v2h-2zM811 49h2v2h-2zM838 28h2v2h-2zM895 28h2v2h-2zM910 34h2v2h-2zM968 22h2v2h-2zM953 34h2v2h-2zM963 46h2v2h-2zM958 52h2v2h-2z"/>
</svg>
<div class="twins" inert aria-hidden="true"></div>
<canvas class="over" aria-hidden="true"></canvas>
<div class="key" aria-hidden="true">
<p class="cap" data-hb-caption><svg viewBox="0 0 16 16"><path d="M8 16v-6" stroke-dasharray="2 2" stroke-linecap="butt"/><path d="m5 1.5 6 6m0-6-6 6"/></svg>Rocket: bursts where it stops rising</p>
<p class="cap" data-hb-caption><svg viewBox="0 0 16 16"><path d="M3 13 13 3M7 3h6v6"/></svg>Sparks fly out and slow down</p>
<p class="cap" data-hb-caption><svg viewBox="0 0 16 16"><path stroke="#ffce5a" stroke-width="2.2" d="M8 1.5v12M4 9.5l4 4 4-4"/></svg>Gravity pulls them down</p>
<p class="cap" data-hb-caption><svg viewBox="0 0 16 16"><path stroke-width="1.2" d="M5.5 1.5h5v13h-5z"/><path fill="#fff" stroke="none" d="M5.5 8h5v6.5h-5z"/></svg>They fade as they age</p>
</div>
<p class="cap half first" aria-hidden="true" data-hb-caption>Hangs in place</p>
<p class="cap half second" aria-hidden="true" data-hb-caption>Falls and fades</p>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-pause" data-hb-pause data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button>
<label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
</div>
<div class="hb-mode-notes" aria-live="polite">
<p data-hb-for="inside" hidden>Inside, the dashed line is a rocket's climb: it bursts where it stops rising. Arrows show sparks flying out and slowing, gravity pulling them down, and each bar how much glow is left.</p>
<p data-hb-for="compare" hidden>Hangs in place has no gravity and no fading: its sparks stop in midair, fully lit, then go out together. Falls and fades pulls them down while they dim.</p>
</div>
</section>
<section class="hb-step hb-try" aria-labelledby="hb-try-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">2</span>
<div><h2 id="hb-try-title">Try it</h2><p>Change a setting and see the difference as it moves.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="often-lbl">How often</p>
<div class="seg" id="often-seg" role="group" aria-labelledby="often-lbl">
<button type="button" data-often="rarely" aria-pressed="false">Rarely</button>
<button type="button" data-often="sometimes" class="on" aria-pressed="true">Sometimes</button>
<button type="button" data-often="often" aria-pressed="false">Often</button>
</div>
<p class="hb-hint">Rarely keeps it calm behind text.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="pal-lbl">Colors</p>
<div class="seg" id="pal-seg" role="group" aria-labelledby="pal-lbl">
<button type="button" data-pal="many" class="on" aria-pressed="true">Many colors</button>
<button type="button" data-pal="gold" aria-pressed="false">Gold</button>
<button type="button" data-pal="blue" aria-pressed="false">Blue</button>
</div>
<p class="hb-hint">Each burst picks one color from the set.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="trail-lbl">Trails</p>
<div class="seg" id="trail-seg" role="group" aria-labelledby="trail-lbl">
<button type="button" data-trail="none" aria-pressed="false">None</button>
<button type="button" data-trail="short" aria-pressed="false">Short</button>
<button type="button" data-trail="long" class="on" aria-pressed="true">Long</button>
</div>
<p class="hb-hint">Trails show the path each spark has flown.</p>
</div>
</div>
<details class="hb-options">
<summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="shape-lbl">Burst shape</p>
<div class="seg" id="shape-seg" role="group" aria-labelledby="shape-lbl">
<button type="button" data-shape="round" class="on" aria-pressed="true">Round</button>
<button type="button" data-shape="ring" aria-pressed="false">Ring</button>
<button type="button" data-shape="willow" aria-pressed="false">Willow</button>
</div>
<p class="hb-hint">Willow sparks droop like the branches of a tree.</p>
</div>
</div>
</details>
</section>
<section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
<div class="hb-step-head">
<span class="hb-num" aria-hidden="true">3</span>
<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
</div>
<div class="hb-card">
<p class="hb-prompt">Add fireworks to [the dark section you want them over]. On a see-through canvas laid over the section, launch a rocket from the bottom every so often; it slows as it climbs and, at the top of its climb, bursts into many sparks that fly out in all directions. Slow the sparks a little on every frame, pull them gently down and fade them out, so each burst blooms, droops and dies away. For trails, cover the canvas with a faint see-through layer instead of clearing it, the same number of times per second on every screen. Use fewer sparks on phones. If the visitor has reduced motion turned on, show a still picture of the bursts. Match the settings listed below.</p>
<div class="hb-chips-box" hidden>
<p class="hb-chips-title">Your settings</p>
<ul class="hb-chips"></ul>
</div>
<div class="hb-copy-row">
<button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
<p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
</div>
</div>
<div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
</section>
<section class="hb-about" aria-labelledby="hb-what-title">
<div class="hb-what">
<h2 id="hb-what-title">What it is</h2>
<div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
</div>
<div class="hb-fit">
<h2>Good for</h2>
<ul class="hb-tags hb-good"><li>Launch and event pages</li><li>Holiday greetings</li><li>Success screens</li><li>Game wins</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Calm brands</li><li>Long reading pages</li></ul>
</div>
</section>
<section class="hb-related" aria-labelledby="hb-related-title" hidden>
<h2 id="hb-related-title">Similar animations</h2>
<div class="hb-rel-list"></div>
</section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.querySelector('.stage'),slowTog=document.getElementById('slow-tog');
const phone=matchMedia('(max-width:600px),(max-height:500px)'); // phones and short screens get fewer sparks
const FRAME=1000/60; // one step is what a 60 Hz frame adds, so a faster screen or a slow frame does not change the speed
// [hue, saturation, lightness] a burst can take: Many colors picks any of them, Gold and Blue keep to their own few
const PALETTES={many:[[0,90,62],[30,95,60],[52,95,62],[130,70,58],[190,85,62],[220,90,70],[280,80,72],[320,85,68]],
gold:[[36,95,60],[44,100,66],[28,90,58]],blue:[[195,90,66],[215,95,72],[255,85,74],[280,75,74]]};
const EVERY={rarely:130,sometimes:70,often:32}; // 60ths of a second between launches, on average
const WASH={none:1,short:.3,long:.13}; // how much of the picture one cover takes away; one cover every 60th of a second
const STILL=[[.5,.3,24],[.24,.38,40],[.77,.22,12]]; // the still picture's bursts: where (shares of the stage) and how old
let W=0,H=0,U=1,D=1,raf=null,lastT=null,fa=0,paused=false,wait=0; // U: the stage's height in 400ths; D: device pixels per CSS pixel
let OFTEN='sometimes',PAL='many',TRAIL='long',SHAPE='round',rockets=[],bursts=[],marks=[]; // marks: faded patches to wipe
// See inside draws on its own canvas (over); Compare keeps Normal's picture on a shelf and its state in saved, and runs two
// worlds, Hangs in place then Falls and fades. X0 and HW: the part of the canvas the world in play owns (all of it in Normal);
// FALL: whether its sparks fall and fade; rnd: where launches, bursts and the crackle take their random numbers (seeded in Compare).
const over=document.querySelector('.over'),octx=over.getContext('2d'),shelf=document.createElement('canvas'),sctx=shelf.getContext('2d');
const SEED=7,worlds=[{fall:false},{fall:true}];
for(let i=0;i<2;i++){const c=document.querySelector('.city').cloneNode(true);c.setAttribute('class','twin');document.querySelector('.twins').append(c);} // the same skyline under each half
let mode='normal',saved=null,X0=0,HW=0,FALL=true,rnd=Math.random,seed=0;
const rand=()=>{let t=seed=seed+0x6D2B79F5|0;t=Math.imul(t^t>>>15,t|1);t^=t+Math.imul(t^t>>>7,t|61);return((t^t>>>14)>>>0)/4294967296;};
// A rocket leaves the bottom (sx) just fast enough for gravity to stop it at the height where it bursts
function launch(){
const x=X0+HW*(.15+rnd()*.7),top=H*(.2+rnd()*.26),g=.07*U;
rockets.push({x,y:H,px:x,py:H,sx:x,vx:(rnd()-.5)*.5*U,vy:-Math.sqrt(2*g*(H-top)),g});
}
// A burst keeps its sparks in one typed array: x, y, speed across, speed down, and where the spark was when the frame began.
// Round spreads the speeds like a ball seen from the side, thickest at the rim; ring gives every spark nearly the top speed;
// willow sparks start slower, droop more and last longer. box is the patch of sky the burst has lit, its flash included.
// In Compare's Hangs in place (FALL off) the sparks have no gravity, and hang marks them to keep their glow (see paint).
function burst(x,y,sx){
const c=PALETTES[PAL][Math.floor(rnd()*PALETTES[PAL].length)],willow=SHAPE==='willow';
const n=phone.matches?50:90,top=(willow?2.8:4.2)*U,P=new Float32Array(n*6),m=60*U;
for(let i=0;i<n;i++){const a=rnd()*6.283,u=rnd()*2-1,v=top*(SHAPE==='ring'?.93+rnd()*.07:.08+.92*Math.sqrt(1-u*u));
P.set([x,y,Math.cos(a)*v,Math.sin(a)*v,x,y],i*6);}
bursts.push({P,n,c,x,y,sx,box:[x-m,y-m,x+m,y+m],age:0,life:willow?125:70+rnd()*20,drag:willow?.972:.955,g:FALL?(willow?.05:.032)*U:0,hang:!FALL,flash:true});
}
const column=(sx,x,y)=>[Math.min(sx,x)-4,y-4,Math.max(sx,x)+4,H]; // the path a rocket has drawn
// The parts of rectangle a that none of the rectangles in list covers, each of those taken 4 px wider on every side
function free(a,list){
return list.reduce((parts,r)=>{const q=[r[0]-4,r[1]-4,r[2]+4,r[3]+4];return parts.flatMap(p=>{
if(!(q[0]<p[2]&&q[2]>p[0]&&q[1]<p[3]&&q[3]>p[1]))return [p];
const y0=Math.max(p[1],q[1]),y1=Math.min(p[3],q[3]); // above, below, left and right of q
return [q[1]>p[1]&&[p[0],p[1],p[2],q[1]],q[3]<p[3]&&[p[0],q[3],p[2],p[3]],q[0]>p[0]&&[p[0],y0,q[0],y1],q[2]<p[2]&&[q[2],y0,p[2],y1]].filter(Boolean);
});},[a]);
}
// An 8-bit canvas cannot fade a faint pixel to nothing (each cover rounds it back up), so every burst would leave a dim ghost.
// When a burst dies, its patch and its rocket's path wait half a second while their last embers fade, then are wiped (4 px
// wider, on whole device pixels) except where something may still glow: a rocket, a burst, the path of a rocket that burst
// in the last half second, or a patch still waiting. Those parts are wiped later with the patch of what glows there.
function wipe(k){
for(const m of marks)m.wait-=k;
const out=v=>Math.floor(v*D)/D,up=v=>Math.ceil(v*D)/D,busy=[...rockets.map(q=>column(q.sx,q.x,q.y)),
...bursts.flatMap(b=>b.age<30?[b.box,column(b.sx,b.x,b.y)]:[b.box]),...marks.filter(m=>m.wait>0).map(m=>m.r)];
for(const m of marks)if(m.wait<=0)for(const p of free([m.r[0]-4,m.r[1]-4,m.r[2]+4,m.r[3]+4],busy)){
const x=out(p[0]),y=out(p[1]);ctx.clearRect(x,y,up(p[2])-x,up(p[3])-y);}
marks=marks.filter(m=>m.wait>0);
}
// Where everything is when a frame begins: the frame draws every rocket and spark from there to where it ends up
function keep(){
for(const r of rockets){r.px=r.x;r.py=r.y;}for(const b of bursts)for(let i=0;i<b.n*6;i+=6){b.P[i+4]=b.P[i];b.P[i+5]=b.P[i+1];}
}
// k is how many 60 Hz frames this step stands for, at most one (the loop splits longer ones): speeds fade by drag to the
// power k and gravity adds k times its pull. A burst that has died leaves its patch to wipe half a second later. A Compare
// half (HW half of W) keeps half the sparks in the air.
function step(k,launching){
if(launching&&(wait-=k)<=0){if(bursts.reduce((s,b)=>s+b.n,0)<(phone.matches?300:900)*HW/W)launch();wait=EVERY[OFTEN]*(.5+rnd());}
for(const r of rockets){r.vy+=r.g*k;r.x+=r.vx*k;r.y+=r.vy*k;if(r.vy>=0){burst(r.x,r.y,r.sx);r.done=true;}}
rockets=rockets.filter(r=>!r.done);
for(const b of bursts){
const d=Math.pow(b.drag,k),P=b.P,B=b.box;b.age+=k;
for(let i=0;i<b.n*6;i+=6){
P[i+2]*=d;P[i+3]=P[i+3]*d+b.g*k;
const x=P[i]+=P[i+2]*k,y=P[i+1]+=P[i+3]*k;
if(x<B[0])B[0]=x;else if(x>B[2])B[2]=x;if(y<B[1])B[1]=y;else if(y>B[3])B[3]=y;
}
}
for(const b of bursts)if(b.age>=b.life)marks.push({r:b.box,wait:30},{r:column(b.sx,b.x,b.y),wait:30});
bursts=bursts.filter(b=>b.age<b.life);
wipe(k);
}
// nf covers wear the old picture down toward see-through (with no trails one cover takes it all), then everything is drawn
// added on top, so sparks that cross glow brighter. A burst is one stroke: its sparks share a color, start white-hot, then
// fade and crackle as it ages; on its first frame it also lights a soft glow, which the covers fade with the trails. A
// hanging burst (Compare) neither fades nor crackles: it goes out when its time is up, and the covers take its picture away.
function paint(nf){
ctx.globalCompositeOperation='destination-out';ctx.fillStyle='rgba(0,0,0,'+WASH[TRAIL]+')';
for(let i=0;i<nf;i++)ctx.fillRect(0,0,W,H);
ctx.globalCompositeOperation='lighter';ctx.lineCap='round';
ctx.lineWidth=2;ctx.strokeStyle='rgba(255,226,180,.9)';ctx.beginPath();
for(const r of rockets){ctx.moveTo(r.px,r.py);ctx.lineTo(r.x,r.y);}
ctx.stroke();ctx.lineWidth=1.7;
for(const b of bursts){
const t=b.age/b.life,[h,s,l]=b.c,P=b.P,hsl='hsla('+h+','+s+'%,';
if(b.flash){
const r=60*U,g=ctx.createRadialGradient(b.x,b.y,0,b.x,b.y,r);b.flash=false;
g.addColorStop(0,hsl+l+'%,.3)');g.addColorStop(1,hsl+l+'%,0)');ctx.fillStyle=g;ctx.fillRect(b.x-r,b.y-r,2*r,2*r);
}
ctx.strokeStyle=hsl+(t<.1?90:l)+'%,'+(b.hang?1:Math.min(1,(1-t)*1.3))+')';ctx.beginPath();
for(let i=0;i<b.n*6;i+=6){if(t>.55&&!b.hang&&rnd()<.35)continue;ctx.moveTo(P[i+4],P[i+5]);ctx.lineTo(P[i],P[i+1]);}
ctx.stroke();
}
ctx.globalCompositeOperation='source-over';
}
// The still picture: at the start, after a resize and when a setting changes while paused. One, two or three bursts (as
// How often asks) at set places and ages are played forward a 60th of a second at a time, so their trails build up as live.
function paintStill(older=0){
rockets=[];bursts=[];marks=[];ctx.clearRect(0,0,W,H);
const shots=STILL.slice(0,{rarely:1,sometimes:2,often:3}[OFTEN]),T=older+Math.max(...shots.map(s=>s[2]));
for(let t=0;t<=T;t++){for(const [x,y,age] of shots)if(T-t===age+older)burst(X0+HW*x,H*y,X0+HW*x);keep();step(1,false);paint(1);}
wait=EVERY[OFTEN]*.5;
}
// The still picture for the mode on show. Compare's is the same plan in both halves from one fixed seed, so it is always the
// same picture; paused, each burst is 16 sixtieths older, when one half's sparks hang still and the other's have drooped and
// dimmed (playing, the bursts get there within a second).
function still(){if(mode==='compare'){seed=SEED;each(()=>paintStill(paused?16:0));}else paintStill();peek();}
// Compare: runs fn once for each half's world, clipped to that half (drawing, covers and wipes), with that world's own rockets,
// bursts and wipes. Both start from the same random series and launch timer, so the same rockets rise in both, at the same
// share of each half, and burst into the same sparks.
function each(fn){
const s=seed,w=wait;HW=W/2;rnd=rand;
worlds.forEach((v,i)=>{({rockets=[],bursts=[],marks=[]}=v);X0=i*HW;FALL=v.fall;seed=s;wait=w;
ctx.save();ctx.beginPath();ctx.rect(X0,0,HW,H);ctx.clip();fn();ctx.restore();Object.assign(v,{rockets,bursts,marks});});
X0=0;HW=W;FALL=true;rnd=Math.random;
}
// See inside, on its own canvas over the fireworks (their trails stay on theirs): each rocket's climb as a dashed curve from where
// it left to where its own gravity stops it (worked out from its speed now), crossed there, and each burst's climb the same; on
// each burst, eight of its sparks with an arrow for their speed now (where 12 more frames would take them), a gold arrow as long
// as its pull beside its middle, and a bar for the glow left. Drawn after every frame, and at once on a mode or setting change.
function peek(){
const o=octx,climb=new Path2D(),cross=new Path2D(),fly=new Path2D(),pull=new Path2D(),bars=[];
o.setTransform(1,0,0,1,0,0);o.clearRect(0,0,over.width,over.height);
if(mode!=='inside')return;
if(over.width!==cvs.width||over.height!==cvs.height){over.width=cvs.width;over.height=cvs.height;} // sized when first shown
o.setTransform(D,0,0,D,0,0);o.lineCap=o.lineJoin='round';
const arrow=(p,x,y,dx,dy)=>{const l=Math.hypot(dx,dy),h=Math.min(6,l/2),a=Math.atan2(dy,dx);if(l<1)return;
p.moveTo(x,y);p.lineTo(x+=dx,y+=dy);p.moveTo(x-h*Math.cos(a-.5),y-h*Math.sin(a-.5));p.lineTo(x,y);p.lineTo(x-h*Math.cos(a+.5),y-h*Math.sin(a+.5));};
const up=(sx,x,y)=>{climb.moveTo(sx,H);climb.quadraticCurveTo((sx+x)/2,y,x,y);cross.moveTo(x-4,y-4);cross.lineTo(x+4,y+4);cross.moveTo(x+4,y-4);cross.lineTo(x-4,y+4);};
for(const r of rockets)up(r.sx,r.x-r.vx*r.vy/r.g,r.y-r.vy*(r.vy+r.g)/(2*r.g)); // where its steps of a 60th end
for(const b of bursts){
const P=b.P,N=b.n*6;let cx=0,cy=0,mx=0,my=0;
for(let i=0;i<N;i+=6){cx+=P[i];cy+=P[i+1];mx+=P[i+2];my+=P[i+3];}
cx/=b.n;cy/=b.n;mx/=b.n;my/=b.n;
// picked once: the sparks nearest eight even directions, measured from the burst's shared drift, so they keep their places
b.pick=b.pick||[0,1,2,3,4,5,6,7].map(j=>{let best=-2,at=0;for(let i=0;i<N;i+=6){const c=Math.cos(Math.atan2(P[i+3]-my,P[i+2]-mx)-j*Math.PI/4);if(c>best){best=c;at=i;}}return at;});
up(b.sx,b.x,b.y);
for(const i of b.pick)arrow(fly,P[i],P[i+1],P[i+2]*12,P[i+3]*12);
arrow(pull,cx+9,cy,0,b.g*800);
bars.push([cx-14,cy-11,Math.min(1,(1-b.age/b.life)*1.3)]); // the glow the strokes are drawn with
}
for(const halo of [1,0])for(const [p,c,w,d] of [[climb,'#fff',1.5,[5,5]],[cross,'#fff',1.5,[]],[fly,'#fff',1.5,[]],[pull,'#ffce5a',2.5,[]]]){
o.setLineDash(d);o.lineWidth=w+3*halo;o.strokeStyle=halo?'rgba(2,3,10,.6)':c;o.stroke(p);}
o.setLineDash([]);o.lineWidth=1;o.strokeStyle='#fff';
for(const [x,y,f] of bars){o.fillStyle='rgba(2,3,10,.8)';o.fillRect(x-1.5,y-1.5,8,25);o.fillStyle='#fff';o.fillRect(x,y+22*(1-f),5,22*f);o.strokeRect(x+.5,y+.5,4,21);}
}
// loop() keeps one frame requested in raf. A drawn frame stands for the time since the last drawn one (n, at most 50 ms) and
// covers once per whole 60th, fa carrying the rest; with no cover due nothing is drawn and the time carries on, which also
// caps drawing near 60 a second. The sparks move by n, a third of it in slow motion (the covers do not slow), in equal
// steps of at most one 60th, so drag and gravity end where a 60 Hz screen would have them.
function loop(ts){
raf=requestAnimationFrame(loop);
if(lastT===null){lastT=ts;return;}
const n=Math.min(ts-lastT,50)/FRAME,w=Math.max(1,Math.round(n));
const f=fa+(Math.abs(n-w)<.05?w:n),nf=Math.round(f+.05); // within 5% of a whole number counts as it; .05 keeps a tie off the edge
if(!nf)return;fa=f-nf;lastT=ts;
const k=n*(slowTog.checked?1/3:1),parts=Math.ceil(k-.05);
const run=()=>{keep();for(let i=0;i<parts;i++)step(k/parts,true);paint(nf);};
if(mode==='compare')each(run);else run();
if(mode==='inside')peek();
}
function start(){if(raf)return;lastT=null;fa=0;raf=requestAnimationFrame(loop);}
// Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the loop on
// arrival, which this listener must catch, and the still picture of bursts stays on screen.
document.addEventListener('hb:pause',e=>{paused=e.detail.paused;if(paused){cancelAnimationFrame(raf);raf=null;}else start();});
// A tab that comes back after being hidden has no earlier frame to measure from, so its first frame adds no step
document.addEventListener('visibilitychange',()=>{lastT=null;fa=0;});
// The mode switch draws the mode at once, playing or paused. Compare shelves Normal's picture and keeps all its state (rockets,
// bursts, wipes, launch timer), then draws the still plan in both halves, from which they play on; leaving it puts Normal back as
// it was (or draws its still, after a setting or size change). See inside draws over Normal as it is.
document.addEventListener('hb:mode',e=>{
const was=mode;mode=e.detail.mode;
if(mode==='compare'&&was!=='compare'){
shelf.width=cvs.width;shelf.height=cvs.height;sctx.drawImage(cvs,0,0);saved={rockets,bursts,marks,wait};still();return;
}
if(was==='compare'&&mode!=='compare'){
if(saved){ctx.save();ctx.setTransform(1,0,0,1,0,0);ctx.clearRect(0,0,cvs.width,cvs.height);ctx.drawImage(shelf,0,0);ctx.restore();({rockets,bursts,marks,wait}=saved);}
else paintStill();
saved=null;
}
peek();
});
// Sizing the canvas wipes it, so a real resize draws the still picture at once; one that leaves the stage as it was does nothing
function resize(){
const w=stage.clientWidth,h=stage.clientHeight;if(w===W&&h===H)return;
W=HW=w;H=h;U=H/400;D=Math.min(window.devicePixelRatio||1,2);
cvs.width=Math.round(W*D);cvs.height=Math.round(H*D);ctx.setTransform(D,0,0,D,0,0);
saved=null;still();
}
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');
buttons.forEach(b=>b.addEventListener('click',()=>{
if(b.classList.contains('on'))return; // pressing the choice already made does nothing
buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
pick(b.dataset);
}));
}
// Paused, a change draws a new still picture, so it shows at once. Playing, trails change at once, How often from the next
// launch, and a new color or shape sends the next rocket up within a fifth of a second.
function changed(soon){saved=null;if(paused)still();else if(soon)wait=Math.min(wait,12);} // a shelved Normal would be out of date
choices('often-seg',d=>{OFTEN=d.often;wait=Math.min(wait,EVERY[OFTEN]);changed(false);});
choices('pal-seg',d=>{PAL=d.pal;changed(true);});
choices('trail-seg',d=>{TRAIL=d.trail;changed(false);});
choices('shape-seg',d=>{SHAPE=d.shape;changed(true);});
resize();
window.addEventListener('resize',resize);
start();
</script>
</body>
</html>