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.

26.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>Text Clip-Path Reveal — Animation Handbook</title>
  <meta name="description" content="Each line of a headline is uncovered in turn. Best for big headlines.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-clip-path-reveal/">
  <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="Text Clip-Path Reveal — Animation Handbook">
  <meta property="og:description" content="Each line of a headline is uncovered in turn. Best for big headlines.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-clip-path-reveal/">
  <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="Text Clip-Path Reveal — Animation Handbook">
  <meta name="twitter:description" content="Each line of a headline is uncovered in turn. Best for big headlines.">
  <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": "Text Clip-Path Reveal", "name": "Text Clip-Path Reveal", "description": "Each line of a headline is uncovered in turn. Best for big headlines.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-clip-path-reveal/", "inLanguage": "en", "articleSection": "Text & Typography", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Text & Typography, Text Clip-Path Reveal"}</script>
  <style>
    :root{--ui-accent:#ff6f8b;--line-dur:700ms;--ease:ease-out}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{display:flex;align-items:center;justify-content:center;background:#04060c;padding:clamp(24px,4vw,48px)}
    /* A laptop window under 651px tall: less room above and below the headline lets the stage keep its 260px minimum, so the player bar stays on the first screen */
    @media(min-width:601px) and (max-height:650px){.stage{padding-block:28px}}
    .headline{display:flex;flex-direction:column;gap:4px;overflow:visible}
    .line-wrap{overflow:hidden;line-height:1.15}
    .line-text{
      font-size:clamp(28px,5vw,56px);
      font-weight:700;
      line-height:1.15;
      color:#fff;
      display:block;
      clip-path:inset(0 100% 0 0);
      transition:clip-path var(--line-dur) var(--ease)
    }
    .line-text.dir-rtl{clip-path:inset(0 0 0 100%)}
    .line-text.dir-ttb{clip-path:inset(0 0 100% 0)}
    .line-text.revealed{clip-path:inset(0 0% 0 0)!important}
    /* See inside and Compare. Each line has a faint copy under it and a bright edge over it. The edge takes the clip's own transition
       from the same class, so it rides exactly with the window; it is as wide as the line and slid out of sight, its bar on the side
       the window grows toward. The stage's other parts are placed from measured sizes (the script) and never change its height. */
    .stage{position:relative}
    .line-wrap{position:relative}
    .ghost,.edge,.legend,.hn{display:none}
    .ghost{position:absolute;inset:0;z-index:-1;font-size:clamp(28px,5vw,56px);font-weight:700;color:rgba(255,255,255,.25)}
    .edge{position:absolute;inset:0;pointer-events:none;transform:translateX(-100%);transition:transform var(--line-dur) var(--ease)}
    .edge::after{content:"";position:absolute;top:0;bottom:0;right:-1.5px;width:3px;background:var(--ui-accent)}
    .line-text.dir-rtl~.edge{transform:translateX(100%)}
    .line-text.dir-rtl~.edge::after{right:auto;left:-1.5px}
    .line-text.dir-ttb~.edge{transform:translateY(-100%)}
    .line-text.dir-ttb~.edge::after{top:auto;left:0;right:0;bottom:-1.5px;width:auto;height:3px}
    .line-text.revealed~.edge{transform:none}
    .twin{display:none;position:absolute;pointer-events:none}
    .twin .line-text{clip-path:none!important;transition:none!important}
    .cap{margin:0;padding:3px 8px;border-radius:5px;background:rgba(8,10,16,.92);color:#f4f4f2;font:600 13px/1.25 var(--hb-font)}
    .cap.posecap{visibility:hidden;white-space:nowrap}
    .stage[data-hb-mode="inside"] .line-wrap,.stage[data-hb-mode="compare"] .line-wrap{outline:1.5px dashed rgba(244,244,242,.4);outline-offset:-1.5px}
    /* See inside: the real headline above, a key below it (three words for the three parts and, at rest, the pose's words) */
    .stage[data-hb-mode="inside"] .line-wrap{isolation:isolate}
    .stage[data-hb-mode="inside"] .ghost{display:block}
    .stage[data-hb-mode="inside"] .edge{display:block}
    .stage[data-hb-mode="inside"] .legend{display:flex;position:absolute;left:10px;right:10px;bottom:10px;flex-wrap:wrap;gap:6px;align-items:flex-start}
    .legend .cap{display:flex;align-items:center;gap:8px;max-width:100%}
    .legend .cap span{text-wrap:balance}
    .sw{flex:none;width:16px;height:12px;background-repeat:no-repeat}
    .sw.s-in{background:rgba(255,255,255,.25);border-radius:2px}
    .sw.s-win{height:14px;background-image:linear-gradient(var(--ui-accent),var(--ui-accent));background-position:50% 0;background-size:3px 100%}
    .sw.s-gap{background-image:linear-gradient(#f4f4f2,#f4f4f2),linear-gradient(#f4f4f2,#f4f4f2),linear-gradient(#f4f4f2,#f4f4f2);
      background-position:0 0,0 5px,0 10px;background-size:16px 2px,10px 2px,5px 2px}
    .stage[data-hb-mode="inside"].posed .legend .posecap,.stage[data-hb-mode="compare"].posed .hn .posecap{visibility:visible}
    /* Compare: the whole headline on one side, the real one on the other, each below its name (side by side, or one over the other on phones) */
    .stage[data-hb-mode="compare"] .twin{display:flex}
    .stage[data-hb-mode="compare"] .hn{display:flex;position:absolute;top:10px;gap:6px;align-items:center}
    .hn.first{left:10px}
    .hn.second{left:calc(50% + 10px)}
    .stage[data-hb-mode="compare"]::after{content:"";position:absolute;top:0;bottom:0;left:50%;border-left:1px solid rgba(244,244,242,.14)}
    @media(max-width:600px){
      .stage[data-hb-mode="compare"] .hn.second{left:10px;top:calc(50% + 10px)}
      .stage[data-hb-mode="compare"]::after{top:50%;bottom:auto;left:0;right:0;border-left:0;border-top:1px solid rgba(244,244,242,.14)}
    }
    @media(prefers-reduced-motion:reduce){.line-text{clip-path:inset(0)!important;transition:none!important}.edge{transition: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="../variable-font-morph/" rel="prev" aria-label="Previous: Variable Font Morph"><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>Variable Font Morph</span></a><a href="../marquee-ticker/" rel="next" aria-label="Next: Marquee / Ticker"><span><span class="hb-dir">Next: </span>Marquee / Ticker</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">05.05 · Text &amp; Typography</p>
    <h1>Text Clip-Path Reveal</h1>
    <p class="hb-lede">Each line of a headline is uncovered in turn. Best for big headlines.</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 hb-grow">
      <div class="headline" id="headline" aria-live="polite"></div>
      <div class="headline twin" aria-hidden="true" inert></div>
      <div class="legend" aria-hidden="true">
        <p class="cap" data-hb-caption><i class="sw s-in"></i><span>Every letter is already in place, just hidden</span></p>
        <p class="cap" data-hb-caption><i class="sw s-win"></i><span class="t-win">Each line's window opens from the left in about two thirds of a second, slowing as it ends</span></p>
        <p class="cap" data-hb-caption><i class="sw s-gap"></i><span class="t-gap">Each line starts a fifth of a second after the one above</span></p>
        <p class="cap posecap" data-hb-caption>Shown part way through</p>
      </div>
      <div class="hn first" aria-hidden="true"><p class="cap" data-hb-caption>Appears at once</p></div>
      <div class="hn second" aria-hidden="true"><p class="cap" data-hb-caption>Line by line</p><p class="cap posecap" data-hb-caption>Part way through</p></div>
    </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>Inside, every letter is already in its place, shown faint. Each line is only uncovered by a window that grows from one side, a moment after the line above.</p>
      <p data-hb-for="compare" hidden>Appears at once shows the whole headline in one go. Line by line uncovers each line in turn, so it reads as a cascade, while no letter ever moves.</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="dir-lbl">Starts from</p>
        <div class="seg" id="dir-seg" role="group" aria-labelledby="dir-lbl">
          <button type="button" data-dir="ltr" class="on" aria-pressed="true">Left</button>
          <button type="button" data-dir="rtl" aria-pressed="false">Right</button>
          <button type="button" data-dir="ttb" aria-pressed="false">Top</button>
        </div>
        <p class="hb-hint">The side each line's wipe begins on.</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="1100" aria-pressed="false">Slow</button>
          <button type="button" data-ms="700" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="400" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How long each line takes to uncover.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="delay-lbl">Delay between lines</p>
        <div class="seg" id="delay-seg" role="group" aria-labelledby="delay-lbl">
          <button type="button" data-ms="100" aria-pressed="false">Short</button>
          <button type="button" data-ms="180" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-ms="300" aria-pressed="false">Long</button>
        </div>
        <p class="hb-hint">Longer delays make the cascade easier to see.</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="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 slows at the end; Even keeps one pace.</p>
        </div>
        <div class="hb-setting">
          <label class="hb-setting-name" for="lines-in">Your lines</label>
          <textarea class="hb-text" id="lines-in" autocomplete="off">Good design
Is honest
Always.</textarea>
          <p class="hb-hint">Put each line on a row of its own.</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 line-by-line reveal to [your multi-line headline]. Lay out every line in its final place from the start, then uncover each line with a wipe that grows from one side until the whole line shows, starting each line a moment after the one above so the headline cascades in. Nothing moves or changes size; only the visible part of each line grows, so the letter spacing stays exactly as designed. A wipe that slows down as it finishes looks calmer than one that stops abruptly. The text must stay readable by screen readers the whole time. If the visitor has reduced motion turned on, show all the lines at once. 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>Headlines</li><li>Taglines</li><li>Portfolios</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Body text</li><li>Buttons and labels</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 headline=document.getElementById('headline');
  const vars=document.documentElement.style;
  const loopTog=document.getElementById('loop-tog'),slowTog=document.getElementById('slow-tog');
  const linesIn=document.getElementById('lines-in');
  const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
  const DEFAULT_LINES=['Good design','Is honest','Always.'];
  let dir='ltr',dur=700,stag=180,ease='ease-out',timers=[];
  // See inside and Compare. The Feel choices as the four numbers of their speed curves (the browser's own), and the pieces the views draw.
  const CURVES={'ease-out':[0,0,.58,1],'ease-in-out':[.42,0,.58,1],linear:[0,0,1,1]};
  const SIDES={ltr:'the left',rtl:'the right',ttb:'the top'};
  const FEELS={'ease-out':'slowing as it ends','ease-in-out':'easing in and out',linear:'at one steady pace'};
  const stage=headline.parentElement,twin=stage.querySelector('.twin'),legend=stage.querySelector('.legend');
  const tWin=legend.querySelector('.t-win'),tGap=legend.querySelector('.t-gap');
  const PAD=10,NAMES=40;
  let mode='normal',running=false,posed=false;
  // Under reduced motion both views are still pictures, and their notes say so
  if(!motionOk){
    document.querySelector('[data-hb-for="inside"]').textContent="Shown still, part way through: the faint letters are already in place, and each line's window is open no further than the one above.";
    document.querySelector('[data-hb-for="compare"]').textContent="Shown still, part way through: Appears at once is complete, while Line by line has each line's window open no further than the one above.";
  }

  function lineDur(){return slowTog.checked?dur*3:dur}
  function gapDur(){return slowTog.checked?stag*3:stag}
  function apply(){
    vars.setProperty('--line-dur',lineDur()+'ms');
    vars.setProperty('--ease',ease);
  }
  // Drops a timer's id once it fires, so `timers` only ever holds ones still pending (not every id ever
  // scheduled) — Loop can run for a long time without the array growing without bound.
  function later(fn,wait){
    const id=setTimeout(()=>{
      const i=timers.indexOf(id);
      if(i>-1)timers.splice(i,1);
      fn();
    },wait);
    timers.push(id);
  }

  // Rebuilds the lines hidden from the current text and direction. Runs directly on every keystroke in
  // Your lines, so the stage never shows stale lines, and again at the start of every play.
  function buildLines(){
    const rows=linesIn.value.split('\n').map(l=>l.trim()).filter(Boolean);
    const useRows=rows.length?rows:DEFAULT_LINES;
    headline.innerHTML='';twin.innerHTML='';
    const spans=useRows.map(line=>{
      const wrap=document.createElement('div');wrap.className='line-wrap';
      // See inside's faint copy of the line (under it) and bright edge (over it): never read aloud
      const ghost=document.createElement('span');ghost.className='ghost';ghost.textContent=line;
      const span=document.createElement('span');span.className='line-text';
      if(dir!=='ltr')span.classList.add(dir==='rtl'?'dir-rtl':'dir-ttb');
      span.textContent=line;
      const edge=document.createElement('span');edge.className='edge';
      ghost.setAttribute('aria-hidden','true');edge.setAttribute('aria-hidden','true');
      wrap.append(ghost,span,edge);headline.appendChild(wrap);
      // Compare's first half: the same line, whole
      const whole=wrap.cloneNode(false),copy=document.createElement('span');
      copy.className='line-text';copy.textContent=line;
      whole.appendChild(copy);twin.appendChild(whole);
      return span;
    });
    posed=false;stage.classList.remove('posed');
    if(mode!=='normal'){layout();if(!running)pose()}
    return spans;
  }

  // One play reveals the lines in turn. While Loop is on, the last line's reveal is followed by a 1200ms
  // hold; whether the lines then vanish and the play starts again is decided right after that hold, from
  // Loop's value at that moment, so switching Loop off during the hold still leaves the lines shown.
  function cycle(){
    apply();
    running=true;
    const lines=buildLines();
    void headline.offsetWidth;
    const gap=gapDur();
    lines.forEach((span,i)=>{
      later(()=>{span.classList.add('revealed')},50+gap*i);
    });
    later(()=>{
      if(!loopTog.checked||!motionOk){running=false;if(mode!=='normal')pose();return}
      cycle();
    },50+gap*(lines.length-1)+lineDur()+1200);
    // Reduced motion shows every line at once, so the views are stills from the start
    if(!motionOk){running=false;if(mode!=='normal')pose()}
  }
  function play(){timers.forEach(clearTimeout);timers=[];cycle()}

  // The value of a speed curve at a share x of its time: the curve's own t for x found by halving, then its height there
  function curveAt([a,b,c,e],x){
    const at=(p,q,t)=>3*p*t*(1-t)*(1-t)+3*q*t*t*(1-t)+t*t*t;
    let lo=0,hi=1,t=x;
    for(let i=0;i<30;i++){t=(lo+hi)/2;if(at(a,c,t)<x)lo=t;else hi=t}
    return at(b,e,t);
  }
  // Rest: the moment the middle line is half way through its time, each line open as far as the curve has taken it by then (the
  // window and its edge, set at once). Worked out by progress, so Slow motion changes nothing.
  function pose(){
    const spans=headline.querySelectorAll('.line-text'),m=spans.length>>1;
    spans.forEach((span,i)=>{
      const shut=(1-curveAt(CURVES[ease],Math.min(1,Math.max(0,(stag*(m-i)+dur/2)/dur))))*100+'%';
      span.style.setProperty('clip-path',dir==='ltr'?`inset(0 ${shut} 0 0)`:dir==='rtl'?`inset(0 0 0 ${shut})`:`inset(0 0 ${shut} 0)`,'important');
      span.style.setProperty('transition','none','important');
      const edge=span.nextElementSibling;
      edge.style.transition='none';
      edge.style.transform=dir==='ttb'?`translateY(-${shut})`:`translateX(${dir==='rtl'?'':'-'}${shut})`;
    });
    posed=spans.length>0;stage.classList.toggle('posed',posed);
  }
  // Leaving the pose shows the lines as the page has them, at once, never a wipe from the posed point
  function unpose(){
    if(!posed)return;
    const parts=headline.querySelectorAll('.line-text,.edge');
    parts.forEach(el=>{el.style.removeProperty('clip-path');el.style.removeProperty('transform')});
    void headline.offsetWidth;
    parts.forEach(el=>el.style.removeProperty('transition'));
    posed=false;stage.classList.remove('posed');
  }
  // The real headline stays in its place in the flow (so the box keeps Normal's height); each view moves and shrinks it into its room
  // with a transform: See inside into the room above the key, Compare into its half, with the whole copy in the other half.
  function layout(){
    const W=stage.clientWidth,H=stage.clientHeight,box=getComputedStyle(headline),hw=parseFloat(box.width),hh=parseFloat(box.height);
    if(!hw||!hh)return;
    const cx=headline.offsetLeft+hw/2,cy=headline.offsetTop+hh/2;
    const put=(el,r)=>el.style.transform=`translate(${r.x+r.w/2-cx}px,${r.y+r.h/2-cy}px) scale(${Math.min(1,r.w/hw,r.h/hh)})`;
    if(mode==='inside'){
      put(headline,{x:PAD,y:PAD,w:W-2*PAD,h:H-legend.offsetHeight-3*PAD});
      return;
    }
    const stacked=matchMedia('(max-width:600px)').matches,w=stacked?W:W/2,h=stacked?H/2:H;
    const half=k=>({x:stacked?PAD:k*w+PAD,y:stacked?k*h+NAMES:NAMES,w:w-2*PAD,h:h-NAMES-8});
    twin.style.cssText=`left:${headline.offsetLeft}px;top:${headline.offsetTop}px;width:${hw}px`;
    put(twin,half(0));put(headline,half(1));
  }
  function lasts(t){return t<500?'under half a second':t<900?'about two thirds of a second':t<1500?'about a second':t<2500?'about two seconds':'over three seconds'}
  function after(g){return g<140?'a tenth of a second':g<240?'a fifth of a second':g<400?'about a third of a second':g<700?'half a second':'nearly a second'}
  function say(el,text){if(el.textContent!==text)el.textContent=text}
  function words(){
    say(tWin,`Each line's window opens from ${SIDES[dir]} in ${lasts(lineDur())}, ${FEELS[ease]}`);
    say(tGap,`Each line starts ${after(gapDur())} after the one above`);
  }
  // A setting changed: the key's words follow, and a view at rest is built and posed again at once (a run is replayed by the page)
  function settle(){
    words();
    if(mode==='normal')return;
    if(running)layout();else buildLines();
  }

  // The mode switch: draws the mode at once. The shared script then presses Replay (not under reduced motion), which plays it.
  document.addEventListener('hb:mode',e=>{
    mode=e.detail.mode;
    words();
    if(mode==='normal'){unpose();headline.style.transform='';return}
    layout();
    if(!running)pose();
  });
  // The box changes size with the window, and its words with the site font once it has loaded: the views are placed again
  const replace=()=>{if(mode!=='normal')layout()};
  const watch=new ResizeObserver(replace);watch.observe(stage);watch.observe(headline);watch.observe(legend);
  if(document.fonts)document.fonts.ready.then(replace);

  document.getElementById('btn-play').addEventListener('click',play);
  loopTog.addEventListener('change',()=>{if(loopTog.checked)play()});
  slowTog.addEventListener('change',settle);
  linesIn.addEventListener('input',buildLines);
  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);
      settle();
    }));
  }
  choices('dir-seg',d=>{dir=d.dir});
  choices('speed-seg',d=>{dur=+d.ms});
  choices('delay-seg',d=>{stag=+d.ms});
  choices('feel-seg',d=>{ease=d.ease});
  apply();
  words();
</script>
</body>
</html>