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.

30.0 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>Split-Flap Board — Animation Handbook</title>
  <meta name="description" content="Tiles flip through the letters to spell a word, like an airport board. Best for hero titles.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/split-flap-board/">
  <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="Split-Flap Board — Animation Handbook">
  <meta property="og:description" content="Tiles flip through the letters to spell a word, like an airport board. Best for hero titles.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/split-flap-board/">
  <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="Split-Flap Board — Animation Handbook">
  <meta name="twitter:description" content="Tiles flip through the letters to spell a word, like an airport board. Best for hero titles.">
  <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": "Split-Flap Board", "name": "Split-Flap Board", "description": "Tiles flip through the letters to spell a word, like an airport board. Best for hero titles.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/split-flap-board/", "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, Split-Flap Board"}</script>
  <style>
    :root{--ui-accent:#ff6f8b;--tile:#1c1d22;--ink:#f4f4f2}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{display:flex;align-items:center;justify-content:center;padding:16px;background:#0b0b0d;container-type:inline-size}
    /* The tiles share the stage's width (100cqw), up to 68px each */
    .board{--gap:clamp(4px,1.2cqw,8px);--pad:clamp(12px,3cqw,26px);--tw:min(68px,calc((100cqw - 2 * var(--pad) - (var(--n,8) - 1) * var(--gap)) / var(--n,8)));
      display:flex;flex-direction:column;gap:clamp(10px,2cqw,16px);padding:var(--pad);border-radius:14px;background:#141519;border:1px solid rgba(255,255,255,.08)}
    .board-top{display:flex;align-items:center;gap:8px;font-size:clamp(12px,1.6cqw,14px);font-weight:700;letter-spacing:.18em;text-transform:uppercase;color:#f2c94c}
    .lamp{width:8px;height:8px;border-radius:50%;background:#5fd88a}
    .row{display:flex;gap:var(--gap)}
    /* A tile: two still halves, and two flaps that turn on the hinge in the middle */
    .tile{position:relative;flex:none;width:var(--tw);height:calc(var(--tw) * 1.4);perspective:calc(var(--tw) * 5);
      font-size:calc(var(--tw) * .78);font-weight:700;line-height:1;color:var(--ink)}
    .half,.flap{position:absolute;left:0;right:0;height:50%;overflow:hidden;background-color:var(--tile)}
    .up{top:0;border-radius:calc(var(--tw) * .1) calc(var(--tw) * .1) 0 0;background-image:linear-gradient(rgba(255,255,255,.07),rgba(255,255,255,0))}
    .down{bottom:0;border-radius:0 0 calc(var(--tw) * .1) calc(var(--tw) * .1);background-image:linear-gradient(rgba(0,0,0,0),rgba(0,0,0,.14))}
    /* Each half holds the whole letter, as tall as the tile, so it shows the top or the bottom of it */
    .half b,.flap b{position:absolute;left:0;right:0;height:200%;display:flex;align-items:center;justify-content:center}
    .up b{top:0}
    .down b{bottom:0}
    .flap{z-index:1;visibility:hidden;backface-visibility:hidden}
    .flap.up{transform-origin:50% 100%}
    .flap.down{transform-origin:50% 0}
    .flap i{position:absolute;inset:0;background:#000;opacity:0}
    .tile::after{content:'';position:absolute;z-index:2;left:0;right:0;top:calc(50% - 1px);height:2px;background:rgba(0,0,0,.55)}
    .row.clearing .half b,.zoom.clearing .half b{opacity:0;transition:opacity .3s linear}
    .row.pre .half b{opacity:0;transition:none!important}
    /* Reduced motion: no flipping; each letter fades in, tile after tile */
    @media(prefers-reduced-motion:reduce){.half b{transition:opacity .4s linear calc(var(--i,0) * 60ms)}}
    /* See inside and Compare. What they add is hidden in Normal; their words are plain words on a dark backing. */
    .board.twin,.zoom,.hinge,.cap{display:none}
    .cap{position:absolute;max-width:100%;padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.92);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);text-wrap:balance}
    .stage[data-hb-mode="inside"],.stage[data-hb-mode="compare"]{container-type:size;position:relative;padding:10px 12px}
    .stage[data-hb-mode="inside"] .board-top,.stage[data-hb-mode="compare"] .board-top{display:none}
    .stage[data-hb-mode="inside"] .board,.stage[data-hb-mode="compare"] .board{padding:0;border:0;border-radius:0;background:none;--gap:clamp(3px,.9cqw,6px)}
    /* See inside: the board in a thin strip; below it one tile up close (its two still halves dashed, the hinge a bright line, its
       flaps outlined in the colors of the side view's arms) beside a side view of the hinge, and the count of flips underneath.
       The strip's tiles and the close tile are sized from the box's own height. */
    .stage[data-hb-mode="inside"]{--arm1:#ff8aa0;--arm2:#6cc4ff;--capH:30px;display:grid;padding-inline:max(12px,calc((100% - 780px) / 2));grid-template-columns:minmax(0,.9fr) minmax(0,1.6fr);
      grid-template-rows:auto minmax(0,1fr) auto;gap:8px clamp(10px,3vw,28px);align-items:stretch;justify-content:normal}
    .stage[data-hb-mode="inside"] .board{grid-column:1/-1;justify-self:center;--tw:min(68px,calc((100cqw - (var(--n,8) - 1) * var(--gap)) / var(--n,8)),11cqh)}
    .stage[data-hb-mode="inside"] .tile.zoomed{outline:2px solid #f2c94c;outline-offset:-2px}
    .stage[data-hb-mode="inside"] .zoom{display:grid;place-items:center;container-type:size;grid-area:2/1;min-height:0}
    .zoom .tile{--tw:min(84cqw,calc(100cqh / 1.4))}
    .zoom .half{outline:2px dashed color-mix(in srgb,var(--ink) 75%,transparent);outline-offset:-4px}
    .zoom .flap.up{outline:2px solid var(--arm1);outline-offset:-2px}
    .zoom .flap.down{outline:2px solid var(--arm2);outline-offset:-2px}
    .zoom .tile::after{top:calc(50% - 1.5px);height:3px;background:#f2c94c;box-shadow:0 0 0 1.5px #0b0b0d}
    .stage[data-hb-mode="inside"] .hinge{display:grid;grid-area:2/2;grid-template-rows:minmax(0,1fr) auto minmax(0,1fr);justify-items:center;container-type:size;min-height:0}
    .hinge svg{width:min(100cqw,calc(100cqh - 2 * var(--capH)));aspect-ratio:1;overflow:visible}
    .hinge .cap{display:block;position:static}
    .hinge .c1{align-self:end;border-left:3px solid var(--arm1)}
    .hinge .c2{align-self:start;border-left:3px solid var(--arm2)}
    .hinge path{fill:none;stroke-linecap:round;stroke-linejoin:round}
    .hinge .still{stroke:rgba(244,244,242,.55);stroke-width:3;stroke-dasharray:4 4}
    .hinge .arc{stroke-width:2.5;stroke-dasharray:.1 5}
    .hinge .tip{stroke-width:2.5}
    .hinge .a1{stroke:var(--arm1)}
    .hinge .a2{stroke:var(--arm2)}
    .hinge .arm{stroke-width:6;stroke-linecap:round;transform-origin:30px 50px}
    .hinge .arm1{stroke:var(--arm1)}
    .hinge .arm2{stroke:var(--arm2)}
    .hinge .pin{fill:#f2c94c}
    .stage[data-hb-mode="inside"] .c3,.stage[data-hb-mode="inside"] .c4{display:block;position:static;justify-self:center;text-align:center}
    .stage[data-hb-mode="inside"] .c3{grid-area:3/2}
    .stage[data-hb-mode="inside"] .c4{grid-area:3/1;visibility:hidden}
    .stage[data-hb-mode="inside"].posed .c4{visibility:visible}
    /* Compare: two bands, one above the other at every width, each clipping its own content: the word on a board that appears at
       once, then the real board. The real board rests part way through a play, with its words, while nothing waits to play again. */
    .stage[data-hb-mode="compare"]{display:grid;grid-template-rows:repeat(2,minmax(0,1fr));padding:0;align-items:stretch;justify-content:normal}
    .stage[data-hb-mode="compare"] .board{display:flex;justify-content:center;align-items:center;overflow:hidden;padding:30px 12px 8px;
      --tw:min(68px,calc((100cqw - 24px - (var(--n,8) - 1) * var(--gap)) / var(--n,8)),calc((50cqh - 40px) / 1.4))}
    .stage[data-hb-mode="compare"] .twin{border-bottom:1px solid rgba(244,244,242,.14)}
    .stage[data-hb-mode="compare"] .side,.stage[data-hb-mode="compare"] .when{display:block;white-space:nowrap;top:6px;left:10px}
    .stage[data-hb-mode="compare"] .side.second,.stage[data-hb-mode="compare"] .when{top:calc(50% + 6px)}
    .stage[data-hb-mode="compare"] .when{left:auto;right:10px;visibility:hidden}
    .stage[data-hb-mode="compare"].posed .when{visibility:visible}
    @media(max-width:600px),(max-height:500px){.cap{font-size:12px}}
    @media(max-width:640px){.stage[data-hb-mode="inside"]{--capH:46px}}
  </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="../highlighter-sweep/" rel="prev" aria-label="Previous: Highlighter Sweep"><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>Highlighter Sweep</span></a><a href="../neon-flicker/" rel="next" aria-label="Next: Neon Flicker"><span><span class="hb-dir">Next: </span>Neon Flicker</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.16 · Text &amp; Typography</p>
    <h1>Split-Flap Board</h1>
    <p class="hb-lede">Tiles flip through the letters to spell a word, like an airport board. Best for hero titles.</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">
      <div class="board twin" aria-hidden="true" inert><div class="row"></div></div>
      <div class="board">
        <p class="board-top" aria-hidden="true"><span class="lamp"></span>Departures</p>
        <div class="row" id="row" role="img" aria-label="BOARDING"></div>
      </div>
      <div class="zoom" aria-hidden="true" inert></div>
      <div class="hinge" aria-hidden="true">
        <p class="cap c1" data-hb-caption>The old letter's top falls forward on the hinge</p>
        <svg viewBox="0 0 100 100" focusable="false">
          <path class="still" d="M30 8V92"/>
          <path class="arc a1" d="M30 8A42 42 0 0 1 72 50"/><path class="tip a1" d="M67 44l5 7 5-7"/>
          <path class="arc a2" d="M72 50A42 42 0 0 1 30 92"/><path class="tip a2" d="M37 87l-7 5 7 5"/>
          <line class="arm arm1" x1="30" y1="50" x2="30" y2="8"/>
          <line class="arm arm2" x1="30" y1="50" x2="30" y2="92"/>
          <circle class="pin" cx="30" cy="50" r="5"/>
        </svg>
        <p class="cap c2" data-hb-caption>then the next letter's bottom swings down</p>
      </div>
      <p class="cap c3" aria-hidden="true" data-hb-caption>This tile flips 18 times, from A up to R, about 12 flips a second</p>
      <p class="cap c4" aria-hidden="true" data-hb-caption>Shown part way through a flip</p>
      <p class="cap side first" aria-hidden="true" data-hb-caption>Appears at once</p>
      <p class="cap side second" aria-hidden="true" data-hb-caption>Flips</p>
      <p class="cap when" aria-hidden="true" data-hb-caption>Part way through</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>Inside, each tile is two still halves and two flaps on a hinge. Flip after flip, the old letter's top falls and the next one's bottom lands, until the tile reaches its letter.</p>
      <p data-hb-for="compare" hidden>Appears at once puts the word on the board in one go. Flips turns each tile through the alphabet, so the word settles tile by tile, like a real departures board.</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">
        <label class="hb-setting-name" for="word-in">Word</label>
        <input class="hb-text" type="text" id="word-in" value="BOARDING" maxlength="10" autocomplete="off" spellcheck="false" autocapitalize="characters">
        <p class="hb-hint">Up to 10 letters, A to Z. A space leaves a blank tile.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="speed-lbl">Flip speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-ms="150" aria-pressed="false">Slow</button>
          <button type="button" data-ms="85" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="45" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How long each tile takes to turn one letter.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="color-lbl">Tile color</p>
        <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
          <button type="button" class="on" aria-pressed="true" aria-label="Black" data-c="#1c1d22" data-ink="#f4f4f2" style="background:#1c1d22"></button>
          <button type="button" aria-pressed="false" aria-label="Blue" data-c="#1d3a6e" data-ink="#f4f4f2" style="background:#1d3a6e"></button>
          <button type="button" aria-pressed="false" aria-label="Red" data-c="#8a1d27" data-ink="#f4f4f2" style="background:#8a1d27"></button>
          <button type="button" aria-pressed="false" aria-label="Green" data-c="#134c39" data-ink="#f4f4f2" style="background:#134c39"></button>
          <button type="button" aria-pressed="false" aria-label="Cream" data-c="#ece5d3" data-ink="#1d1d22" style="background:#ece5d3"></button>
        </div>
        <p class="hb-hint">Cream tiles get dark letters.</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 split-flap board to [your headline or short word]. Show each letter on its own tile, split across the middle like an old airport departures board. To change a tile, flip it through the alphabet one letter at a time until it reaches its letter: the top half of the old letter falls forward over the hinge, turning in 3D and darkening as it turns, and the bottom half of the next letter swings down into place. Start the tiles a moment apart from left to right, so they settle one by one. Turn the halves with transforms only, and let screen readers hear the final word once. If the visitor has reduced motion turned on, fade the letters in instead. 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 titles</li><li>Travel sites</li><li>Event intros</li><li>Retro designs</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Long text</li><li>Calm 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 row=document.getElementById('row'),wordIn=document.getElementById('word-in');
  const stage=row.closest('.stage'),board=row.parentNode,twin=stage.querySelector('.twin'),twinRow=twin.firstChild,zoomBox=stage.querySelector('.zoom');
  const arm1=stage.querySelector('.arm1'),arm2=stage.querySelector('.arm2'),count=stage.querySelector('.c3');
  const vars=document.documentElement.style;
  const loopTog=document.getElementById('loop-tog'),slowTog=document.getElementById('slow-tog');
  const motionOk=()=>!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
  const DRUM=' ABCDEFGHIJKLMNOPQRSTUVWXYZ',STAGGER=40; // the letters a tile turns through, in order; each tile starts 40ms after the one on its left
  // See inside and Compare. While nothing waits to play again the views rest on a pose: Compare's real board two thirds of a second
  // into a play (sooner for a short word, so something is still turning), See inside's close tile with its top flap two thirds down.
  const POSE_MS=667,POSE_SHARE=.6,POSE_P=Math.sqrt(2/3)/2,NAMES={normal:'',inside:', with one tile shown up close',compare:', twice: appearing at once, then flipping'};
  let ms=85,flipMs=85,tiles=[],raf=0,clock=0,lastT=null,timers=[]; // ms: the setting; flipMs: the flip time of the play under way
  let mode='normal',text_='BOARDING',zt=null,zi=0,landed=false,boardPosed=false,fading=false; // landed: the word has settled; fading: reduced motion's letters are fading in
  document.addEventListener('hb:mode',e=>{mode=e.detail.mode;label();settle()});
  if(!motionOk()){
    document.querySelector('[data-hb-for="inside"]').textContent='Shown still, part way through one flip: the old letter\'s top half is falling forward on the hinge. Played, each tile flips like this until it reaches its letter.';
    document.querySelector('[data-hb-for="compare"]').textContent='Shown still, part way through: Appears at once already shows the word, while Flips still has its later letters turning through the alphabet.';
  }

  // The field only ever holds what the board can show (capitals, A to Z, single spaces), so the board, the field and the
  // "Word" chip always agree. Cleaned as the visitor types, before the shared script reads it; the caret stays in place.
  // A phone keyboard that is still composing a word is left alone until it is done.
  const clean=s=>s.toUpperCase().replace(/[^A-Z ]/g,'').replace(/ {2,}/g,' ');
  function tidy(){
    const v=wordIn.value,c=clean(v);
    if(c===v)return false;
    const at=clean(v.slice(0,wordIn.selectionStart)).length;
    wordIn.value=c;wordIn.setSelectionRange(at,at);
    return true;
  }
  wordIn.addEventListener('input',e=>{if(!e.isComposing)tidy()});
  wordIn.addEventListener('compositionend',()=>{if(tidy())wordIn.dispatchEvent(new Event('input',{bubbles:true}))});
  // A field left empty gets the default word back, and the input event updates the chip and plays it again
  wordIn.addEventListener('blur',()=>{
    if(wordIn.value.trim())return;
    wordIn.value='BOARDING';
    wordIn.dispatchEvent(new Event('input',{bubbles:true}));
  });
  function word(){return clean(wordIn.value).trim()||'BOARDING'}
  // A tile keeps its run of letters, from a blank to its own letter, and the flip it is on (k).
  function mk(parent,ch,i){
    const el=document.createElement('div');
    el.className='tile';el.setAttribute('aria-hidden','true');el.style.setProperty('--i',i);
    el.innerHTML='<div class="half up"><b></b></div><div class="half down"><b></b></div><div class="flap up"><b></b><i></i></div><div class="flap down"><b></b><i></i></div>';
    parent.appendChild(el);
    const [u,d,f1,f2]=el.children;
    return {el,u:u.firstChild,d:d.firstChild,f1,f2,f1b:f1.firstChild,f2b:f2.firstChild,s1:f1.lastChild,s2:f2.lastChild,seq:DRUM.slice(0,DRUM.indexOf(ch)+1),k:-1};
  }
  // One tile per letter. Compare's first board shows the finished word; See inside's close tile copies the tile with the most flips.
  function build(text){
    row.textContent=twinRow.textContent=zoomBox.textContent='';
    board.style.setProperty('--n',text.length);
    twin.style.setProperty('--n',text.length);
    text_=text.trim();label();
    tiles=[...text].map((ch,i)=>mk(row,ch,i));
    [...text].forEach((ch,i)=>rest(mk(twinRow,ch,i),ch));
    zi=tiles.reduce((best,t,i)=>t.seq.length>tiles[best].seq.length?i:best,0);
    zt=mk(zoomBox,text[zi],zi);rest(zt,' ');
    tiles[zi].el.classList.add('zoomed');
    landed=boardPosed=false;stage.classList.remove('posed');
    caption();
  }
  function rest(t,ch){t.u.textContent=t.d.textContent=ch;t.f1.style.visibility=t.f2.style.visibility='hidden'}
  function turn(flap,shade,deg){flap.angle=deg;flap.style.visibility='visible';flap.style.transform='rotateX('+deg.toFixed(2)+'deg)';shade.style.opacity=(Math.abs(deg)/180).toFixed(3)}
  // A tile on flip k of its run, p (0 to 1) of the way through it. During a flip from cur to nxt the still top half already shows
  // nxt and the still bottom half still shows cur; cur's top half falls from upright to edge-on (speeding up), then nxt's bottom
  // half swings from edge-on down into place (slowing down).
  function show(t,k,p){
    if(t.k!==k){t.k=k;const cur=t.seq[k],nxt=t.seq[k+1];t.u.textContent=t.f2b.textContent=nxt;t.d.textContent=t.f1b.textContent=cur}
    if(p<.5){const q=p*2;turn(t.f1,t.s1,-90*q*q);t.f2.style.visibility='hidden'}
    else{const q=(p-.5)*2;t.f1.style.visibility='hidden';turn(t.f2,t.s2,90*(1-q)*(1-q))}
  }
  // Tile i at a given time played, flipping fm ms a flip: works out which flip it is on and how far through it.
  // Returns whether it is still waiting or turning.
  function paint(t,i,clk,fm){
    const flips=t.seq.length-1,local=clk-i*STAGGER;
    if(local<0)return true;
    const k=Math.floor(local/fm);
    if(k>=flips){if(t.k!=='done'){t.k='done';rest(t,t.seq[flips])}return false}
    show(t,k,local/fm-k);
    return true;
  }
  // The side view's arms turn by the close tile's flaps, a flap's angle seen from the side
  function arms(){
    const a=zt.f1.style.visibility==='visible',b=zt.f2.style.visibility==='visible';
    arm1.style.opacity=a?1:0;arm2.style.opacity=b?1:0;
    if(a)arm1.style.transform='rotate('+(-zt.f1.angle).toFixed(2)+'deg)';
    if(b)arm2.style.transform='rotate('+(-zt.f2.angle).toFixed(2)+'deg)';
  }
  // Every frame works out, from the time played, which flip each tile is on and how far through it (See inside's close tile the
  // same way, from the same clock). The clock moves by the time since the last frame (at most 50ms, so a hidden tab does not
  // jump), a third as fast in Slow motion.
  function frame(ts){
    const dt=lastT===null?0:Math.min(ts-lastT,50);lastT=ts;
    clock+=dt*(slowTog.checked?1/3:1);
    let busy=false;
    for(let i=0;i<tiles.length;i++)if(paint(tiles[i],i,clock,flipMs))busy=true;
    if(mode==='inside'){paint(zt,zi,clock,flipMs);arms()}
    raf=busy?requestAnimationFrame(frame):0;
    if(!busy)finished();
  }
  function later(fn,wait){timers.push(setTimeout(fn,wait))}
  function setClearing(on){row.classList.toggle('clearing',on);twinRow.classList.toggle('clearing',on);zoomBox.classList.toggle('clearing',on)}
  // While Loop is on, the word holds 1800ms, the letters fade away and the board flips again from blank
  function finished(){
    landed=true;
    if(!loopTog.checked||!motionOk()){settle();return}
    later(()=>{
      if(!loopTog.checked)return;
      setClearing(true);
      later(cycle,400);
    },1800);
  }
  // Every play starts from blank tiles. Under reduced motion the letters fade in instead of flipping. The flip time is read
  // here, once per play: a Flip speed pressed mid-run applies from the next play, so no tile jumps or flips again.
  function cycle(){
    cancelAnimationFrame(raf);raf=0;
    flipMs=ms;
    build(word());
    setClearing(false);
    if(!motionOk()){
      tiles.forEach(t=>rest(t,t.seq[t.seq.length-1]));
      row.classList.add('pre');void row.offsetWidth;row.classList.remove('pre');
      landed=fading=true;settle();
      later(()=>{fading=false;settle()},tiles.length*60+500);
      return;
    }
    tiles.forEach(t=>rest(t,' '));
    clock=0;lastT=null;raf=requestAnimationFrame(frame);
  }
  function play(){timers.forEach(clearTimeout);timers=[];cycle()}

  // The views' rest pictures, drawn at once and again after any change (a play under way is drawn by the frame step): Compare's
  // real board part way through a play, See inside's close tile part way through a flip, while nothing waits to play again.
  function settle(){
    caption();
    if(raf)return;
    const still=!loopTog.checked||!motionOk(),posing=mode==='compare'&&still&&!fading;
    if(posing)poseBoard();else if(boardPosed)unpose();
    if(mode==='inside'){
      if(still)show(zt,zt.seq.length-2,POSE_P);else paint(zt,zi,clock,flipMs);
      arms();
    }
    stage.classList.toggle('posed',posing||(mode==='inside'&&still));
  }
  function poseBoard(){
    boardPosed=true;
    const clk=Math.min(POSE_MS,POSE_SHARE*tiles.reduce((m,t,i)=>Math.max(m,i*STAGGER+(t.seq.length-1)*ms),0));
    tiles.forEach((t,i)=>{if(clk<i*STAGGER){t.k=-1;rest(t,' ')}else paint(t,i,clk,ms)});
  }
  // The real tiles go back at once to what Normal shows: the settled word, or blank before any play
  function unpose(){
    boardPosed=false;
    tiles.forEach(t=>{t.k=landed?'done':-1;rest(t,landed?t.seq[t.seq.length-1]:' ')});
  }
  // The picture is named for the mode on show
  function label(){row.setAttribute('aria-label',text_+NAMES[mode])}
  // The close tile's count of flips and their pace (a third as many a second in Slow motion)
  function caption(){
    const n=zt.seq.length-1,per=Math.round(1000/(raf?flipMs:ms)/(slowTog.checked?3:1));
    const s='This tile flips '+(n>1?n+' times, from A up to ':'once, from blank to ')+zt.seq[n]+', about '+per+' flips a second';
    if(count.textContent!==s)count.textContent=s;
  }
  document.addEventListener('visibilitychange',()=>{lastT=null});

  document.getElementById('btn-play').addEventListener('click',play);
  loopTog.addEventListener('change',()=>{if(loopTog.checked)play();else settle()});
  slowTog.addEventListener('change',caption);
  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);
    }));
  }
  choices('speed-seg',d=>{ms=+d.ms;settle()});
  choices('color-sw',d=>{vars.setProperty('--tile',d.c);vars.setProperty('--ink',d.ink)});
  build(word());tiles.forEach(t=>rest(t,' '));
</script>
</body>
</html>