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.

32.8 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>Progress Animation — Animation Handbook</title>
  <meta name="description" content="A bar, a ring and steps fill up to show progress. Best for uploads.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/progress-animation/">
  <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="Progress Animation — Animation Handbook">
  <meta property="og:description" content="A bar, a ring and steps fill up to show progress. Best for uploads.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/progress-animation/">
  <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="Progress Animation — Animation Handbook">
  <meta name="twitter:description" content="A bar, a ring and steps fill up to show progress. Best for uploads.">
  <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": "Progress Animation", "name": "Progress Animation", "description": "A bar, a ring and steps fill up to show progress. Best for uploads.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/progress-animation/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Progress Animation"}</script>
  <style>
    :root{--ui-accent:#ff9d5c;--ui-border:rgba(255,255,255,.09);--ui-muted:#8a8a92;--prog-dur:2000ms;--prog-color:#58a6ff}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:24px;padding:24px;background:#0b0b0d}
    /* Bar: the fill is as wide as the track and slides in from the left; the track clips the part still outside */
    .prog-section{width:100%;max-width:320px}
    .prog-label{font-size:11px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-muted);display:flex;justify-content:space-between;margin-bottom:8px}
    .prog-track{height:8px;background:var(--ui-border);border-radius:4px;overflow:hidden}
    .prog-fill{height:100%;width:100%;background:var(--prog-color);border-radius:4px;transform:translateX(-100%)}
    .prog-fill.indet{width:40%;animation:indet var(--prog-dur) ease-in-out infinite}
    @keyframes indet{0%{transform:translateX(-150%)}100%{transform:translateX(350%)}}
    /* Ring */
    .prog-circle{position:relative;width:80px;height:80px;flex-shrink:0}
    .prog-circle svg{transform:rotate(-90deg)}
    .prog-ring{fill:none;stroke:var(--ui-border);stroke-width:6}
    .prog-arc{fill:none;stroke:var(--prog-color);stroke-width:6;stroke-linecap:round;stroke-dasharray:201;stroke-dashoffset:201}
    .prog-pct{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;font-size:12px;font-weight:bold}
    /* Steps */
    .steps{display:flex;gap:4px;align-items:center}
    .step{flex:1;height:6px;background:var(--ui-border);border-radius:3px;transition:background 200ms ease}
    .step.done{background:var(--prog-color)}
    /* See inside and Compare. The displays sit in .shows, which takes no part in the layout in Normal. See inside: them at the left
       (above the graph on phones, where the steps go) and a graph of how full it is over time. Compare: them twice, side by side. */
    .shows{display:contents}
    .graph,.twin,.side,.when,.cap-sum{display:none}
    .lbl{position:absolute;padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.88);color:#f4f4f2;font:600 13px/1.2 var(--hb-font);white-space:nowrap;text-wrap-style:balance}
    .twin .step{transition:none}
    .stage[data-hb-mode="inside"] .shows:not(.twin),.stage[data-hb-mode="compare"] .shows{display:flex;flex-direction:column;align-items:center;gap:14px;min-width:0}
    .stage[data-hb-mode="inside"] .prog-circle,.stage[data-hb-mode="compare"] .prog-circle{width:64px;height:64px}
    .stage[data-hb-mode="inside"] .prog-circle svg,.stage[data-hb-mode="compare"] .prog-circle svg{width:100%;height:100%}
    .stage[data-hb-mode="inside"]{display:grid;grid-template-columns:minmax(0,.85fr) minmax(0,1.6fr);grid-template-rows:minmax(0,1fr) auto;
      align-items:center;gap:8px clamp(12px,3vw,32px);padding:clamp(12px,2.5vw,24px)}
    .stage[data-hb-mode="inside"] .shows{grid-area:1/1}
    .stage[data-hb-mode="inside"] .graph{display:block;grid-area:1/2/3;position:relative;align-self:stretch;min-height:0}
    .stage[data-hb-mode="inside"] .cap-sum{display:block;position:static;grid-area:2/1;max-width:100%;white-space:normal;text-wrap:balance;text-align:center}
    .graph svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
    .graph path{fill:none;stroke-linecap:round;stroke-linejoin:round}
    .g-axis,.g-top{stroke:rgba(244,244,242,.5);stroke-width:1.5}
    .g-fifths{stroke:rgba(244,244,242,.14);stroke-width:1}
    .g-top{stroke-dasharray:5 5}
    .g-curve{stroke:var(--prog-color);stroke-width:3}
    .g-dot{fill:#f4f4f2;stroke:var(--prog-color);stroke-width:2}
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:1fr auto auto 1fr;
      align-items:center;gap:0 clamp(12px,3vw,32px);padding:44px clamp(8px,2vw,24px) 12px}
    .stage[data-hb-mode="compare"]::after{content:'';position:absolute;top:0;bottom:0;left:50%;border-left:1px solid rgba(244,244,242,.12)}
    .stage[data-hb-mode="compare"] .twin{grid-area:2/1}
    .stage[data-hb-mode="compare"] .shows:not(.twin){grid-area:2/2}
    .stage[data-hb-mode="compare"] .side,.stage[data-hb-mode="compare"] .when{display:block}
    .side{top:12px;left:clamp(8px,2vw,24px)}
    .side.second{left:calc(50% + clamp(8px,2vw,24px))}
    .when{position:static;grid-area:3/2;justify-self:center;margin-top:8px;visibility:hidden}
    .stage[data-hb-mode="compare"].posed .when{visibility:visible}
    .stage[data-hb-mode="compare"].posed .prog-fill.indet{animation-play-state:paused;animation-delay:calc(var(--prog-dur)*-.45)}
    @media(max-width:600px),(max-height:500px){
      .stage[data-hb-mode="inside"] .prog-section:last-child,.stage[data-hb-mode="compare"] .prog-section:last-child{display:none}
      .stage[data-hb-mode="inside"]{grid-template-columns:minmax(0,1fr);grid-template-rows:auto auto minmax(0,1fr);padding:12px}
      .stage[data-hb-mode="inside"] .shows:not(.twin){flex-direction:row;justify-content:center;gap:14px;width:100%}
      .stage[data-hb-mode="inside"] .graph{grid-area:3/1}
    }
    @media(prefers-reduced-motion:reduce){.prog-fill.indet{animation:none!important;width:100%!important;transform:none!important}}
  </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="once" data-hb-autoplay>
<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="../loading-spinner/" rel="prev" aria-label="Previous: Loading Spinner"><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>Loading Spinner</span></a><a href="../checkmark-draw/" rel="next" aria-label="Next: Checkmark Draw"><span><span class="hb-dir">Next: </span>Checkmark Draw</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">04.12 · Micro-Interactions</p>
    <h1>Progress Animation</h1>
    <p class="hb-lede">A bar, a ring and steps fill up to show progress. Best for uploads.</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 plays by itself. 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 hb-dots" role="img" aria-label="Progress filling to 100%">
      <div class="shows">
        <div class="prog-section">
          <div class="prog-label"><span>Bar</span><span id="pct-label">0%</span></div>
          <div class="prog-track"><div class="prog-fill" id="linear-fill"></div></div>
        </div>
        <div class="prog-circle">
          <svg width="80" height="80" viewBox="0 0 80 80">
            <circle class="prog-ring" cx="40" cy="40" r="32"/>
            <circle class="prog-arc" id="prog-arc" cx="40" cy="40" r="32"/>
          </svg>
          <div class="prog-pct" id="circ-pct">0%</div>
        </div>
        <div class="prog-section">
          <div class="prog-label"><span>Steps</span><span id="step-label">0 / 5</span></div>
          <div class="steps" id="steps">
            <div class="step"></div><div class="step"></div><div class="step"></div><div class="step"></div><div class="step"></div>
          </div>
        </div>
      </div>
      <div class="graph" aria-hidden="true">
        <svg><path class="g-fifths"/><path class="g-top"/><path class="g-axis"/><path class="g-curve"/><circle class="g-dot" r="6"/></svg>
        <span class="lbl cap-top" data-hb-caption>Fills to all of it</span>
        <span class="lbl cap-time" data-hb-caption>About two seconds</span>
        <span class="lbl cap-line" data-hb-caption>Fast at first, slowing near the end</span>
      </div>
      <p class="lbl cap-sum" aria-hidden="true" data-hb-caption>One number fills the bar, the ring and the steps</p>
      <p class="lbl side first" aria-hidden="true" data-hb-caption>Empty until done</p>
      <p class="lbl side second" aria-hidden="true" data-hb-caption>Fills over time</p>
      <p class="lbl when" aria-hidden="true" data-hb-caption>Half a second in</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-play" data-hb-replay><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Replay</button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="loop-tog" data-hb-loop autocomplete="off"><span>Loop</span></label>
      <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>The graph shows how full it gets over time, in the shape the Feel gives it; the dot rides the line as the bar, the ring and the steps fill.</p>
      <p data-hb-for="compare" hidden>Empty until done: nothing shows how far it has got until the end; Fills over time: you see how much is done, and with Loop off it rests half a second in.</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 the animation plays again.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="target-lbl">Fills to</p>
        <div class="seg" id="target-seg" role="group" aria-labelledby="target-lbl">
          <button type="button" data-pct="50" aria-pressed="false">50%</button>
          <button type="button" data-pct="75" aria-pressed="false">75%</button>
          <button type="button" data-pct="100" class="on" aria-pressed="true">100%</button>
        </div>
        <p class="hb-hint">How far the progress goes in one play.</p>
      </div>
      <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-ms="3200" aria-pressed="false">Slow</button>
          <button type="button" data-ms="2000" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="1200" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How long the fill takes.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="feel-lbl">Feel</p>
        <div class="seg" id="feel-seg" role="group" aria-labelledby="feel-lbl">
          <button type="button" data-ease="ease-out" class="on" aria-pressed="true">Smooth</button>
          <button type="button" data-ease="ease-in-out" aria-pressed="false">Gentle</button>
          <button type="button" data-ease="linear" aria-pressed="false">Even</button>
        </div>
        <p class="hb-hint">Smooth starts fast and eases into the end.</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">
          <label class="hb-switch-row"><span>Sliding bar</span><input class="hb-switch" type="checkbox" role="switch" id="indet-tog" autocomplete="off"></label>
          <p class="hb-hint">For waits of unknown length: the bar slides instead.</p>
        </div>
        <div class="hb-setting">
          <p class="hb-setting-name" id="color-lbl">Fill color</p>
          <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
            <button type="button" aria-pressed="false" aria-label="Pink" data-c="#ff6f8b" style="background:#ff6f8b"></button>
            <button type="button" aria-pressed="false" aria-label="White" data-c="#f4f4f2" style="background:#f4f4f2"></button>
            <button type="button" class="on" aria-pressed="true" aria-label="Blue" data-c="#58a6ff" style="background:#58a6ff"></button>
            <button type="button" aria-pressed="false" aria-label="Purple" data-c="#d2a8ff" style="background:#d2a8ff"></button>
            <button type="button" aria-pressed="false" aria-label="Green" data-c="#56d364" style="background:#56d364"></button>
            <button type="button" aria-pressed="false" aria-label="Orange" data-c="#ffa657" style="background:#ffa657"></button>
          </div>
          <p class="hb-hint">The color of the bar, the ring and the steps.</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 a progress indicator to [the upload, download or task whose progress you know]. Show how much is done with a bar that fills from left to right, a ring whose outline fills around the circle, or a row of steps that light up in turn, all driven by the same percentage and shown with the number. Fill it smoothly as progress arrives, and only show real progress: never race to near the end and stall. When the settings ask for it, slide a short piece along the bar for waits of unknown length. If the visitor has reduced motion turned on, jump straight to the current value. 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>Uploads</li><li>Downloads</li><li>Multi-step forms</li><li>Installs</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Unknown waits</li><li>Fake progress</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 linearFill=document.getElementById('linear-fill');
  const arc=document.getElementById('prog-arc');
  const pctLabel=document.getElementById('pct-label');
  const circPct=document.getElementById('circ-pct');
  const stepEl=document.getElementById('steps');
  const stepLabel=document.getElementById('step-label');
  const indetTog=document.getElementById('indet-tog');
  const loopTog=document.getElementById('loop-tog'),slowTog=document.getElementById('slow-tog');
  const steps=[...stepEl.querySelectorAll('.step')];
  const vars=document.documentElement.style;
  const reduceMotion=matchMedia('(prefers-reduced-motion: reduce)');
  const CIRC=201;
  const EASE={'ease-out':t=>1-(1-t)**3,'linear':t=>t,'ease-in-out':t=>t<.5?4*t*t*t:1-(-2*t+2)**3/2};
  let dur=2000,target=100,ease=EASE['ease-out'],animRaf=null,timers=[];
  const real={fill:linearFill,arc,bar:pctLabel,pct:circPct,steps,count:stepLabel};
  // The hold between plays, Compare's rest pose (half a second into a play), the dot's radius and the graph line's samples
  const HOLD=1200,POSE_MS=500,R=6,N=60;
  let mode='normal',feel='ease-out',shown=0,curT=1,busy=false,posed=false;

  // Slow motion triples the fill and the slide, never the hold or the steps' colour change.
  function speed(){return slowTog.checked?dur*3:dur}
  // Drops a timer's id once it fires, so `timers` only ever holds ones still pending.
  function later(fn,wait){
    const id=setTimeout(()=>{
      const i=timers.indexOf(id);
      if(i>-1)timers.splice(i,1);
      fn();
    },wait);
    timers.push(id);
  }

  // The bar is a full-width fill slid in from the left, so progress moves it with transform.
  function draw(d,p){
    const pct=Math.round(p);
    d.fill.style.transform='translateX('+(p-100)+'%)';
    d.arc.style.strokeDashoffset=CIRC*(1-p/100);
    d.bar.textContent=pct+'%';
    d.pct.textContent=pct+'%';
    const doneSteps=Math.ceil(p/100*5);
    d.steps.forEach((s,i)=>s.classList.toggle('done',i<doneSteps));
    d.count.textContent=doneSteps+' / 5';
  }
  function setProgress(p){shown=p;draw(real,p)}
  // Ends any slide and puts all three displays at p at once: 0 to start a fill, the result under reduced motion.
  // The steps skip their colour change for it, so a replay never shows them fading out or back in.
  function resetDisplays(p){
    linearFill.classList.remove('indet');
    steps.forEach(s=>{s.style.transition='none'});
    setProgress(p);
    void stepEl.offsetWidth;
    steps.forEach(s=>{s.style.transition=''});
  }
  function startIndet(len){
    linearFill.classList.add('indet');
    later(()=>{linearFill.classList.remove('indet')},len);
  }

  // One play fills the bar, the ring and the steps to the target. With Sliding bar on, the bar also slides for twice
  // the Speed before it shows its fill. While Loop is on, the displays hold 1200ms (HOLD) after the last movement ends
  // (the slide when there is one); Loop is checked then, so switching it off leaves them filled.
  function cycle(){
    cancelAnimationFrame(animRaf);animRaf=null;
    const move=speed(),rm=reduceMotion.matches,slide=indetTog.checked,end=slide?move*2:move;
    vars.setProperty('--prog-dur',move+'ms');
    posed=false;stage.classList.remove('posed'); // a play lifts Compare's pose
    busy=!rm;curT=rm?1:0;
    resetDisplays(rm?target:0);
    stage.setAttribute('aria-label',name());
    if(slide)startIndet(end);
    twinSync();notes();dotAt();
    later(settle,rm?1600:end+HOLD);
    if(rm)return;
    later(()=>{busy=false;twinSync()},end);
    const start=performance.now();
    function tick(now){
      const t=Math.min(Math.max((now-start)/move,0),1);
      curT=t;setProgress(target*ease(t));dotAt();
      animRaf=t<1?requestAnimationFrame(tick):null;
    }
    animRaf=requestAnimationFrame(tick);
    later(()=>{
      if(!loopTog.checked||reduceMotion.matches)return;
      cycle();
    },end+HOLD);
  }
  function play(){timers.forEach(clearTimeout);timers=[];cycle()}

  // See inside and Compare. Compare's first half is a copy of the displays (no ids, out of reach of the keyboard and screen readers)
  // that stays empty while a play runs and shows the end when it is done. The second half is the page's own displays.
  const twinBox=stage.querySelector('.shows').cloneNode(true);
  twinBox.classList.add('twin');twinBox.inert=true;twinBox.setAttribute('aria-hidden','true');
  twinBox.querySelectorAll('[id]').forEach(n=>n.removeAttribute('id'));
  stage.querySelector('.shows').before(twinBox);
  const twinLabels=twinBox.querySelectorAll('.prog-label span:last-child');
  const twin={fill:twinBox.querySelector('.prog-fill'),arc:twinBox.querySelector('.prog-arc'),bar:twinLabels[0],pct:twinBox.querySelector('.prog-pct'),
    steps:[...twinBox.querySelectorAll('.step')],count:twinLabels[1]};
  const graph=stage.querySelector('.graph'),gs=graph.querySelector('svg'),dot=gs.querySelector('.g-dot');
  const capTop=graph.querySelector('.cap-top'),capTime=graph.querySelector('.cap-time'),capLine=graph.querySelector('.cap-line'),capSum=stage.querySelector('.cap-sum');
  const noteIn=document.querySelector('[data-hb-for="inside"]'),noteCmp=document.querySelector('[data-hb-for="compare"]');
  const WORDS={inside:noteIn.textContent,compare:noteCmp.textContent};
  const STILL={inside:'The graph shows how full it would get over time, shown still. With reduced motion on, the bar, the ring and the steps show the end at once.',
    posed:'Shown still, half a second in: Empty until done shows nothing yet; Fills over time is part way. With reduced motion on, Replay jumps both straight to the end.',
    lifted:'Both show the end at once because reduced motion is on; then Empty until done is empty again and Fills over time returns to half a second in.'};
  const PHONE=matchMedia('(max-width:600px),(max-height:500px)');
  const TARGETS={50:'half',75:'three quarters',100:'all of it'};
  const FEELS={'ease-out':'Fast at first, slowing near the end','ease-in-out':'Slow, then fast, then slow',linear:'One steady pace'};
  let G={x0:0,x1:0,yb:0,h:0};
  function put(el,t){if(el.textContent!==t)el.textContent=t}
  function timeWords(ms){return ms<=1500?'About a second':ms<=2500?'About two seconds':ms<=3500?'About three seconds':ms<=4500?'About four seconds':ms<=6500?'About six seconds':'About ten seconds'}
  function name(){
    const n='Progress filling to '+target+'%';
    return mode==='inside'?n+', beside a graph of how full it is over time':mode==='compare'?'Progress shown twice: empty until done, beside filling to '+target+'% over time':n;
  }
  // Written only when its words change, so the live region is not read again. Under reduced motion the notes say what the stills show.
  function notes(){
    const rm=reduceMotion.matches,ph=PHONE.matches,all='the bar, the ring and the steps';
    const fill=indetTog.checked?(ph?'the ring fills while the bar slides':'the ring and the steps fill while the bar slides'):(ph?'the bar and the ring fill':all+' fill');
    put(noteIn,rm?STILL.inside.replace(all,ph?'the bar and the ring':all):WORDS.inside.replace(all+' fill',fill));
    put(noteCmp,rm?(posed?STILL.posed:STILL.lifted):WORDS.compare);
  }
  // The copy is empty while the play runs or Compare is posed, and shows the end once the play is done
  function twinSync(){
    if(mode!=='compare')return;
    twin.steps.forEach(s=>{s.style.transition='none'});
    draw(twin,posed||busy?0:target);
  }
  // Compare at rest: half a second into a play, by progress (it never changes with Slow motion). The page's own displays are set
  // through its own setProgress; with the sliding bar on, its short piece is held part way along.
  function pose(on){
    posed=on;stage.classList.toggle('posed',on);
    if(on){
      resetDisplays(target*ease(Math.min(1,POSE_MS/dur)));
      if(indetTog.checked&&!reduceMotion.matches)linearFill.classList.add('indet');
    }
    twinSync();notes();
  }
  function settle(){if(mode==='compare'&&!loopTog.checked)pose(true)}
  // The dot: across with the time since the play began, up with the value the page has set (at rest, where the line ends)
  function dotAt(){
    if(mode!=='inside')return;
    dot.style.transform='translate('+(G.x0+(G.x1-G.x0)*curT)+'px,'+(G.yb-G.h*(busy?shown:target)/100)+'px)';
  }
  // The graph, drawn in the box's own pixels: the line is the Feel's own ease sampled across the Speed, the top line is where Fills to
  // stops, and a faint line crosses at each fifth, where a step lights. Its words keep their size; the line's words take the spot
  // farthest from the line (the dot rides it, so the dot counts too), in one row or two. Words are measured at the box's left edge,
  // never at their last place, where the room left there would make them wrap.
  function drawGraph(){
    const W=graph.clientWidth,H=graph.clientHeight,ph=PHONE.matches;
    if(!W||!H)return;
    put(capTop,'Fills to '+TARGETS[target]);put(capTime,timeWords(speed()));put(capLine,FEELS[feel]);
    put(capSum,indetTog.checked?(ph?'The bar slides first; the ring fills':'The bar slides first; the ring and steps fill'):(ph?'One number fills the bar and the ring':'One number fills the bar, the ring and the steps'));
    const place=(el,l,t)=>{el.style.left=l+'px';el.style.top=t+'px'};
    [capTop,capTime,capLine].forEach(c=>place(c,0,0));
    capLine.style.whiteSpace='';capLine.style.width='';
    const ch=capTop.offsetHeight,tw=capTop.offsetWidth,mw=capTime.offsetWidth,nat=capLine.offsetWidth,row=capLine.offsetHeight;
    const x0=10,x1=W-R-2,yt=ch+R+4,yb=H-ch-10,h=yb-yt,yT=yb-h*target/100,pts=[],dense=[];
    G={x0,x1,yb,h};
    for(let i=0;i<=N;i++)pts.push([x0+(x1-x0)*i/N,yb-h*target*ease(i/N)/100]);
    const at=(n,d)=>gs.querySelector('.g-'+n).setAttribute('d',d);
    gs.setAttribute('viewBox','0 0 '+W+' '+H);
    at('curve','M'+pts.map(p=>p[0].toFixed(1)+' '+p[1].toFixed(1)).join('L'));
    at('axis','M'+x0+' '+(yt-4)+'V'+yb+'H'+(W-3)+'M'+(W-9)+' '+(yb-4)+'L'+(W-3)+' '+yb+'L'+(W-9)+' '+(yb+4));
    at('top','M'+x0+' '+yT+'H'+x1);
    at('fifths',[20,40,60,80].filter(v=>v<target).map(v=>'M'+x0+' '+(yb-h*v/100)+'H'+x1).join(''));
    place(capTop,x0+4,yT-ch-4);place(capTime,(x0+x1-mw)/2,yb+8);
    pts.forEach((p,i)=>{const q=pts[i+1]||p,k=Math.max(1,Math.ceil(Math.hypot(q[0]-p[0],q[1]-p[1])/5));
      for(let j=0;j<k;j++)dense.push([p[0]+(q[0]-p[0])*j/k,p[1]+(q[1]-p[1])*j/k])});
    // What a caption keeps clear of: the top line and the two other words
    const boxes=[[x0-2,yT-3,x1-x0+4,6],[x0,yT-ch-8,tw+8,ch+8],[(x0+x1-mw)/2-4,yb+4,mw+8,ch+8]];
    const CLEAR=16;
    // Searched from the lower right upward in steps of 3; the first spot with the most room (up to CLEAR from the line) wins
    const spot=(w,hh,t0,t1)=>{
      let best=null;
      for(let t=t0;t>=t1&&!(best&&best.c>=CLEAR);t-=3)for(let l=x1-4-w;l>=x0+6&&!(best&&best.c>=CLEAR);l-=3){
        if(boxes.some(([bx,by,bw,bh])=>l<bx+bw&&l+w>bx&&t<by+bh&&t+hh>by))continue;
        let c=CLEAR;
        for(const [x,y] of dense){c=Math.min(c,Math.hypot(Math.max(l-x,0,x-l-w),Math.max(t-y,0,y-t-hh)));if(best&&c<=best.c)break}
        if(!best||c>best.c)best={c,l,t,w,h:hh,rows:1};
      }
      return best;
    };
    const wrapTo=w=>{capLine.style.whiteSpace=w?'normal':'';capLine.style.width=w?w+'px':''};
    let pick=spot(nat,row,yb-8-row,2);
    if(!pick||pick.c<R+6){
      // Two rows: the narrowest width that needs only two, then the width of its longer row
      let w=Math.ceil(nat*.5);
      for(wrapTo(w);capLine.offsetHeight>row*1.9&&w<nat;wrapTo(w+=6));
      const rg=document.createRange();rg.selectNodeContents(capLine);
      w=Math.ceil(Math.max(...[...rg.getClientRects()].map(q=>q.width)))+15;wrapTo(w);
      const two=capLine.offsetHeight<row*1.9?spot(capLine.offsetWidth,capLine.offsetHeight,yb-8-capLine.offsetHeight,2):null;
      if(two&&(!pick||two.c>pick.c)){pick=two;pick.rows=2;pick.w=w}
    }
    // Under the axis, beside the time words, only when no spot above it keeps off the line
    if(!pick||pick.c<R+3){const off=spot(nat,row,yb+5,yb+5);if(off&&(!pick||off.c>pick.c))pick=off}
    if(!pick)pick={l:Math.max(2,x1-nat-4),t:yb-8-row,rows:1};
    wrapTo(pick.rows===2?pick.w:0);
    place(capLine,pick.l,pick.t);
    dotAt();
  }
  // Draws the current mode for the settings as they now are (also while at rest, and after a resize)
  function refresh(){
    if(mode==='normal')return;
    if(mode==='inside')drawGraph();
    if(mode==='compare'&&posed)pose(true);
    twinSync();notes();
  }
  // The mode switch draws the mode at once (the shared script then presses Replay, not under reduced motion). Leaving Compare's rest
  // pose puts the displays back at the end; leaving it in the middle of a play changes nothing.
  document.addEventListener('hb:mode',e=>{
    const was=mode;mode=e.detail.mode;
    if(was==='compare'&&mode!=='compare'&&posed){pose(false);resetDisplays(target)}
    if(mode==='compare')pose(!loopTog.checked&&!busy);
    stage.setAttribute('aria-label',name());
    refresh();
  });
  new ResizeObserver(refresh).observe(graph);
  document.fonts.ready.then(refresh);
  loopTog.addEventListener('change',refresh);slowTog.addEventListener('change',refresh);indetTog.addEventListener('change',refresh);

  document.getElementById('btn-play').addEventListener('click',play);
  loopTog.addEventListener('change',()=>{if(loopTog.checked)play()});
  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
      refresh();
    }));
  }
  choices('target-seg',d=>{target=+d.pct});
  choices('speed-seg',d=>{dur=+d.ms});
  choices('feel-seg',d=>{feel=EASE[d.ease]?d.ease:'ease-out';ease=EASE[feel]});
  choices('color-sw',d=>{vars.setProperty('--prog-color',d.c)});
  vars.setProperty('--prog-dur',speed()+'ms');
  if(reduceMotion.matches)resetDisplays(target); // reduced motion: the first picture is the finished one
  notes();
</script>
</body>
</html>