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.4 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>Wave Layers — Animation Handbook</title>
  <meta name="description" content="Layers of waves drift sideways, each at its own speed. Best for hero sections.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/wave-layers/">
  <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="Wave Layers — Animation Handbook">
  <meta property="og:description" content="Layers of waves drift sideways, each at its own speed. Best for hero sections.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/wave-layers/">
  <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="Wave Layers — Animation Handbook">
  <meta name="twitter:description" content="Layers of waves drift sideways, each at its own speed. Best for hero sections.">
  <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": "Wave Layers", "name": "Wave Layers", "description": "Layers of waves drift sideways, each at its own speed. Best for hero sections.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/wave-layers/", "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, Wave Layers"}</script>
  <style>
    :root{--ui-accent:#ffce5a}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    /* A size container, so the waves are measured in cqh: hundredths of the stage's height */
    /* The sky glows lightest at the horizon and every layer is darker than that glow, the near ones darkest, so each
       layer stands out against the one behind it and against the sky, however many are shown. The sky is painted inside
       the stage's see-through border only (the waves stop there too), so all four edges show the same faint line as on
       other pages, not the sky's light bottom. */
    .stage{position:relative;overflow:hidden;container-type:size;--dur:24s;--amp:11cqh;
      --sky1:#040b16;--sky2:#3f8fb5;--c1:#1f6386;--c2:#164d6c;--c3:#0f3a54;--c4:#08263b;
      background:linear-gradient(var(--sky1) 20%,var(--sky2) 44%);background-clip:padding-box}
    .stage[data-pal="sunset"]{--sky1:#1a0b2e;--sky2:#f08a5d;--c1:#c04a5e;--c2:#8e2f5a;--c3:#5c1f4f;--c4:#2f1238}
    .stage[data-pal="night"]{--sky1:#03040b;--sky2:#43539c;--c1:#2a376f;--c2:#1f2856;--c3:#161c3e;--c4:#0d1129}
    .defs{position:absolute;width:0;height:0}
    .hero{position:absolute;top:clamp(14px,7cqh,36px);left:0;right:0;z-index:2;padding:0 16px;text-align:center}
    .hero-eyebrow{font-size:11px;font-weight:600;letter-spacing:2px;text-transform:uppercase;color:rgba(255,255,255,.72);margin-bottom:6px}
    .hero-title{font-size:clamp(16px,2.4vw,24px);font-weight:700;line-height:1.25;color:#fff;text-wrap:balance}
    /* A layer bobs gently up and down; its track, twice the stage's width, slides left by half its width and starts over.
       Both cycles and the head start (--o, a share of the cycle) follow --dur, so a new speed can keep every wave in place. */
    .wave{position:absolute;left:0;right:0;top:calc(var(--y) - var(--amp)*var(--k)/2);bottom:-12cqh;
      animation:bob calc(var(--dur)*var(--s)/4) ease-in-out calc(var(--dur)*var(--s)*var(--o)) infinite alternate}
    .track{display:flex;flex-direction:column;width:200%;height:100%;
      animation:drift calc(var(--dur)*var(--s)) linear calc(var(--dur)*var(--s)*var(--o)) infinite}
    .track svg{display:block;flex:none;width:100%;height:calc(var(--amp)*var(--k));fill:var(--fill)}
    .track i{flex:1;margin-top:-1px;background:var(--fill)}
    /* Far layers sit higher, with smaller waves and a slower cycle; near layers are lower, bigger and faster */
    .w1{--y:42%;--k:.5;--s:1.7;--o:-.15;--fill:var(--c1)}
    .w2{--y:54%;--k:.65;--s:1.35;--o:-.6;--fill:var(--c2)}
    .w3{--y:66%;--k:.8;--s:1.1;--o:-.35;--fill:var(--c3)}
    .w4{--y:80%;--k:1;--s:.9;--o:-.8;--fill:var(--c4)}
    @keyframes drift{to{transform:translateX(-50%)}}
    @keyframes bob{from{transform:translateY(-1.2cqh)}to{transform:translateY(1.2cqh)}}

    /* See inside and Compare. Everything they add is hidden in Normal, and the layers' own rules above are never touched. */
    .stage{--pace:1;--u:calc((50cqw - 26px)/1.905)}
    .twin,.ins,.cmp,.so,.mid{display:none}
    .stage[data-hb-mode="inside"] .hero,.stage[data-hb-mode="compare"] .hero{display:none}
    .twin{position:absolute;inset:0 50% 0 0;overflow:hidden}
    .ins,.cmp{position:absolute;inset:0;pointer-events:none}
    .cap{max-width:100%;padding:3px 7px;border-radius:5px;background:rgba(8,8,10,.9);color:#f4f4f2;font:600 13px/1.2 var(--hb-font)}
    /* An arrow points the way the layer slides and is as long as its pace: one over its cycle share, times the Speed setting */
    .ar{position:relative;display:block;flex:none;width:calc(var(--u)*var(--pace)/var(--s));height:10px}
    .ar::before{content:"";position:absolute;left:6px;right:0;top:3.5px;height:3px;border-radius:2px;background:#f4f4f2}
    .ar::after{content:"";position:absolute;left:0;top:0;border:solid transparent;border-width:5px 8px 5px 0;border-right-color:#f4f4f2}
    /* See inside: every layer is drawn at half size from its left edge (scale leaves its own movement running), so its strip,
       twice as wide as the box, fits the box's width. The script places the strips one under another, far at the top. */
    .stage[data-hb-mode="inside"] .wave{top:var(--st);bottom:auto;height:calc(var(--S)*2);scale:.5;transform-origin:0 0}
    .stage[data-hb-mode="inside"] .track{position:relative;outline:3px solid rgba(244,244,242,.6);outline-offset:-3px}
    .stage[data-hb-mode="inside"] .so,.stage[data-hb-mode="inside"] .mid{display:block;position:absolute;top:0;bottom:0}
    .stage[data-hb-mode="inside"] .so{left:0;width:200%;outline:3px dashed rgba(244,244,242,.5);outline-offset:-3px}
    .stage[data-hb-mode="inside"] .mid{left:50%;border-left:3px dashed #f4f4f2}
    .stage[data-hb-mode="inside"] .ins{display:block}
    .ins .veil,.ins .win{position:absolute}
    .ins .veil{left:50%;right:0;background:rgba(4,11,22,.5)}
    .ins .win{left:0;width:50%;border:2px dashed var(--ui-accent)}
    .ins .top,.ins .bot{position:absolute;left:6px;right:6px;display:flex;flex-wrap:wrap;align-items:flex-start;gap:4px 6px}
    .ins .top .cap:first-child{text-wrap:balance}
    .ins .bot .cap:first-child{max-width:calc(50% - 4px)}
    .ins .top .cap:last-child,.ins .bot .cap:last-child{margin-left:auto}
    .chip{position:absolute;right:6px;display:flex;align-items:center;height:16px;padding:0 6px;border-radius:6px;background:rgba(8,8,10,.9)}
    /* Compare: the first half is the copy of the layers, the second the page's own layers moved to the right half */
    .stage[data-hb-mode="compare"] .wave{left:50%;overflow:hidden}
    .stage[data-hb-mode="compare"] .twin{display:block}
    .stage[data-hb-mode="compare"] .twin .wave{--s:1.25;left:0}
    .stage[data-hb-mode="compare"] .cmp{display:grid;grid-template-columns:1fr 1fr}
    .cmp::after{content:"";position:absolute;left:50%;top:0;bottom:0;width:2px;margin-left:-1px;background:rgba(244,244,242,.55)}
    .half{display:flex;flex-direction:column;align-items:flex-start;gap:3px;min-width:0;padding:8px 6px}
    .half .cap{white-space:nowrap}
    .gauge{align-self:flex-start;display:flex;flex-direction:column;align-items:flex-end;gap:1px;padding:5px 7px;border-radius:6px;background:rgba(8,8,10,.9)}
    .gauge.same .ar{--s:1.25}
    .ins [hidden],.cmp [hidden]{display:none}
  </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="../plasma/" rel="prev" aria-label="Previous: Plasma 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>Plasma Field</span></a><a href="../snow-rain/" rel="next" aria-label="Next: Snow / Rain"><span><span class="hb-dir">Next: </span>Snow / 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.18 · Ambient &amp; Background</p>
    <h1>Wave Layers</h1>
    <p class="hb-lede">Layers of waves drift sideways, each at its own speed. Best for hero sections.</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">
      <!-- One path of ten whole waves; each layer's viewBox shows the first ten, eight, six or four of them -->
      <svg class="defs" aria-hidden="true"><defs><path id="wv" d="M0 0C18 0 32 40 50 40S82 0 100 0S132 40 150 40S182 0 200 0S232 40 250 40S282 0 300 0S332 40 350 40S382 0 400 0S432 40 450 40S482 0 500 0S532 40 550 40S582 0 600 0S632 40 650 40S682 0 700 0S732 40 750 40S782 0 800 0S832 40 850 40S882 0 900 0S932 40 950 40S982 0 1000 0V40H0Z"/></defs></svg>
      <div class="hero">
        <p class="hero-eyebrow">Wave layers</p>
        <p class="hero-title">Near waves move faster than far ones</p>
      </div>
      <div class="wave w1" aria-hidden="true"><div class="track"><svg viewBox="0 0 1000 40" preserveAspectRatio="none"><use href="#wv"/></svg><i></i></div></div>
      <div class="wave w2" aria-hidden="true"><div class="track"><svg viewBox="0 0 800 40" preserveAspectRatio="none"><use href="#wv"/></svg><i></i></div></div>
      <div class="wave w3" aria-hidden="true"><div class="track"><svg viewBox="0 0 600 40" preserveAspectRatio="none"><use href="#wv"/></svg><i></i></div></div>
      <div class="wave w4" aria-hidden="true"><div class="track"><svg viewBox="0 0 400 40" preserveAspectRatio="none"><use href="#wv"/></svg><i></i></div></div>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-pause" data-hb-pause="css" 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="css" autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, each strip is shown whole at half size: twice as wide as the box, so when it starts over the box never sees a seam. Near strips slide faster than far ones.</p>
      <p data-hb-for="compare" hidden>Same pace slides every layer at one speed, so the water looks like one flat sheet. Near ones faster gives the near layers a quicker pace, so the water looks deep.</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-secs="40" aria-pressed="false">Slow</button>
          <button type="button" data-secs="24" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-secs="14" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">Slow feels like a calm sea.</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="ocean" class="on" aria-pressed="true">Ocean</button>
          <button type="button" data-pal="sunset" aria-pressed="false">Sunset</button>
          <button type="button" data-pal="night" aria-pressed="false">Night</button>
        </div>
        <p class="hb-hint">Far layers are the palest, near ones the darkest.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="amp-lbl">Wave height</p>
        <div class="seg" id="amp-seg" role="group" aria-labelledby="amp-lbl">
          <button type="button" data-amp="6cqh" aria-pressed="false">Low</button>
          <button type="button" data-amp="11cqh" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-amp="17cqh" aria-pressed="false">High</button>
        </div>
        <p class="hb-hint">High waves look like rough water.</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="count-lbl">Number of layers</p>
          <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">
            <button type="button" data-n="2" aria-pressed="false">2</button>
            <button type="button" data-n="3" aria-pressed="false">3</button>
            <button type="button" data-n="4" class="on" aria-pressed="true">4</button>
          </div>
          <p class="hb-hint">More layers give more depth; the far ones go first.</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 layered waves to [the bottom of the section you want them in]. Stack a few strips of water, each one twice as wide as the section with the same wave shape repeated along its top edge: far strips sit higher, paler and with smaller waves, near strips lower, darker and with bigger ones. Slide every strip sideways by half its width, over and over, so the repeated shape hides the restart, and give near strips a quicker pace than far ones so the water feels deep. Move the strips only by shifting them, which keeps it smooth on phones. If the visitor has reduced motion turned on, keep the waves 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>Hero sections</li><li>Page footers</li><li>Section dividers</li><li>Travel and beach sites</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Dense dashboards</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 stage=document.querySelector('.stage');
  const waves=[...document.querySelectorAll('.wave')];
  let secs=24,count=4,amp=11;

  // Under reduced motion the layers are held, so while they are held the notes say what the still picture shows; Play brings
  // back the moving words and Pause the still ones. A note is written only when its words change.
  const reduceMQ=matchMedia('(prefers-reduced-motion: reduce)');
  const notes=[...document.querySelectorAll('[data-hb-for]')],moving=notes.map(n=>n.textContent);
  const still=[
    "Inside, shown still: each strip appears whole at half size, twice as wide as the box. The arrows show each strip's pace, the near ones fastest.",
    "Shown still: the arrows show each layer's pace, all the same in Same pace and quicker toward the front in Near ones faster."];
  let paused=reduceMQ.matches;
  function describe(){notes.forEach((n,i)=>{const words=reduceMQ.matches&&paused?still[i]:moving[i];if(n.textContent!==words)n.textContent=words})}
  describe();
  document.addEventListener('hb:pause',e=>{paused=e.detail.paused;describe()});
  reduceMQ.addEventListener('change',describe);

  // Made once: Compare's first half is a copy of the layers (no ids, inert, hidden from screen readers), See inside adds to each
  // layer the outline where its strip starts (it bobs but never slides) and a line at the strip's middle (it rides the track).
  const mk=(tag,cls,html)=>{const e=document.createElement(tag);e.className=cls;if(html)e.innerHTML=html;return e};
  const cap=t=>`<p class="cap" data-hb-caption>${t}</p>`;
  const arrows='<i class="ar w1"></i><i class="ar w2"></i><i class="ar w3"></i><i class="ar w4"></i>';
  const twin=mk('div','twin');
  const copies=waves.map(w=>twin.appendChild(w.cloneNode(true)));
  waves.forEach(w=>{w.prepend(mk('b','so'));w.querySelector('.track').append(mk('b','mid'))});
  const ins=mk('div','ins','<b class="veil"></b><b class="win"></b>'+[1,2,3,4].map(n=>`<span class="chip w${n}"><i class="ar"></i></span>`).join('')
    +'<div class="top">'+cap('Each strip is twice as wide; it slides half its width, then starts over')+cap('Far: slowest')+'</div>'
    +'<div class="bot">'+cap('The box shows only this part')+cap('Near: fastest')+'</div>');
  const cmp=mk('div','cmp',`<div class="half">${cap('Same pace')}<div class="gauge same">${arrows}</div></div>`
    +`<div class="half">${cap('Near ones faster')}<div class="gauge">${arrows}</div></div>`);
  [twin,ins,cmp].forEach(e=>{e.setAttribute('aria-hidden','true');e.inert=true});
  stage.append(twin,ins,cmp);

  // Fewer layers hide the far ones first, so the near waves at the bottom always stay (the copy, the chips and the arrows too)
  function showLayers(){
    waves.forEach((w,i)=>{w.hidden=copies[i].hidden=i<4-count});
    [...ins.querySelectorAll('.chip'),...cmp.querySelectorAll('.ar')].forEach((e,k)=>{e.hidden=k%4<4-count});
  }

  // The copy's animations start when Compare shows it, so they are set to the originals' time: both halves run on one clock
  function syncCopy(){
    waves.forEach((w,i)=>[[w,copies[i]],[w.querySelector('.track'),copies[i].querySelector('.track')]].forEach(([a,b])=>{
      const x=a.getAnimations()[0],y=b.getAnimations()[0];
      if(x&&y&&x.currentTime!=null)y.currentTime=x.currentTime;
    }));
  }

  // See inside: the strips (each as tall as its biggest wave plus a little body) spread down the box, far at the top, with the
  // window over the left half, which is what the real box shows. The captions are measured, so the rows follow their wrapping.
  function layout(){
    if(stage.getAttribute('data-hb-mode')!=='inside')return;
    const H=stage.clientHeight,n=count,top=ins.querySelector('.top'),bot=ins.querySelector('.bot');
    const hT=top.offsetHeight,hB=bot.offsetHeight,pad=4,room=H-12-hT-hB-2*pad-8;
    const S=Math.min((amp/2+6)*H/100,(room-(n-1)*7)/n);
    const g=7+Math.min(13,Math.max(0,room-n*S-(n-1)*7)/(3*(n-1)));
    const G=n*S+(n-1)*g,y0=Math.max(6,(H-(hT+hB+G+2*pad+8))/2),yS=y0+hT+4+pad;
    const at=i=>yS+(i-(4-n))*(S+g);
    top.style.top=y0+'px';bot.style.top=yS+G+pad+4+'px';
    ['.veil','.win'].forEach(c=>{const e=ins.querySelector(c);e.style.top=yS-pad+'px';e.style.height=G+2*pad+'px'});
    stage.style.setProperty('--S',S+'px');
    ins.querySelectorAll('.chip').forEach((c,i)=>{c.style.top=at(i)+(S-16)/2+'px'});
    waves.forEach((w,i)=>w.style.setProperty('--st',at(i)+'px'));
  }
  document.addEventListener('hb:mode',e=>{if(e.detail.mode==='compare')syncCopy();layout()});
  new ResizeObserver(layout).observe(stage);
  if(document.fonts)document.fonts.ready.then(layout);

  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 new --dur keeps the time already played, so every layer would jump to another point of its cycle. Scaling each
  // animation's time by the same factor as its cycle (and its head start, which follows --dur) keeps it where it is.
  choices('speed-seg',d=>{
    const next=+d.secs;
    stage.getAnimations({subtree:true}).forEach(a=>{if(a.currentTime!=null)a.currentTime=a.currentTime*next/secs});
    secs=next;
    stage.style.setProperty('--dur',next+'s');
    stage.style.setProperty('--pace',24/next); // the arrows' length follows the speed, also while paused
  });
  choices('pal-seg',d=>{stage.dataset.pal=d.pal});
  choices('amp-seg',d=>{amp=parseFloat(d.amp);stage.style.setProperty('--amp',d.amp);layout()});
  choices('count-seg',d=>{count=+d.n;showLayers();layout()});
</script>
</body>
</html>