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.

35.1 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Elastic Transition — Animation Handbook</title>
  <meta name="description" content="The new page slides in, goes too far and springs back. Best for playful apps.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/elastic-transition/">
  <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="Elastic Transition — Animation Handbook">
  <meta property="og:description" content="The new page slides in, goes too far and springs back. Best for playful apps.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/elastic-transition/">
  <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="Elastic Transition — Animation Handbook">
  <meta name="twitter:description" content="The new page slides in, goes too far and springs back. Best for playful apps.">
  <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": "Elastic Transition", "name": "Elastic Transition", "description": "The new page slides in, goes too far and springs back. Best for playful apps.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/03-page-transitions/elastic-transition/", "inLanguage": "en", "articleSection": "Page Transitions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Page Transitions, Elastic Transition"}</script>
  <style>
    :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#b98cff;--ui-text:#f4f4f2;--ui-muted:#8a8a92}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{display:flex;flex-direction:column;overflow:hidden;background:var(--bg);color:var(--ui-text)}
    .mini-nav{display:flex;gap:6px;padding:10px 12px;background:var(--ui-bg);border-bottom:1px solid var(--ui-border);flex-shrink:0}
    .nav-btn{background:none;border:1px solid var(--ui-border);color:var(--ui-muted);font:inherit;font-size:13px;padding:0 12px;border-radius:4px;cursor:pointer;min-height:44px}
    .nav-btn.active{border-color:var(--ui-accent);color:var(--ui-accent)}
    @media(hover:hover){.nav-btn:hover{border-color:var(--ui-accent)}}
    .page-area{flex:1;position:relative;overflow:hidden}
    .page{position:absolute;inset:0;opacity:0;pointer-events:none}
    .page.active{opacity:1;pointer-events:auto}
    .pc{display:flex;flex-direction:column;justify-content:center;height:100%;padding:clamp(20px,4vw,40px);gap:12px}
    .pe{font-size:10px;text-transform:uppercase;letter-spacing:3px;opacity:.6}
    .ph{font-size:clamp(22px,3.5vw,36px);font-weight:bold;line-height:1.15}
    .pp{font-size:12px;opacity:.6;line-height:1.65;max-width:320px}
    .pgrid{display:grid;grid-template-columns:repeat(3,1fr);gap:6px;margin-top:4px}
    .gi{background:rgba(255,255,255,.07);border:1px solid rgba(255,255,255,.1);border-radius:6px;padding:18px 12px;font-size:11px}
    .pstat{display:flex;gap:20px;margin-top:4px}
    .pstat b{display:block;font-size:clamp(18px,2.5vw,24px);color:var(--ui-accent)}
    .pstat span{font-size:9px;opacity:.6;text-transform:uppercase;letter-spacing:1px}
    /* Short windows leave the page area under 220px: tighter spacing keeps the tallest page inside it with room to spare */
    @media(max-height:640px){.pc{gap:8px}.gi{padding:12px}}
    @media(prefers-reduced-motion:reduce){.page{transition:none!important;animation:none!important}}
    /* See inside and Compare. The small site sits in a box; in Compare a copy of it, the twin, sits beside it. Labels are plain words
       on a dark backing. In both views the page area's own ground is hatched, so the gap a page leaves off its place shows. */
    .box{display:flex;flex-direction:column;flex:1;min-height:0;min-width:0}
    .twin,.trace,.lbl{display:none}.twin .nav-btn{display:inline-flex;align-items:center}
    .lbl{position:absolute;z-index:5;padding:2px 7px;border-radius:5px;background:#111114;color:#f4f4f2;font:600 12px/1.25 var(--hb-font);white-space:nowrap}
    .stage[data-hb-mode="inside"] .page-area,.stage[data-hb-mode="compare"] .page-area{background:repeating-linear-gradient(135deg,#2e2642 0 2px,var(--bg) 2px 7px)}
    /* See inside: under the page area, the graph of the new page's place over time (drawTrace); at rest the page is held at its farthest point */
    .stage[data-hb-mode="inside"] .trace,.stage[data-hb-mode="inside"] .trace .lbl,.stage[data-hb-mode="inside"].posed .past{display:block}
    .stage[data-hb-mode="inside"] .pc{gap:8px}.stage[data-hb-mode="inside"] .pgrid{display:none}
    .trace{position:relative;flex:none;height:100px;background:#111114;border-top:1px solid var(--ui-border)}
    .trace svg{position:absolute;left:0;top:26px;width:100%;height:calc(100% - 32px)}
    .place{fill:none;stroke:rgba(244,244,242,.6);stroke-width:1.5;stroke-dasharray:5 4}.tip{fill:var(--ui-accent)}.dot{fill:#f4f4f2}
    .curve{fill:none;stroke:var(--ui-accent);stroke-width:2.5;stroke-linejoin:round}
    .past{right:calc(var(--past,0%) + 6px);bottom:8px}.at{right:6px}
    /* Compare: the copy, whose new page stops at its place, then the real site, which goes past it; at rest both are held at the
       moment the real page is furthest past its place. The half names sit at the bottom middle, under the labels and off the gaps. */
    .stage[data-hb-mode="compare"]{position:relative;display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:2px;background:#2a2a30}.stage[data-hb-mode="compare"] .twin{display:flex}
    .stage[data-hb-mode="compare"] .side,.stage[data-hb-mode="compare"].posed .past,.stage[data-hb-mode="compare"].posed .at{display:block}
    .stage[data-hb-mode="compare"] .past,.stage[data-hb-mode="compare"] .at{bottom:36px}
    .side{bottom:10px;transform:translateX(-50%)}.side.first{left:25%}.side.second{left:75%}
    @media(max-width:600px){.stage[data-hb-mode="compare"] .mini-nav{gap:4px;padding:6px}.stage[data-hb-mode="compare"] .ph{font-size:16px}.stage[data-hb-mode="compare"] .pc{padding:12px;gap:6px}
      .stage[data-hb-mode="compare"] .nav-btn{padding:0 6px;font-size:12px;min-width:44px;justify-content:center}}
    /* Phones and short windows (a phone held sideways too): a shorter strip, and the pages keep only their eyebrow and heading */
    @media(max-width:600px),(max-height:700px){.trace{height:80px}
      .stage[data-hb-mode="inside"] .pp,.stage[data-hb-mode="inside"] .pstat,.stage[data-hb-mode="compare"] .pp,.stage[data-hb-mode="compare"] .pgrid,.stage[data-hb-mode="compare"] .pstat{display:none}
    }
    /* From 360px down the halves stack: a half is short, so its page text starts at the top, its labels sit in its bottom row and its
       name at the right end of its nav row */
    @media(max-width:360px){
      .stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}
      .stage[data-hb-mode="compare"] .pc{justify-content:flex-start}.stage[data-hb-mode="compare"] .past,.stage[data-hb-mode="compare"] .at{bottom:10px}
      .stage[data-hb-mode="compare"] .side{left:auto;right:10px;bottom:auto;top:19px;transform:none}.stage[data-hb-mode="compare"] .side.second{top:calc(50% + 20px)}
    }
  </style>
  <style id="kf-style"></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="do" 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="../blur-transition/" rel="prev" aria-label="Previous: Blur Transition"><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>Blur Transition</span></a><a href="../portal-zoom/" rel="next" aria-label="Next: Portal / Tunnel Zoom"><span><span class="hb-dir">Next: </span>Portal / Tunnel Zoom</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">03.09 · Page Transitions</p>
    <h1>Elastic Transition</h1>
    <p class="hb-lede">The new page slides in, goes too far and springs back. Best for playful apps.</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">Click it</h2><p>Click Work or About at the top of the box, or press Show me.</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">
      <div class="box" id="box">
        <nav class="mini-nav" id="nav" aria-label="Demo pages"></nav>
        <div class="page-area" id="page-area"><span class="lbl past" aria-hidden="true" data-hb-caption>Past its place</span></div>
      </div>
      <div class="trace" aria-hidden="true"><svg><path class="place"/><path class="curve"/><path class="tip" d="M-6 -5L6 0L-6 5Z"/><circle class="dot" r="5"/></svg><span class="lbl way" data-hb-caption>A planned path</span><span class="lbl here" data-hb-caption>Its place</span><span class="lbl ends" data-hb-caption>Settles</span></div>
      <p class="lbl side first" aria-hidden="true" data-hb-caption>Stops dead</p>
      <p class="lbl side second" aria-hidden="true" data-hb-caption>Springs back</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-demo" data-hb-demo><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>Show me</button>
      <button class="hb-play" type="button" id="btn-reset" data-hb-reset><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>Reset</button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the graph traces the new page's place over time: it comes in from the side, goes past its place, springs back and settles. Click Work or About, or press Show me.</p>
      <p data-hb-for="compare" hidden>Stops dead slides the new page in and stops at its place; Springs back goes a little past and bounces back before settling. Click Work or About, or press Show me.</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, then try it again or press Show me.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="mode-lbl">How it bounces</p>
        <div class="seg" id="mode-seg" role="group" aria-labelledby="mode-lbl">
          <button type="button" data-mode="css" class="on" aria-pressed="true">Planned path</button>
          <button type="button" data-mode="spring" aria-pressed="false">Live spring</button>
        </div>
        <p class="hb-hint">A planned path is cheap; a live spring feels more physical.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="size-lbl">Bounce size</p>
        <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">
          <button type="button" data-size="0" aria-pressed="false">Small</button>
          <button type="button" data-size="1" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-size="2" aria-pressed="false">Large</button>
        </div>
        <p class="hb-hint">How far the page goes past its place before settling.</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-speed="0" aria-pressed="false">Slow</button>
          <button type="button" data-speed="1" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-speed="2" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How quickly the page springs into place.</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 an elastic transition to [the pages or panels you want to move between]. The new page should slide in from the side, go a little past its place and spring back before settling, as if pulled by a rubber band, while the old page slides away. The bounce can be planned ahead as a fixed path, which is cheap, or worked out live with a simple spring, which can react if the visitor interrupts it; with a live spring, advance it in small fixed time steps so it moves the same on every screen, and limit the time one frame adds so a slow frame cannot throw it off. If the visitor has reduced motion turned on, switch pages without movement. 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>Playful apps</li><li>Panels and drawers</li><li>Games</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Formal sites</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 PAGES=[
    {name:'Home',bg:'linear-gradient(135deg,#0d1a2e,#192640)',html:`<div class="pc"><div class="pe">Creative Studio</div><p class="ph">Design without compromise.</p><p class="pp">We craft digital products that earn attention and keep it.</p></div>`},
    {name:'Work',bg:'linear-gradient(135deg,#0d1f15,#152e1e)',html:`<div class="pc"><div class="pe">Selected Work</div><p class="ph">The Portfolio</p><div class="pgrid">${['Atlas','Orbit','Prism','Lumen','Frame','Echo'].map(n=>`<div class="gi">${n}</div>`).join('')}</div></div>`},
    {name:'About',bg:'linear-gradient(135deg,#1f0d26,#2e1540)',html:`<div class="pc"><div class="pe">Our Story</div><p class="ph">Built on conviction.</p><p class="pp">18 people who care unreasonably about craft. Independent since 2016.</p><div class="pstat"><div><b>140+</b><span>Projects</span></div><div><b>9</b><span>Years</span></div></div></div>`},
  ];
  const nav=document.getElementById('nav'),area=document.getElementById('page-area'),slowTog=document.getElementById('slow-tog');
  const kfStyle=document.getElementById('kf-style');
  const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
  // Bounce size (Small, Medium, Large) and Speed (Slow, Normal, Fast) each pick one value for both ways of bouncing
  const OVERSHOOT=[30,60,100],DURATION=[1400,900,550],STIFFNESS=[75,200,640];
  // The live spring's damping for each size (row) and speed (column); it and the stiffness above were found by running its loop so that it goes about as far past its place as the planned path does
  const DAMPING=[[12.8,19.7,33.6],[10.6,17.4,29.7],[9.5,15.2,27.2]];
  const STEP=1/120,STEPS_MAX=8; // the live spring moves in steps of 1/120 s, and takes at most 8 of them in one frame
  let current=0,animating=false,mode='css',size=1,speed=1,rafId=0,afterMove=null,wayBack=0,runFrom=null,timers=[];
  const pages=[],navBtns=[];

  PAGES.forEach((p,i)=>{
    const d=document.createElement('div');d.className='page';d.style.background=p.bg;d.innerHTML=p.html;area.appendChild(d);pages.push(d);
    const b=document.createElement('button');b.type='button';b.className='nav-btn';b.textContent=p.name;
    b.addEventListener('click',()=>navigate(i));nav.appendChild(b);navBtns.push(b);
  });
  // Compare's first half: the same small site, whose new page slides in and stops at its place. A copy built from the same pages,
  // with no ids and no buttons, out of the Tab order and hidden from screen readers; every change of the real site plays on it too.
  const box=document.getElementById('box'),stage=box.parentElement,twin=document.createElement('div');
  twin.className='box twin';twin.inert=true;twin.setAttribute('aria-hidden','true');
  twin.innerHTML=`<div class="mini-nav">${PAGES.map(p=>`<span class="nav-btn">${p.name}</span>`).join('')}</div>`+
    `<div class="page-area">${PAGES.map(p=>`<div class="page" style="background:${p.bg}">${p.html}</div>`).join('')}<span class="lbl at" data-hb-caption>At its place</span></div>`;
  box.before(twin);
  const tp=[...twin.querySelectorAll('.page')],tn=[...twin.querySelectorAll('.nav-btn')],atLbl=twin.querySelector('.at');
  // See inside's graph: 1.6 s across (slow motion aside); up and down, from 8% of the width short of its place to 16% past it; 26px down
  const trace=stage.querySelector('.trace'),svg=trace.querySelector('svg'),[placeEl,curveEl,tipEl]=svg.querySelectorAll('path');
  const dot=svg.querySelector('.dot'),[wayLbl,hereLbl,endsLbl]=trace.querySelectorAll('.lbl'),SPAN=1600,TOP=8,BOTTOM=-16,SVG_TOP=26;
  // look: the mode on show; posed: held at the farthest point (pose); run: the change under way, as drawn; tw: the copy's spring
  let look='normal',posed=false,run=null,tw=null,dotRaf=0,G={W:0,k:0};
  if(!motionOk){ // under reduced motion both views are still pictures, and their notes say so
    document.querySelector('[data-hb-for="inside"]').textContent='Shown still at its farthest point: the new page has gone past its place, and the graph traces how it comes in, overshoots and settles.';
    document.querySelector('[data-hb-for="compare"]').textContent='Shown still at the moment Springs back is furthest past its place: Stops dead has not gone past it.';
  }

  // Drops a timer's id once it fires, so `timers` only holds the ones still waiting.
  function later(fn,wait){
    const id=setTimeout(()=>{
      const i=timers.indexOf(id);
      if(i>-1)timers.splice(i,1);
      fn();
    },wait);
    timers.push(id);
    return id;
  }
  function cancel(id){
    clearTimeout(id);
    const i=timers.indexOf(id);
    if(i>-1)timers.splice(i,1);
  }

  // The planned path: keyframes written for each transition; ov (30, 60 or 100) scales how far past its place the page goes
  function buildKF(ov){
    const t=ov/100;
    kfStyle.textContent=`@keyframes elastic-in{
      0%{transform:translateX(100%);opacity:0}
      5%{opacity:1}
      55%{transform:translateX(${-t*12}%)}
      75%{transform:translateX(${t*4}%)}
      88%{transform:translateX(${-t*1.5}%)}
      100%{transform:translateX(0);opacity:1}
    }
    @keyframes elastic-out{
      0%{transform:translateX(0);opacity:1}
      40%{transform:translateX(${t*6}%)}
      100%{transform:translateX(-100%);opacity:0}
    }`;
  }

  function updateNav(n){navBtns.forEach((b,i)=>{b.classList.toggle('active',i===n);b.setAttribute('aria-current',i===n?'page':'false')})}
  // One page's resting look: its class alone shows or hides it, with nothing left over from a transition (its background
  // stays). A page that is not shown is also out of the Tab order and the accessibility tree (inert).
  function rest(el,on){
    el.classList.toggle('active',on);
    el.inert=!on;
    el.style.animation='';el.style.transition='';el.style.transform='';el.style.opacity='';
  }
  // The resting state for page n, at once. Running animations are cancelled too, so no tail of the last movement shows.
  function showPage(n){
    pages.forEach((el,i)=>{rest(el,i===n);el.getAnimations().forEach(a=>a.cancel())});
    updateNav(n);
  }

  // Ends a transition: the demo is idle again, and a Show me run goes on to its next step
  function finish(){
    animating=false;
    const then=afterMove;afterMove=null;
    if(then)then();
    views();
  }
  // Stops everything at once (timers, the spring's frames, the run's next step) and shows the resting state for `current`
  function settle(){
    timers.forEach(clearTimeout);timers=[];wayBack=0;afterMove=null;
    if(rafId){cancelAnimationFrame(rafId);rafId=0}
    animating=false;
    showPage(current);
    views();
  }

  function navigate(next,then){
    if(next===current||animating)return;
    unpose();
    const prev=current;current=next;updateNav(next);
    afterMove=then||null;
    if(!motionOk){showPage(next);finish();return}
    animating=true;
    const k=slowTog.checked?3:1; // read when the transition starts; slow motion triples its movements and the timers that wait for them
    run={way:mode,size,speed};
    if(mode==='css')doCSS(pages[prev],pages[next],OVERSHOOT[size],DURATION[speed]*k);
    else doSpring(pages[prev],pages[next],k,STIFFNESS[speed],DAMPING[size][speed]);
    // The views: in Compare the copy plays the same change its own way; in See inside the dot rides the curve
    if(look==='compare')twinGo(prev,next,k);
    ride();
  }

  // Planned path: the old page's keyframes carry it away, the new page's carry it in past its place and back; d is their length in ms
  function doCSS(o,n,overshoot,d){
    buildKF(overshoot);
    n.style.transform='translateX(100%)';
    void n.offsetWidth; // commit the start off-screen before the animations begin
    o.style.animation=`elastic-out ${d}ms ease forwards`;
    n.classList.add('active');n.style.animation=`elastic-in ${d}ms ease forwards`;
    later(()=>{rest(o,false);rest(n,true);finish()},d+50);
  }

  // Live spring: the old page slides away simply, the new one is pulled in from the right by a spring worked out in fixed steps: each frame adds up the time
  // that has passed and takes the steps it covers, so the spring moves the same on every screen. The last step can end a little after the frame, so the page is
  // drawn between the last two steps, at the frame's own time.
  function doSpring(o,n,k,stiffness,damping){
    const out=300*k;
    o.style.transition=`transform ${out}ms ease-in, opacity ${out}ms ease-in`;
    o.style.transform='translateX(-40%)';o.style.opacity='0';
    let pos=100,before=pos,vel=0,target=0,acc=0,lastTime=null,steps=0;
    n.style.transform=`translateX(${pos}%)`;n.classList.add('active');
    function tick(now){
      if(lastTime!==null)acc+=Math.min(now-lastTime,50)/1000/k; // the 50ms limit first, then a third of the time in slow motion; the first frame adds none
      lastTime=now;
      for(let i=0;acc>0&&i<STEPS_MAX;i++){
        before=pos;vel+=(-stiffness*(pos-target)-damping*vel)*STEP;pos+=vel*STEP;acc-=STEP;
        steps++;if(tw)twinStep(); // in Compare the copy's spring takes the same step
        if(Math.abs(pos-target)<0.5&&Math.abs(vel)<0.5){rafId=0;rest(o,false);rest(n,true);finish();return}
      }
      if(acc>0)acc=0; // the step limit was reached: drop the time left, so the page is never drawn ahead of the last step
      const shown=pos+(before-pos)*(-acc/STEP);
      n.style.transform=`translateX(${shown.toFixed(2)}%)`;
      springDrawn(steps*STEP+acc,shown,-acc/STEP);
      rafId=requestAnimationFrame(tick);
    }
    later(()=>{rafId=requestAnimationFrame(tick)},100*k);
  }

  // Show me: one visit to the next page, a hold of 1200ms, and the way back to where the run started
  // runFrom is the page the run started from, kept until the way back ends: a Show me pressed during a run (a second press, or the
  // one a mode press sends) goes back to it first, not to the page the run was heading for
  function showMe(){
    if(runFrom!==null){current=runFrom;updateNav(current)}
    settle();
    const start=current;runFrom=start;
    navigate((start+1)%PAGES.length,()=>{wayBack=later(()=>{wayBack=0;navigate(start,()=>{runFrom=null})},1200)});
  }
  function reset(){runFrom=null;current=0;settle()}

  document.getElementById('btn-demo').addEventListener('click',showMe);
  document.getElementById('btn-reset').addEventListener('click',reset);
  // The visitor took over: cancel what the run has not started yet. A page already moving finishes on its own.
  document.addEventListener('hb:input',()=>{afterMove=null;runFrom=null;cancel(wayBack);wayBack=0;pose()});

  // CSS's ease at a share x of a keyframe step: the curve's own t for x found by halving, then its height there
  function ease(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(.25,.25,t)<x)lo=t;else hi=t}return at(.1,1,t)}
  // The planned path as buildKF writes it, [share of the time, place in % of the width] at each keyframe, and the place at share u
  const keys=T=>[[0,100],[.55,-12*T],[.75,4*T],[.88,-1.5*T],[1,0]];
  function along(K,u){let i=1;while(i<K.length-1&&u>K[i][0])i++;const[a,p]=K[i-1],[b,q]=K[i];return p+(q-p)*ease((u-a)/(b-a))}
  // The live spring run ahead with doSpring's own steps and settle test: the real page's place after each step, and the copy's,
  // whose spring is damped just enough not to go past its place (twice the square root of the stiffness)
  function springRun(st,da){const P=[100],C=[100],dc=2*Math.sqrt(st);let p=100,v=0,c=100,w=0;
    do{v+=(-st*p-da*v)*STEP;p+=v*STEP;w+=(-st*c-dc*w)*STEP;c+=w*STEP;P.push(p);C.push(c)}while(!(Math.abs(p)<0.5&&Math.abs(v)<0.5));return{P,C}}
  // The change the views draw: the one under way, else the next one as the settings stand. pts: [ms, place] (slow motion aside);
  // dip: how far it goes (minus: past its place), at ms `at`; twin: where the copy is then; ends: when it settles
  function plan(){
    const r=run||{way:mode,size,speed},T=OVERSHOOT[r.size]/100,d=DURATION[r.speed],K=keys(T),pts=[];
    if(r.way==='css'){for(let i=0;i<=200;i++)pts.push([d*i/200,along(K,i/200)]);return{pts,dip:-12*T,at:.55*d,twin:0,ends:d}}
    const{P,C}=springRun(STIFFNESS[r.speed],DAMPING[r.size][r.speed]),ms=i=>100+i*STEP*1000,end=ms(P.length-1);let m=0;
    P.forEach((p,i)=>{if(p<P[m])m=i});
    return{pts:[[0,100],...P.map((p,i)=>[ms(i),p]),[end,0]],dip:P[m],at:ms(m),twin:C[m],ends:end};
  }
  const gx=t=>8+(G.W-16)*t/SPAN,gy=v=>(TOP-v)*G.k;
  function dotAt(t,v){dot.style.transform=`translate(${gx(t).toFixed(1)}px,${gy(v).toFixed(1)}px)`}
  // The graph in the strip's own pixels: across is time, down is past its place; an arrow 10px down the slide coming in from the top.
  // Its words keep off the curve: the way it bounces in the top row, "Its place" over the line's right end, "Settles" under it.
  function drawTrace(){
    const W=trace.clientWidth,H=trace.clientHeight-SVG_TOP-6;if(!W)return;
    const p=plan(),pt=([t,v])=>gx(t).toFixed(1)+' '+gy(v).toFixed(1);G={W,k:H/(TOP-BOTTOM)};
    svg.setAttribute('viewBox',`0 0 ${W} ${H}`);placeEl.setAttribute('d',`M0 ${gy(0).toFixed(1)}H${W}`);curveEl.setAttribute('d','M'+p.pts.map(pt).join('L')+'H'+W);
    const vA=TOP-10/G.k,i=p.pts.findIndex(([,v])=>v<vA),[t1,v1]=p.pts[i-1],[t2,v2]=p.pts[i],a=Math.atan2(gy(v2)-gy(v1),gx(t2)-gx(t1));
    tipEl.setAttribute('transform',`translate(${gx(t1+(t2-t1)*(v1-vA)/(v1-v2)).toFixed(1)} 10) rotate(${(a*180/Math.PI).toFixed(1)})`);
    wayLbl.textContent=(run||{way:mode}).way==='css'?'A planned path':'A live spring';
    const put=(el,l,t)=>{el.style.left=Math.max(4,Math.min(l,W-el.offsetWidth-4))+'px';el.style.top=t.toFixed(1)+'px'},line=SVG_TOP+gy(0);
    put(wayLbl,4,3);put(hereLbl,W,line-hereLbl.offsetHeight-4);put(endsLbl,gx(p.ends)+4,line+4);if(posed)dotAt(p.at,p.dip);
  }
  // In See inside the dot rides the curve during a change: a planned path's at the animation's own time; a spring's from the start,
  // then wherever the spring's frames draw the page (springDrawn)
  function ride(){
    cancelAnimationFrame(dotRaf);dotRaf=0;if(look!=='inside'||!animating)return;
    const a=run.way==='css'&&pages[current].getAnimations()[0],u=(a&&a.effect.getComputedTiming().progress)||0;
    dotAt(u*DURATION[run.speed],a?along(keys(OVERSHOOT[run.size]/100),u):100);if(a)dotRaf=requestAnimationFrame(ride);
  }
  // Each step and frame of the real spring (doSpring): the copy's spring takes the same step and is drawn at the same moment, between
  // its last two steps, and the dot goes to the real page's drawn place
  function twinStep(){tw.before=tw.pos;tw.vel+=(-tw.st*tw.pos-tw.da*tw.vel)*STEP;tw.pos+=tw.vel*STEP}
  function springDrawn(s,shown,f){if(tw)tw.n.style.transform=`translateX(${(tw.pos+(tw.before-tw.pos)*f).toFixed(2)}%)`;if(look==='inside')dotAt(100+s*1000,shown)}
  // A change on the copy: the planned path without its bounce (the same start and ease, at its place by 55% of the time, where the real
  // page is furthest past; the old page leaves with a plain slide), or the same spring damped not to go past, stepped in doSpring's loop
  function twinGo(a,b,k){
    const o=tp[a],n=tp[b],d=DURATION[run.speed]*k,st=STIFFNESS[run.speed];
    tn.forEach((s,i)=>s.classList.toggle('active',i===b));n.classList.add('active');n.style.transform='translateX(100%)';
    if(run.way==='css'){
      kfStyle.textContent+='@keyframes stops-in{0%{transform:translateX(100%);opacity:0}5%{opacity:1}55%{transform:translateX(0)}100%{transform:translateX(0);opacity:1}}'+
        '@keyframes stops-out{0%{transform:translateX(0);opacity:1}100%{transform:translateX(-100%);opacity:0}}';
      void n.offsetWidth;o.style.animation=`stops-out ${d}ms ease forwards`;n.style.animation=`stops-in ${d}ms ease forwards`;
    }else{
      o.style.transition=`transform ${300*k}ms ease-in, opacity ${300*k}ms ease-in`;o.style.transform='translateX(-40%)';o.style.opacity='0';
      tw={n,st,da:2*Math.sqrt(st),pos:100,before:100,vel:0};
    }
  }
  // The copy at rest on the page the real site shows: its spring stopped, its animations gone
  function twinRest(){tw=null;tn.forEach((s,i)=>s.classList.toggle('active',i===current));
    tp.forEach((el,i)=>{el.classList.toggle('active',i===current);el.style.animation=el.style.transition=el.style.transform=el.style.opacity=''})}
  // At rest in See inside and Compare the page is held still at the moment its change goes furthest past its place, and the copy at
  // that moment; a change, and Normal, lift the pose, and the page is back at its place before a change starts from there
  function pose(){
    if(look==='normal'||animating||wayBack){unpose();return}const p=plan();
    pages[current].style.transform=`translateX(${p.dip.toFixed(2)}%)`;tp[current].style.transform=`translateX(${p.twin.toFixed(2)}%)`;
    stage.style.setProperty('--past',(-p.dip).toFixed(2)+'%');atLbl.textContent=p.twin>0.5?'Not there yet':'At its place';
    stage.classList.add('posed');posed=true;dotAt(p.at,p.dip);
  }
  function unpose(){if(posed){posed=false;stage.classList.remove('posed');pages[current].style.transform=tp[current].style.transform='';void stage.offsetWidth}}
  // After a change, or once everything is stopped: the copy at rest, the graph for the next change, and the pose
  function views(){twinRest();run=null;if(look==='inside')drawTrace();pose()}
  // The mode switch: draws the mode at once. The shared script then presses Show me (not under reduced motion), which plays it.
  // Leaving Compare stops only the copy: a change of the real site goes on as it was.
  document.addEventListener('hb:mode',e=>{look=e.detail.mode;if(look!=='compare')twinRest();drawTrace();pose();ride()});
  // The graph is drawn in pixels: a new size of the strip (a phone turned), or of its words once the site font has loaded, draws it again
  const redraw=new ResizeObserver(()=>{if(look==='inside')drawTrace()});redraw.observe(trace);redraw.observe(wayLbl);

  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);
      if(look==='inside')drawTrace();pose(); // the views follow the setting at once
    }));
  }
  choices('mode-seg',d=>{mode=d.mode});
  choices('size-seg',d=>{size=+d.size});
  choices('speed-seg',d=>{speed=+d.speed});
  showPage(0);twinRest();
</script>
</body>
</html>