Skip to content

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.

24.1 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Synthwave Grid — Animation Handbook</title>
  <meta name="description" content="A glowing grid rolls toward you under a striped sun. Best for music and games.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/synthwave-grid/">
  <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="Synthwave Grid — Animation Handbook">
  <meta property="og:description" content="A glowing grid rolls toward you under a striped sun. Best for music and games.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/synthwave-grid/">
  <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="Synthwave Grid — Animation Handbook">
  <meta name="twitter:description" content="A glowing grid rolls toward you under a striped sun. Best for music and games.">
  <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": "Synthwave Grid", "name": "Synthwave Grid", "description": "A glowing grid rolls toward you under a striped sun. Best for music and games.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/synthwave-grid/", "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, Synthwave Grid"}</script>
  <style>
    :root{--ui-accent:#ffce5a}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow:hidden;background:#0a0316}
    canvas{display:block;width:100%;height:100%}
    /* See inside and Compare: plain words over the picture in dark pills. The script places the three captions (top and left
       from what it measures); the two half names sit at the top of their halves */
    .lbl{display:none;position:absolute;left:0;top:0;padding:4px 9px;border-radius:14px;background:rgba(5,1,10,.9);color:#f4f4f2;font:600 13px/16px var(--hb-font)}
    .cap{max-width:calc(100% - 20px)}
    .side{top:10px;white-space:nowrap}
    .side.first{left:10px}
    .side.second{left:calc(50% + 10px)}
    .stage[data-hb-mode="inside"] .cap,.stage[data-hb-mode="compare"] .side{display:block}
    @media(max-width:900px){.cap.b,.cap.c{max-width:calc(50% - 24px)}}
    @media(max-width:600px){.lbl{font-size:12px;line-height:15px;padding:3px 8px}}
    @media(max-width:360px){.side.second{left:calc(50% + 6px)}}
  </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="../flow-field/" rel="prev" aria-label="Previous: Flow Field"><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>Flow Field</span></a><a href="../matrix-rain/" rel="next" aria-label="Next: Matrix Rain"><span><span class="hb-dir">Next: </span>Matrix Rain</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.15 · Ambient &amp; Background</p>
    <h1>Synthwave Grid</h1>
    <p class="hb-lede">A glowing grid rolls toward you under a striped sun. Best for music and games.</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>
      <p class="lbl cap a" aria-hidden="true" data-hb-caption>All the lines meet at one point on the horizon</p>
      <p class="lbl cap b" aria-hidden="true" data-hb-caption>The white row rolls toward you, then starts again far away</p>
      <p class="lbl cap c" aria-hidden="true" data-hb-caption>Rows bunch up far away and spread out near you</p>
      <p class="lbl side first" aria-hidden="true" data-hb-caption>Flat grid</p>
      <p class="lbl side second" aria-hidden="true" data-hb-caption>In perspective</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, every floor line meets at one point on the horizon, and the rows bunch up far away and spread out near you. The white row rolls toward you, then starts again far away.</p>
      <p data-hb-for="compare" hidden>Flat grid keeps its rows evenly spaced and its lines parallel, so it looks like a sheet of paper. In perspective, the lines meet on the horizon and the rows bunch up far away.</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="speed-lbl">Speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-spd="0.6" aria-pressed="false">Slow</button>
          <button type="button" data-spd="1" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-spd="1.6" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How fast the floor rolls toward you.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="lines-lbl">Number of lines</p>
        <div class="seg" id="lines-seg" role="group" aria-labelledby="lines-lbl">
          <button type="button" data-n="10" aria-pressed="false">Few</button>
          <button type="button" data-n="16" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-n="24" aria-pressed="false">Many</button>
        </div>
        <p class="hb-hint">More lines make a finer grid.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="color-lbl">Grid color</p>
        <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
          <button type="button" class="on" aria-pressed="true" aria-label="Pink" data-col="#ff2fb0" style="background:#ff2fb0"></button>
          <button type="button" aria-pressed="false" aria-label="Cyan" data-col="#2de2e6" style="background:#2de2e6"></button>
          <button type="button" aria-pressed="false" aria-label="Purple" data-col="#b967ff" style="background:#b967ff"></button>
          <button type="button" aria-pressed="false" aria-label="Orange" data-col="#ff8a3d" style="background:#ff8a3d"></button>
        </div>
        <p class="hb-hint">The color of the glowing lines.</p>
      </div>
    </div>
  </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 a synthwave grid to [the section you want a retro scene in]. Draw a purple sky with a striped, glowing sun on the horizon and, below it, a glowing grid floor in perspective: lines that meet at one point on the horizon, crossed by lines that bunch up near the horizon and spread out toward the viewer. Move the cross lines steadily toward the viewer and wrap them around, fading each one in at the horizon, so the floor rolls forward forever with no visible jump. If the visitor has reduced motion turned on, show the scene still. 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>Music and gaming sites</li><li>Retro themes</li><li>Event pages</li><li>Hero sections</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');
  const slowTog=document.getElementById('slow-tog');
  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
  let W=0,H=0,raf=null,lastT=null,offset=0;
  let SPD=1,DENS=16,GLOW='#ff2fb0';
  // See inside and Compare: mode is what the box shows; fi is the row See inside follows; paused is whether the loop is stopped
  let mode='normal',fi=0,paused=false;

  function horizon(){return H*0.42;}
  // How far down the floor a row is, 0 at the horizon and 1 at the viewer (Compare's flat grid spaces its rows evenly instead)
  function rowY(p,flat){return horizon()+(flat?p:Math.pow(p,2.2))*(H-horizon());}

  // w: the width of the scene, the whole box except in Compare, where each half is a scene of its own
  function drawSky(w=W){
    const hz=horizon();
    const sky=ctx.createLinearGradient(0,0,0,hz);
    sky.addColorStop(0,'#1a0b2e');sky.addColorStop(.55,'#3d1152');sky.addColorStop(1,'#8a2160');
    ctx.fillStyle=sky;ctx.fillRect(0,0,w,hz);
    // glowing sun striped with seven gaps in its lower half, wider toward the horizon
    const cx=w/2,cy=hz-6,rad=Math.min(w,H)*0.18;
    const sun=ctx.createLinearGradient(0,cy-rad,0,cy+rad);
    sun.addColorStop(0,'#ffe15a');sun.addColorStop(.5,'#ff8a3d');sun.addColorStop(1,'#ff2fb0');
    ctx.save();
    ctx.beginPath();ctx.arc(cx,cy,rad,Math.PI,0);ctx.closePath();ctx.clip();
    ctx.fillStyle=sun;ctx.fillRect(cx-rad,cy-rad,rad*2,rad*2);
    ctx.fillStyle='#1a0b2e';
    for(let i=0;i<7;i++)ctx.fillRect(cx-rad,cy-rad*.55+i*rad*.08,rad*2,rad*(.015+i*.009));
    ctx.restore();
    // floor
    const flr=ctx.createLinearGradient(0,hz,0,H);
    flr.addColorStop(0,'#160726');flr.addColorStop(1,'#05010a');
    ctx.fillStyle=flr;ctx.fillRect(0,hz,w,H-hz);
  }

  // flat: Compare's first half, where the lines stand straight up and the rows are evenly spaced
  function drawGrid(w=W,flat=false){
    const hz=horizon(),cx=w/2;
    ctx.strokeStyle=GLOW;ctx.shadowColor=GLOW;ctx.shadowBlur=8;ctx.lineWidth=1.4;
    // lines toward the horizon converge to the vanishing point
    const cols=Math.round(DENS*0.7);
    for(let i=-cols;i<=cols;i++){
      const fx=cx+(i/cols)*w*1.4;
      if(flat&&Math.abs(fx-cx)>w/2+2)continue;
      ctx.beginPath();ctx.moveTo(flat?fx:cx,hz);ctx.lineTo(fx,H);ctx.stroke();
    }
    // lines across: perspective spacing, scrolled by modulo offset
    for(let i=0;i<DENS;i++){
      const p=((i+offset)%DENS)/DENS; // 0 at horizon -> 1 at viewer, loops seamlessly
      const y=rowY(p,flat);
      ctx.globalAlpha=Math.min(1,p*1.6);
      ctx.beginPath();ctx.moveTo(0,y);ctx.lineTo(w,y);ctx.stroke();
    }
    ctx.globalAlpha=1;ctx.shadowBlur=0;
  }

  // The whole picture at the current offset. It paints every pixel, so nothing of an earlier picture stays.
  function frame(){
    if(mode==='compare'){drawCompare();return;}
    drawSky();drawGrid();
    if(mode==='inside')drawMarks();
  }

  const HALVES=[true,false],DASH=[7,5],SOLID=[]; // made once: the halves, and the dashes and the solid line of See inside
  // Compare: the scene twice at half the width, from the same offset, each half drawn only inside its own clip (so the glow's
  // blur is not doubled): the flat grid on the left, the page's floor on the right
  function drawCompare(){
    const half=W/2;
    for(const flat of HALVES){
      ctx.save();ctx.beginPath();ctx.rect(flat?0:half,0,half,H);ctx.clip();
      ctx.translate(flat?0:half,0);
      drawSky(half);drawGrid(half,flat);
      ctx.restore();
    }
    ctx.fillStyle='rgba(244,244,242,.7)';ctx.fillRect(half-1,0,2,H);
  }

  // See inside, drawn over the picture with the glow off: a ring where the lines meet and the horizon dashed across; one row
  // followed in white (the row fi) with an arrow as long as Speed; and a bracket on the right edge for every other gap between
  // neighboring rows, short far away and long near you. The row has the page's own alpha, so it fades in at the horizon
  function drawMarks(){
    const hz=horizon(),cx=W/2,pf=((fi+offset)%DENS)/DENS,yf=rowY(pf);
    const len=12+22*SPD,y0=Math.max(hz+4,Math.min(yf-len/2,H-2-len)),ax=cx;
    ctx.save();ctx.lineCap='round';ctx.lineJoin='round';
    ctx.strokeStyle='rgba(255,255,255,.9)';ctx.lineWidth=1.5;ctx.setLineDash(DASH);
    ctx.beginPath();ctx.moveTo(0,hz);ctx.lineTo(W,hz);ctx.stroke();ctx.setLineDash(SOLID);
    ctx.beginPath();ctx.arc(cx,hz,8,0,Math.PI*2);
    ctx.strokeStyle='rgba(5,1,10,.6)';ctx.lineWidth=5;ctx.stroke();ctx.strokeStyle='#fff';ctx.lineWidth=2;ctx.stroke();
    ctx.globalAlpha=Math.min(1,pf*1.6);
    ctx.beginPath();ctx.moveTo(0,yf);ctx.lineTo(W,yf);ctx.stroke();ctx.globalAlpha=1;
    ctx.beginPath();ctx.moveTo(ax,y0);ctx.lineTo(ax,y0+len);ctx.moveTo(ax-6,y0+len-7);ctx.lineTo(ax,y0+len);ctx.lineTo(ax+6,y0+len-7);
    ctx.strokeStyle='rgba(5,1,10,.55)';ctx.lineWidth=5.5;ctx.stroke();ctx.strokeStyle='#fff';ctx.lineWidth=2.5;ctx.stroke();
    ctx.beginPath();
    for(let i=0;i<DENS;i+=2){
      const a=((i+offset)%DENS)/DENS,b=((i+1+offset)%DENS)/DENS;
      if(b<=a)continue; // the nearer row has wrapped round to the horizon: its bracket has left the floor
      const t=rowY(a),u=rowY(b);
      if(u-t>=2.5){ctx.moveTo(W-15,t);ctx.lineTo(W-9,t);ctx.lineTo(W-9,u);ctx.lineTo(W-15,u);}
    }
    ctx.strokeStyle='rgba(5,1,10,.55)';ctx.lineWidth=4;ctx.stroke();ctx.strokeStyle='#fff';ctx.lineWidth=1.5;ctx.stroke();
    ctx.restore();
    dodge(yf,y0,y0+len,ax);
  }
  // Pick the row See inside follows: the one about three fifths of the way down the floor, so a paused picture shows it mid-floor
  function pose(){
    let best=1;
    for(let i=0;i<DENS;i++){const d=Math.abs(((i+offset)%DENS)/DENS-.6);if(d<best){best=d;fi=i;}}
  }

  // See inside's three captions: the first over the sky, the second and third on the floor. Placed from their measured sizes (when
  // the mode, a setting, the size or the font changes, never per frame), each in the first spot that meets no other caption, the sun,
  // the ring, the brackets, the followed row or its arrow. A caption on the floor steps aside while the row or its arrow passes it.
  const caps=[...stage.querySelectorAll('.cap')],spots=[],gone=[0,0,0];
  function place(){
    if(mode!=='inside')return;
    const M=10,R=26,hz=horizon(),rad=Math.min(W,H)*.18,yf=rowY(((fi+offset)%DENS)/DENS),len=12+22*SPD;
    const y0=Math.max(hz+4,Math.min(yf-len/2,H-2-len));
    const busy=[[W/2-rad-6,hz-rad-12,W/2+rad+6,hz+16],[W-20,hz,W,H],[0,yf-8,W,yf+8],[W/2-12,y0-6,W/2+12,y0+len+6]];
    const meets=(c,w,h,set)=>set.some(o=>c[0]<o[2]+5&&o[0]<c[0]+w+5&&c[1]<o[3]+5&&o[1]<c[1]+h+5);
    const given=[];
    const list=[
      (w,h)=>[[M,M],[W-M-w,M]],
      (w,h)=>[[M,H-M-h],[W-R-w,H-M-h]],
      (w,h)=>[[W-R-w,H-M-h],[W-R-w,(given[1]?given[1][1]:H)-6-h],[M,(given[1]?given[1][1]:H)-6-h],[W-M-w,M]]
    ];
    // each is measured from the left edge, where it has the whole width: at its old spot after a resize it would wrap narrower
    caps.forEach(el=>{el.style.left=el.style.top='0px';});
    caps.forEach((el,i)=>{
      const w=el.offsetWidth,h=el.offsetHeight,all=list[i](w,h).filter(c=>c[0]>=M&&c[1]>=M&&c[0]+w<=W-M&&c[1]+h<=H-M);
      const c=all.find(c=>!meets(c,w,h,busy)&&!meets(c,w,h,given))||all.find(c=>!meets(c,w,h,given))||all[0]||[M,M];
      el.style.left=c[0]+'px';el.style.top=c[1]+'px';el.style.visibility='';gone[i]=0;
      given[i]=spots[i]=[c[0],c[1],c[0]+w,c[1]+h];
    });
  }
  // The two floor captions step aside while the row or its arrow is near them (6px) and return when it is 16px away
  function dodge(yf,a0,a1,ax){
    for(let i=1;i<3;i++){
      const s=spots[i];if(!s)continue;
      const m=gone[i]?16:6,hit=(yf+2+m>s[1]&&yf-2-m<s[3])||(ax+8+m>s[0]&&ax-8-m<s[2]&&a1+m>s[1]&&a0-m<s[3]);
      if(hit!==!!gone[i]){gone[i]=hit?1:0;caps[i].style.visibility=hit?'hidden':'';}
    }
  }

  // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per
  // 16 ms. A step is Speed x 0.02, or a third of that in slow motion, for every 60th of a second since the last frame, never
  // more than 50 ms (a background tab does not leap); the first frame after a start has nothing to measure from and adds none.
  function loop(ts){
    raf=requestAnimationFrame(loop);
    if(lastT!==null&&ts-lastT<16)return;
    const dt=lastT===null?0:Math.min(ts-lastT,50);
    lastT=ts;
    offset+=SPD*0.02*dt/FRAME*(slowTog.checked?1/3:1);
    frame();
  }
  function start(){if(raf)return;lastT=null;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. The picture on screen stays as it is while paused.
  document.addEventListener('hb:pause',e=>{
    paused=e.detail.paused;
    if(paused){cancelAnimationFrame(raf);raf=null;}
    else start();
    notes();
  });
  // The mode switch draws the mode at once, playing or paused. The picture comes from the offset alone, so Normal comes back as it was.
  document.addEventListener('hb:mode',e=>{
    mode=e.detail.mode;
    if(mode==='inside'){pose();place();}
    // the horizon's row of pixels is only partly covered by the sky and the floor, so it keeps a little of the last picture:
    // while the loop is stopped, a few more drawings settle it, so Normal comes back exactly
    for(let k=raf?1:5;k>0;k--)frame();
    notes();
  });
  // See inside's note speaks of the white row rolling; a still (reduced motion, paused) says what the arrow shows instead, and the
  // words are written only when they change, so the live region is not read again at every Pause and Play
  const noteIn=document.querySelector('[data-hb-for="inside"]'),reduced=matchMedia('(prefers-reduced-motion: reduce)');
  const NOTE_IN=noteIn.textContent,NOTE_STILL='Inside, every floor line meets at one point on the horizon, and the rows bunch up far away and spread out near you. The arrow shows which way the white row would roll.';
  function notes(){const t=reduced.matches&&paused?NOTE_STILL:NOTE_IN;if(noteIn.textContent!==t)noteIn.textContent=t;}
  reduced.addEventListener('change',notes);
  // 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;});

  // Setting the canvas size wipes it, so a real change of the stage's size draws the picture again at once,
  // playing or paused. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.
  function resize(){
    const w=stage.clientWidth,h=stage.clientHeight;
    if(w===W&&h===H)return;
    W=cvs.width=w;H=cvs.height=h;
    place();frame();
  }

  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);
    }));
  }
  // A setting that changes the picture draws it at once, so the change shows while the loop is paused (or is paused a
  // moment later, before the next frame). Speed changes no picture: the floor carries on from where it is.
  // In See inside Speed lengthens the arrow, so it draws at once too; Number of lines picks the followed row again
  choices('speed-seg',d=>{SPD=+d.spd;if(mode==='inside'){place();frame();}});
  choices('lines-seg',d=>{DENS=+d.n;if(mode==='inside'){pose();place();}frame();});
  choices('color-sw',d=>{GLOW=d.col;frame();});

  resize();
  window.addEventListener('resize',resize);
  document.fonts.ready.then(place);
  start();
</script>
</body>
</html>