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>Bounce In — Animation Handbook</title>
  <meta name="description" content="Lands with a springy bounce. Best for badges and success messages.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/02-entrance-and-exit/bounce-in/">
  <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="Bounce In — Animation Handbook">
  <meta property="og:description" content="Lands with a springy bounce. Best for badges and success messages.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/02-entrance-and-exit/bounce-in/">
  <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="Bounce In — Animation Handbook">
  <meta name="twitter:description" content="Lands with a springy bounce. Best for badges and success messages.">
  <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": "Bounce In", "name": "Bounce In", "description": "Lands with a springy bounce. Best for badges and success messages.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/02-entrance-and-exit/bounce-in/", "inLanguage": "en", "articleSection": "Entrance & Exit", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Entrance & Exit, Bounce In"}</script>
  <style>
    :root{--ui-accent:#5fd88a;--ui-muted:#8a8a92;--dur:1100ms}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;display:flex;align-items:center;justify-content:center;background:#0b0b0d}
    .card{background:#161b22;border:1px solid #30363d;border-radius:14px;padding:clamp(28px,5vw,48px);text-align:center;max-width:300px;
      opacity:0;transform:scale(0.3) translateY(40px)}
    .card.in{animation:bounce-in var(--dur) ease forwards}
    .card.out{animation:bounce-out calc(var(--dur)*0.5) ease forwards}
    @keyframes bounce-out{from{opacity:1;transform:scale(1) translateY(0)}to{opacity:0;transform:scale(0.4) translateY(30px)}}
    .card-icon{font-size:36px;margin-bottom:14px}
    .card-title{font-size:clamp(16px,2.4vw,22px);font-weight:bold;margin-bottom:8px}
    .card-sub{font-size:11px;color:var(--ui-muted);line-height:1.65}
    /* See inside and Compare. Their labels are plain words on a dark backing. */
    .twin,.ghost,.graph,.side,.when{display:none}
    .lbl{position:absolute;padding:3px 7px;border-radius:5px;background:rgba(11,11,13,.88);color:#f4f4f2;font:600 13px/1.2 var(--hb-font);white-space:nowrap}
    /* See inside: the card beside a graph of its size over time (below it on phones). Across is time, up is size, the dashed line is
       full size; the line is drawn from the same stops as the keyframes and the dot rides it as the card plays. */
    .stage[data-hb-mode="inside"]{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.3fr);grid-template-rows:minmax(0,1fr);
      align-items:center;gap:clamp(12px,3vw,32px);padding:clamp(12px,3vw,32px)}
    .stage[data-hb-mode="inside"] .card{justify-self:center}
    .stage[data-hb-mode="inside"] .graph{display:block}
    .stage[data-hb-mode="inside"]:has(.card.out) .dot{visibility:hidden}
    .graph{position:relative;width:100%;height:100%;max-width:560px;justify-self:center}
    .graph svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
    .graph .axis{fill:none;stroke:rgba(244,244,242,.5);stroke-width:1.5}
    .graph .level{fill:none;stroke:rgba(244,244,242,.6);stroke-width:1.5;stroke-dasharray:6 5}
    .graph .curve{fill:none;stroke:var(--ui-accent);stroke-width:3;stroke-linecap:round;stroke-linejoin:round}
    .graph .dot{fill:#f4f4f2}
    /* Compare: the card twice, each the size it has in Normal, one on each side of a thin line, each inside a dashed outline of full
       size. On the left a copy that grows to full size on one smooth curve; on the right the card itself, which bounces. Between plays
       both are held at the card's biggest moment (its first stop), with those words under them (their row is kept while they are
       hidden, so the cards do not move when they show). */
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:1fr auto 1fr;
      align-items:center;justify-items:center;gap:0 clamp(12px,3vw,32px);padding:48px clamp(8px,2vw,24px) 40px;container-type:size}
    .stage[data-hb-mode="compare"]::after{content:'';position:absolute;top:0;bottom:0;left:50%;border-left:1px solid rgba(244,244,242,.12)}
    .stage[data-hb-mode="compare"] :is(.twin,.ghost,.side,.when){display:block}
    .stage[data-hb-mode="compare"] .card{grid-row:2;grid-column:2}
    .stage[data-hb-mode="compare"] .card.a{grid-column:1}
    .card.twin.in{animation:plain-grow var(--dur) ease-out forwards,plain-fade var(--dur) ease forwards}
    .card.ghost{background:none;border:1px dashed rgba(244,244,242,.6);opacity:1;transform:none;pointer-events:none;position:relative;z-index:2}
    .ghost>*{visibility:hidden}
    .side{top:12px}
    .side.first{left:12px}
    .side.second{left:calc(50% + 12px)}
    .when{position:static;grid-area:3/1/4/3;align-self:start;margin-top:14px;max-width:100%;white-space:normal;text-align:center;text-wrap:balance;
      visibility:hidden}
    .stage[data-hb-mode="compare"].posed .when{visibility:visible}
    .stage[data-hb-mode="compare"].posed .card:not(.ghost){transform:var(--pose)!important;opacity:1!important}
    @media(max-width:600px){
      .stage[data-hb-mode="inside"]{grid-template-columns:minmax(0,1fr);grid-template-rows:auto minmax(0,1fr);gap:8px}
      .stage[data-hb-mode="inside"] .graph{max-width:none}
    }
    /* Phones and short boxes (a window up to 640px tall): compact cards, narrow enough that a strong bounce stays in its half.
       Phones, held either way: no "Dips just under" in the graph. */
    @media(max-width:600px),(max-height:640px){
      .stage[data-hb-mode="inside"] .card,.stage[data-hb-mode="compare"] .card{padding:12px 8px}
      .stage[data-hb-mode="inside"] .card-sub,.stage[data-hb-mode="compare"] .card-sub{display:none}
      .stage[data-hb-mode="inside"] .card-icon,.stage[data-hb-mode="compare"] .card-icon{font-size:22px;margin-bottom:6px}
      .stage[data-hb-mode="inside"] .card-title,.stage[data-hb-mode="compare"] .card-title{font-size:clamp(12px,3.8vw,15px);margin-bottom:0;white-space:nowrap}
    }
    @media(max-width:600px),(max-height:500px){.lbl.dip{display:none}}
    /* Compare's halves also get the compact card when the box is too small for a full-size one at its biggest moment: a card that
       size would cross the middle line, or slide under its half's name. (The box's inside, so its padding is taken off.) */
    @container (max-width:700px),(max-height:275px){
      .stage[data-hb-mode="compare"] .card{padding:12px 8px}
      .stage[data-hb-mode="compare"] .card-sub{display:none}
      .stage[data-hb-mode="compare"] .card-icon{font-size:22px;margin-bottom:6px}
      .stage[data-hb-mode="compare"] .card-title{font-size:clamp(12px,3.8vw,15px);margin-bottom:0;white-space:nowrap}
    }
    @media(prefers-reduced-motion:reduce){.card.in{animation:none!important;opacity:1;transform:none}.card.out{animation:none!important;opacity:0}}
  </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="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="../flip-in/" rel="prev" aria-label="Previous: Flip In"><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>Flip In</span></a><a href="../rotate-in/" rel="next" aria-label="Next: Rotate In"><span><span class="hb-dir">Next: </span>Rotate In</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">02.12 · Entrance &amp; Exit</p>
    <h1>Bounce In</h1>
    <p class="hb-lede">Lands with a springy bounce. Best for badges and success messages.</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="card" id="card">
        <div class="card-icon">◈</div>
        <div class="card-title">Bounce Landing</div>
        <div class="card-sub">A real bounce goes past its size and back, a little less each time.</div>
      </div>
      <div class="graph" aria-hidden="true">
        <span class="lbl full" id="lbl-full" data-hb-caption>Full size</span>
        <span class="lbl big" id="lbl-big" data-hb-caption>Goes about a tenth too big</span>
        <span class="lbl dip" id="lbl-dip" data-hb-caption>Dips just under</span>
        <span class="lbl start" id="lbl-start" data-hb-caption>Starts small and see-through</span>
        <span class="lbl end" id="lbl-end" data-hb-caption>Lands in about a second</span>
        <svg viewBox="0 0 300 170">
          <path class="axis" d="M8 16V140H290"/>
          <path class="level" d="M8 40H290"/>
          <path class="curve" d="M8 140"/>
          <circle class="dot" r="6"/>
        </svg>
      </div>
      <p class="lbl side first" aria-hidden="true" data-hb-caption>No bounce</p>
      <p class="lbl side second" aria-hidden="true" data-hb-caption>Bounces</p>
      <p class="lbl when" id="when" aria-hidden="true" data-hb-caption>At its biggest, about half a second in</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-play" data-hb-replay><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>Replay</button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="loop-tog" data-hb-loop autocomplete="off"><span>Loop</span></label>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the graph shows the card's size over time: it grows past full size, dips just under, then settles. The dot rides the line as the card plays.</p>
      <p data-hb-for="compare" hidden>No bounce grows smoothly to full size and stops; Bounces grows past its outline, dips under and settles, like it landed. With Loop off, both rest at its biggest moment.</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="strength-lbl">Bounce strength</p>
        <div class="seg" id="strength-seg" role="group" aria-labelledby="strength-lbl">
          <button type="button" data-strength="30" aria-pressed="false">Soft</button>
          <button type="button" data-strength="60" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-strength="100" aria-pressed="false">Strong</button>
        </div>
        <p class="hb-hint">How far it grows past full size before settling.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="bounces-lbl">Number of bounces</p>
        <div class="seg" id="bounces-seg" role="group" aria-labelledby="bounces-lbl">
          <button type="button" data-bounces="1" class="on" aria-pressed="true">One</button>
          <button type="button" data-bounces="2" aria-pressed="false">Two</button>
          <button type="button" data-bounces="3" aria-pressed="false">Three</button>
        </div>
        <p class="hb-hint">One usually looks best; more looks cartoonish.</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="1800" aria-pressed="false">Slow</button>
          <button type="button" data-ms="1100" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-ms="650" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">Too fast and the bounce looks like a jitter.</p>
      </div>
    </div>
    <details class="hb-options">
      <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
      <div class="hb-settings">
        <div class="hb-setting">
          <label class="hb-switch-row"><span>Fades in</span><input class="hb-switch" type="checkbox" role="switch" id="fade-tog" checked autocomplete="off"></label>
          <p class="hb-hint">Softens the first moment.</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 bounce-in entrance to [the element you want to animate]. When it appears, it should start small and a little below its final spot, grow slightly past full size, dip just under it, then settle, as if it landed with a small spring. Each bounce is smaller than the one before, like energy running out, and one bounce usually looks best. Unless the settings turn it off, fade it in at the same time. Animate only its position, size and opacity so it stays smooth. If the visitor has reduced motion turned on, show the element without the bounce. 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>Success messages</li><li>Badges</li><li>Small pop-ups</li><li>Playful brands</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Formal content</li><li>Large panels</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 card=document.getElementById('card');
  const stage=card.parentElement;
  const vars=document.documentElement.style;
  const loopTog=document.getElementById('loop-tog'),slowTog=document.getElementById('slow-tog');
  const fadeTog=document.getElementById('fade-tog');
  const kfStyle=document.getElementById('kf-style');
  const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;
  let strength=60,bounces=1,ms=1100,timers=[];

  // The landing as its stops, [share of the time in %, size, lift in px]. The keyframes, the graph and Compare's held moment all
  // read this table, so they always agree.
  const PCT={1:[55,80],2:[40,60,75,90],3:[35,52,65,78,88]},AMP=[.15,-.07,.06,-.03,.03],LIFT=[-8,3,-3,1,-1];
  function stops(t,n){return [[0,.3,40],...PCT[n].map((p,i)=>[p,1+t*AMP[i],LIFT[i]]),[100,1,0]]}

  function dur(){return slowTog.checked?ms*3:ms}
  // bounce-in is the card's own landing. plain-grow and plain-fade are Compare's copy: the same start, one smooth curve to full
  // size, and solid at the same stop as the card.
  function buildKF(intensity,n){
    const fadeIn=fadeTog.checked,s=stops(intensity/100,n);
    const frames=s.map(([p,size,y],i)=>`  ${p}%{${i===0?`opacity:${fadeIn?0:1};`:i===1||i===s.length-1?'opacity:1;':''}transform:scale(${size.toFixed(3)}) translateY(${y}px)}`);
    kfStyle.textContent=`@keyframes bounce-in{\n${frames.join('\n')}\n}
@keyframes plain-grow{from{transform:scale(${s[0][1]}) translateY(${s[0][2]}px)}to{transform:none}}
@keyframes plain-fade{from{opacity:${fadeIn?0:1}}${s[1][0]}%,to{opacity:1}}`;
  }
  function apply(){
    vars.setProperty('--dur',dur()+'ms');
    buildKF(strength,bounces);
  }
  function later(fn,wait){timers.push(setTimeout(fn,wait))}

  // See inside and Compare. The browser's own speed curves (ease between stops, ease-out for the copy), the time the graph keeps
  // after the landing, the dot's radius, how far its words keep off the line (pixels), and the largest size any setting reaches.
  const EASE=[.25,.1,.25,1],OUT=[0,0,.58,1],TAIL=.2,R=6,OFF=14,TOP=1.15;
  const graph=stage.querySelector('.graph'),svg=graph.querySelector('svg'),dot=svg.querySelector('.dot');
  const [axis,level,curve]=['axis','level','curve'].map(c=>svg.querySelector('.'+c));
  const [lblFull,lblBig,lblDip,lblStart,lblEnd]=['full','big','dip','start','end'].map(k=>document.getElementById('lbl-'+k));
  const when=document.getElementById('when');
  // Compare's copy ("No bounce") and the dashed outlines of full size, out of reach of the keyboard and screen readers
  const dup=cls=>{const c=card.cloneNode(true);c.removeAttribute('id');c.className='card '+cls;c.inert=true;c.setAttribute('aria-hidden','true');return c};
  const twin=dup('twin a'),ghostL=dup('ghost a'),ghostR=dup('ghost');
  card.before(twin,ghostL);card.after(ghostR);
  const noteIn=document.querySelector('[data-hb-for="inside"]'),noteCmp=document.querySelector('[data-hb-for="compare"]');
  let mode='normal',runStart=-1e9,runMs=0,dotRaf=0,G={};

  function bounceIn(){
    for(const c of [card,twin]){c.classList.remove('in','out');void c.offsetWidth;c.classList.add('in')}
  }
  // Compare's held moment comes back once a play is over, if Compare is still on show
  function holdLater(){later(()=>{if(mode==='compare')pose(true)},dur()+700)}
  // One play bounces in. While Loop is on it then holds, shrinks out at half the duration, waits
  // and plays again; switching Loop off lets the card finish landed. Slow motion stretches dur(),
  // which the shrink-out is always half of; the 800ms hold and 400ms wait stay fixed.
  // A play lifts Compare's held moment (not under reduced motion: nothing moves then, so it stays).
  function cycle(){
    apply();
    if(motionOk)pose(false);
    runStart=performance.now();runMs=dur();
    bounceIn();
    if(mode==='inside')ride();
    if(!loopTog.checked||!motionOk){
      if(mode==='compare'&&motionOk)holdLater();
      return;
    }
    later(()=>{
      if(!loopTog.checked)return;
      for(const c of [card,twin]){c.classList.remove('in');void c.offsetWidth;c.classList.add('out')}
      later(cycle,Math.round(dur()*0.5)+400);
    },dur()+800);
  }
  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);
  }
  function words(t){return t<=400?'about a third of a second':t<=700?'about half a second':t<=1200?'about a second':t<=2000?'about two seconds':t<=4000?'about three seconds':'about five seconds'}
  // The graph, drawn in the box's own pixels: across is time (the landing, then a little more), up is the card's size from where
  // it starts to the largest any setting reaches, and each step between two stops has the browser's ease. Its words keep their
  // size and stay off the line, so the dot passes them: the biggest one over the first hump, "Full size" above the dashed line and
  // "Lands in" under it at the right, "Dips just under" beside it where it fits, the start's words under the axis.
  function drawGraph(){
    const W=graph.clientWidth,H=graph.clientHeight;
    if(!W)return;
    const s=stops(strength/100,bounces),lh=lblBig.offsetHeight,x0=R+2,x1=W-R-2,yt=lh+OFF;
    const over=s[1][1]-1;
    lblBig.textContent='Goes '+(over<.06?'a little':over>.12?'about a sixth':'about a tenth')+' too big';
    lblStart.textContent=fadeTog.checked?'Starts small and see-through':'Starts small, already solid';
    lblEnd.textContent='Lands in '+words(dur());
    const put=(el,left,top)=>{el.style.left=Math.max(Math.min(left,W-el.offsetWidth),0)+'px';el.style.top=top+'px'};
    const low=lblStart.offsetWidth+lblEnd.offsetWidth+12<=W;   // both words fit under the axis, else "Lands in" goes under the dip
    // Does a word touch the line (or the dot riding it)?
    const onLine=el=>{
      const l=el.offsetLeft-R-3,r=el.offsetLeft+el.offsetWidth+R+3,t=el.offsetTop-R-3,b=el.offsetTop+el.offsetHeight+R+3,len=curve.getTotalLength();
      for(let i=0;i<=240;i++){const q=curve.getPointAtLength(len*i/240);if(q.x>l&&q.x<r&&q.y>t&&q.y<b)return true}
      return false;
    };
    // A second row under the axis for "Lands in" is the last resort: only when its words would cross the line under the dip
    for(const extra of [0,lh+6]){
      const yb=H-lh-OFF-extra;
      const X=p=>x0+(x1-x0)*p/(100*(1+TAIL)),Y=v=>yb-(yb-yt)*(v-s[0][1])/(TOP-s[0][1]);
      G={x0,x1,Y};
      svg.setAttribute('viewBox',`0 0 ${W} ${H}`);
      axis.setAttribute('d',`M${x0} ${yt}V${yb}H${x1}`);
      level.setAttribute('d',`M${x0} ${Y(1)}H${x1}`);
      const [a,b,c,e]=EASE,f=n=>n.toFixed(1);
      let d=`M${f(X(0))} ${f(Y(s[0][1]))}`;
      for(let i=1;i<s.length;i++){
        const [p0,v0]=s[i-1],[p1,v1]=s[i],dx=X(p1)-X(p0),dy=Y(v1)-Y(v0);
        d+=`C${f(X(p0)+dx*a)} ${f(Y(v0)+dy*b)} ${f(X(p0)+dx*c)} ${f(Y(v0)+dy*e)} ${f(X(p1))} ${f(Y(v1))}`;
      }
      curve.setAttribute('d',d+`H${x1}`);
      const row=Y(s[2][1])+OFF;
      put(lblFull,W,Y(1)-lh-8);
      put(lblEnd,W,low?yb+OFF:extra?yb+OFF+lh+6:row);
      put(lblStart,0,yb+OFF);
      const peak=Math.max(Y(s[1][1])-OFF-lh,0);
      put(lblBig,X(s[1][0])-lblBig.offsetWidth/2,peak);
      // the biggest one gives way to "Full size" when they would touch
      if(peak+lh>lblFull.offsetTop-2)put(lblBig,Math.min(lblBig.offsetLeft,lblFull.offsetLeft-lblBig.offsetWidth-6),peak);
      put(lblDip,X(s[2][0])-lblDip.offsetWidth/2,row);
      lblDip.style.visibility=!low&&!extra&&lblDip.offsetLeft+lblDip.offsetWidth+8>lblEnd.offsetLeft?'hidden':'';
      if(low||extra||!onLine(lblEnd))break;
    }
  }
  // The dot: across with the time since the play began, up with the card's real size read from its transform right now. While a
  // play runs the time comes from the card's own animation (the browser starts it a frame or two after the play), so the dot stays
  // on the line; at rest the dot sits at the line's end.
  function placeDot(){
    if(!G.Y)return false;
    const a=motionOk&&card.classList.contains('in')&&card.getAnimations()[0],total=runMs*(1+TAIL);
    if(a&&a.playState==='running'&&a.currentTime!==null)runStart=performance.now()-a.currentTime;
    const t=Math.min(performance.now()-runStart,total),done=!motionOk||t>=total;
    const m=getComputedStyle(card).transform,size=done||m==='none'?1:new DOMMatrixReadOnly(m).a;
    dot.style.transform=`translate(${done?G.x1:G.x0+(G.x1-G.x0)*t/total}px,${G.Y(size)}px)`;
    return !done;
  }
  function ride(){
    cancelAnimationFrame(dotRaf);
    const step=()=>{dotRaf=placeDot()&&mode==='inside'?requestAnimationFrame(step):0};
    step();
  }
  // Compare's held moment: both cards at the share of the time where the real card is biggest (its first stop). The card is at its
  // stop's size; the copy is as far along its own smooth curve as the card is along its time.
  function pose(on){
    stage.classList.toggle('posed',on);
    if(!on)return;
    const s=stops(strength/100,bounces),[p,size,lift]=s[1],k=curveAt(OUT,p/100);
    card.style.setProperty('--pose',`scale(${size.toFixed(3)}) translateY(${lift}px)`);
    twin.style.setProperty('--pose',`scale(${(s[0][1]+(1-s[0][1])*k).toFixed(3)}) translateY(${(s[0][2]*(1-k)).toFixed(1)}px)`);
    when.textContent='At its biggest, '+words(dur()*p/100)+' in';
  }
  // The notes say what the picture shows now: the number of bounces, and still under reduced motion
  function note(el,text){if(el.textContent!==text)el.textContent=text}
  function notes(){
    const many=bounces>1,times=bounces===2?'two':'three';
    note(noteIn,motionOk
      ?`Inside, the graph shows the card's size over time: it ${many?`bounces past full size ${times} times, a little less each time, then settles. The dot rides the line.`:'grows past full size, dips just under, then settles. The dot rides the line as the card plays.'}`
      :`Shown still: the graph shows the card's size over time, ${many?`bouncing past full size ${times} times, a little less each time, and settling.`:'growing past full size, dipping just under and settling.'}`);
    note(noteCmp,motionOk
      ?`No bounce grows smoothly to full size and stops; Bounces ${many?`goes past its outline and back ${times} times, a little less each time.`:'grows past its outline, dips under and settles, like it landed.'} With Loop off, both rest at its biggest moment.`
      :"Shown still, at the bounce's biggest moment: No bounce is still inside its outline, while Bounces pokes past it.");
  }
  function refresh(){
    notes();
    if(mode==='inside'){drawGraph();ride()}
    if(stage.classList.contains('posed'))pose(true);
  }

  // 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;
    pose(mode==='compare'&&(!motionOk||!loopTog.checked));
    refresh();
  });

  // The box changes size with the window, and its words with the site font once it has loaded: the graph is drawn again
  const redraw=new ResizeObserver(()=>{if(mode==='inside')refresh()});redraw.observe(graph);redraw.observe(lblFull);

  document.getElementById('btn-play').addEventListener('click',play);
  // Loop off: a play still landing or holding ends there (and in Compare is then held again)
  loopTog.addEventListener('change',()=>{
    if(loopTog.checked)play();
    else if(card.classList.contains('in')&&mode==='compare'&&motionOk)holdLater();
    refresh();
  });
  slowTog.addEventListener('change',refresh);
  fadeTog.addEventListener('change',refresh);
  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
      refresh();
    }));
  }
  choices('strength-seg',d=>{strength=+d.strength});
  choices('bounces-seg',d=>{bounces=+d.bounces});
  choices('speed-seg',d=>{ms=+d.ms});
  apply();
  refresh();
</script>
</body>
</html>