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.

33.9 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>2D Physics — Animation Handbook</title>
  <meta name="description" content="Balls drop where you click, bounce and pile up. Best for playful intros.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/physics-2d/">
  <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="2D Physics — Animation Handbook">
  <meta property="og:description" content="Balls drop where you click, bounce and pile up. Best for playful intros.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/physics-2d/">
  <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="2D Physics — Animation Handbook">
  <meta name="twitter:description" content="Balls drop where you click, bounce and pile up. Best for playful intros.">
  <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": "2D Physics", "name": "2D Physics", "description": "Balls drop where you click, bounce and pile up. Best for playful intros.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/physics-2d/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, 2D Physics"}</script>
  <style>
    :root{--ui-accent:#3fd6c4;--b1:#ff6f8b;--b2:#ffce5a;--b3:#6ea8ff;--b4:#5fd88a;--b5:#b98cff;--b6:#3fd6c4;--mark:#fff;--hint:rgba(255,255,255,.72)}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow:hidden;background:#0b0b0d}
    canvas{display:block;width:100%;height:100%;touch-action:none;cursor:pointer}
    .hint{position:absolute;inset:0;display:grid;place-items:center;padding:16px;text-align:center;pointer-events:none;
      font-size:clamp(15px,2vw,18px);font-weight:600;color:var(--hint);transition:opacity .3s}
    .hint.gone{opacity:0}
    /* See inside and Compare: plain words over the canvas. See inside's key sits top left with the gravity arrow beside it (drawn on the canvas) */
    .key,.half{display:none}
    .stage[data-hb-mode="inside"] .key{display:flex}
    .stage[data-hb-mode="compare"] .half{display:block}
    .stage[data-hb-mode="inside"] .hint,.stage[data-hb-mode="compare"] .hint{display:none} /* the notes say what to do; on a small phone the hint would sit under See inside's key */
    .lbl{text-wrap:balance;padding:3px 10px;border-radius:12px;background:rgba(11,11,13,.9);color:#f4f4f2;font:600 13px/1.25 var(--hb-font);pointer-events:none}
    .key{position:absolute;left:10px;top:10px;flex-direction:column;align-items:flex-start;gap:5px;max-width:min(320px,70%);padding-left:26px;pointer-events:none}
    .key .asleep{display:none}.key.calm .asleep{display:block}
    .half{position:absolute;top:6px;transform:translateX(-50%);white-space:nowrap}
    .half.first{left:25%}.half.second{left:75%}
    @media(max-width:600px){.lbl{font-size:12px}.key{max-width:48%}.more{display:none}} /* on a phone the key keeps the short words, so it stays clear of the pile */
  </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="../ascii-halftone/" rel="prev" aria-label="Previous: ASCII / Halftone"><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>ASCII / Halftone</span></a><a href="../liquid-glass/" rel="next" aria-label="Next: Liquid Glass"><span><span class="hb-dir">Next: </span>Liquid Glass</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">06.23 · 3D &amp; Advanced</p>
    <h1>2D Physics</h1>
    <p class="hb-lede">Balls drop where you click, bounce and pile up. Best for playful intros.</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 or tap anywhere in the box to drop a ball, drag a ball to throw it, 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 hb-dots" id="stage">
      <canvas id="cvs" tabindex="0" role="button" aria-label="A box of balls: click, tap or press Enter to drop a ball, or drag a ball to throw it"></canvas>
      <p class="hint" id="hint" aria-hidden="true">Click or tap to drop a ball</p>
      <div class="key" aria-hidden="true">
        <p class="lbl kc" data-hb-caption>Gravity speeds every ball down</p>
        <p class="lbl kc" data-hb-caption>Touching balls push apart, never pull</p>
        <p class="lbl kc" data-hb-caption>Lower balls push harder<span class="more">, holding up the ones above</span></p>
        <p class="lbl asleep" data-hb-caption>All still: the steps stop<span class="more"> until something moves</span></p>
      </div>
      <p class="lbl half first" aria-hidden="true" data-hb-caption>Pass through</p>
      <p class="lbl half second" aria-hidden="true" data-hb-caption>Bump and pile</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, gravity speeds every ball down; where two touch (or a ball meets a wall) they push apart, and lower balls push harder, holding up the ones above. Click to drop a ball.</p>
      <p data-hb-for="compare" hidden>Pass through checks only the walls, so its balls sink into each other on the floor; Bump and pile also pushes touching balls apart, so none overlap. Click to drop a ball in both.</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 drop a few balls or press Show me.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="grav-lbl">Gravity</p>
        <div class="seg" id="grav-seg" role="group" aria-labelledby="grav-lbl">
          <button type="button" data-g="0.2" aria-pressed="false">Moon</button>
          <button type="button" data-g="1" class="on" aria-pressed="true">Earth</button>
          <button type="button" data-g="2" aria-pressed="false">Heavy</button>
        </div>
        <p class="hb-hint">How hard the balls are pulled down; on the Moon they float.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="bounce-lbl">Bounciness</p>
        <div class="seg" id="bounce-seg" role="group" aria-labelledby="bounce-lbl">
          <button type="button" data-e="0.2" aria-pressed="false">Low</button>
          <button type="button" data-e="0.5" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-e="0.8" aria-pressed="false">High</button>
        </div>
        <p class="hb-hint">How much of its speed a ball keeps after each bounce.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="size-lbl">Ball sizes</p>
        <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">
          <button type="button" data-mix="0" class="on" aria-pressed="true">All the same</button>
          <button type="button" data-mix="1" aria-pressed="false">Mixed</button>
        </div>
        <p class="hb-hint">Mixed sizes pack into a looser, bumpier pile.</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>Shows the speed</span><input class="hb-switch" type="checkbox" role="switch" id="speed-tog" autocomplete="off"></label>
          <p class="hb-hint">An arrow on each moving ball shows how fast it goes, and where.</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 box of bouncing balls to [the hero or empty area you want to liven up]. A click or tap drops a ball there; it falls, bounces off the walls and each other, and comes to rest in a pile. Dragging a ball with the pointer or a finger picks it up, and letting go throws it at the speed of the drag. Run the physics in fixed steps, as many as the time since the last frame needs, so it behaves the same on every screen, and stop drawing once everything is still. Fade out the oldest ball when there are too many. If the visitor has reduced motion turned on, show each new ball already resting where it would land. 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 intros</li><li>Empty states</li><li>Games and toys</li><li>Kids' and learning sites</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Forms and checkout</li><li>Serious content</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 cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.getElementById('stage'),hint=document.getElementById('hint'),slowTog=document.getElementById('slow-tog');
// Phones are screens up to 600px wide, or up to 500px tall when held sideways: they keep fewer balls
const reduce=matchMedia('(prefers-reduced-motion:reduce)'),phone=matchMedia('(max-width:600px),(max-height:500px)');
const css=getComputedStyle(document.documentElement),COLORS=[1,2,3,4,5,6].map(n=>css.getPropertyValue('--b'+n)),MARK=css.getPropertyValue('--mark');
const FRAME=1000/60,FADE=300,RING=450; // one physics step is 1/60 s; a fade and a tap ring last 300 and 450 ms
const WALL={vx:0,vy:0,im:0}; // the walls and floor never move
const clamp=(v,a,b)=>Math.max(a,Math.min(b,v));
let W=0,H=0,R0=20,balls=[],rings=[],made=0,held=null,raf=null,lastT=null,behind=0,clock=0,awake=false,calm=0,runTimers=[];
// See inside and Compare: the mode on show; the contacts of the last step; Compare's two worlds (null outside Compare), each with its own
// balls, rings, held ball, sleep and contacts, and "pairs" (whether balls push each other); Normal's own state stays where it is
let mode='normal',contacts=[],pairs=true,halves=null,own=false,flag=false,keyCalm=false,fadeMs=0,kept=null;
let GRAV=1,E=.5,MIX=false,SHOW=false;const target={x:0,y:0},grab={x:0,y:0},pv={x:0,y:0},last={x:0,y:0,t:0};

// One physics step of 1/60 s. Gravity, then the contacts: touching pairs and the four walls. Each contact pushes only apart,
// and one that closes faster than 1 px a step bounces back with E of its speed (slower ones just rest). Eight passes over all
// contacts share the pushes (and the friction, at most half the push) through a pile; then the balls move, overlaps left
// over are eased apart and everything is kept inside the box. A held ball follows the pointer and nothing pushes it.
function step(){
  const g=GRAV*H/1000,cs=contacts=[],im=b=>b===held?0:b.im; // contacts: the list See inside draws
  for(const b of balls)if(b!==held)b.vy+=g;
  if(held){held.vx=clamp(target.x,held.r,W-held.r)-held.x;held.vy=clamp(target.y,held.r,H-held.r)-held.y;}
  const touch=(a,b,nx,ny)=>{const vn=(b.vx-a.vx)*nx+(b.vy-a.vy)*ny;cs.push({a,b,nx,ny,t:vn<-1?-E*vn:0,j:0,jt:0});};
  for(let i=0,a;a=balls[i];i++){
    if(pairs)for(let k=i+1;k<balls.length;k++){const b=balls[k],dx=b.x-a.x,dy=b.y-a.y,d=Math.hypot(dx,dy);if(d>0&&d<a.r+b.r+.5)touch(a,b,dx/d,dy/d);}
    if(a.x-a.r<.5)touch(a,WALL,-1,0);if(a.x+a.r>W-.5)touch(a,WALL,1,0);
    if(a.y-a.r<.5)touch(a,WALL,0,-1);if(a.y+a.r>H-.5)touch(a,WALL,0,1);
  }
  for(let it=0;it<8;it++)for(const c of cs){
    const {a,b,nx,ny}=c,ia=im(a),ib=im(b),s=ia+ib;if(!s)continue;
    const j=Math.max(0,c.j+(c.t-((b.vx-a.vx)*nx+(b.vy-a.vy)*ny))/s),dj=j-c.j;c.j=j;
    const f=.5*c.j,jt=clamp(c.jt-((b.vx-a.vx)*-ny+(b.vy-a.vy)*nx)/s,-f,f),dt=jt-c.jt;c.jt=jt;
    const px=dj*nx-dt*ny,py=dj*ny+dt*nx;
    a.vx-=px*ia;a.vy-=py*ia;b.vx+=px*ib;b.vy+=py*ib;
  }
  const vmax=R0*.9; // a ball never moves more than about one radius a step, so it cannot pass through another
  for(const b of balls){
    const s=Math.hypot(b.vx,b.vy);if(s>vmax&&b!==held){b.vx*=vmax/s;b.vy*=vmax/s;}
    b.x+=b.vx;b.y+=b.vy;b.a+=b.vx/b.r;
  }
  for(let it=0;it<3;it++){
    if(pairs)for(let i=0;i<balls.length;i++)for(let k=i+1;k<balls.length;k++){
      const a=balls[i],b=balls[k],dx=b.x-a.x,dy=b.y-a.y,d=Math.hypot(dx,dy)||.01,o=a.r+b.r-d,ia=im(a),ib=im(b);
      if(o<=.3||!(ia+ib))continue;
      const m=(o-.3)*.6/(ia+ib)/d;a.x-=dx*m*ia;a.y-=dy*m*ia;b.x+=dx*m*ib;b.y+=dy*m*ib;
    }
    for(const b of balls){b.x=clamp(b.x,b.r,W-b.r);b.y=clamp(b.y,b.r,H-b.r);}
  }
  // Everything still for 45 steps (3/4 s): the balls sleep and the loop stops drawing
  let top=0;for(const b of balls)if(b!==held)top=Math.max(top,b.vx*b.vx+b.vy*b.vy);
  calm=held||top>.02?0:calm+1;
  if(calm>45){awake=false;for(const b of balls)b.vx=b.vy=0;}
}
// Under reduced motion nothing falls on screen: the steps run at once, out of sight, and the result is shown
function settle(){awake=true;calm=0;for(let i=0;i<900&&awake;i++)step();awake=false;for(const b of balls)b.vx=b.vy=0;}

function draw(){ctx.clearRect(0,0,W,H);scene();if(mode==='inside')inside();}
function scene(){
  ctx.strokeStyle=ctx.fillStyle=MARK;ctx.lineWidth=2;
  for(const r of rings){const p=(clock-r.t)/RING;ctx.globalAlpha=(1-p)*.7;ctx.beginPath();ctx.arc(r.x,r.y,6+R0*1.2*p,0,Math.PI*2);ctx.stroke();}
  for(const b of balls){
    ctx.globalAlpha=b.al;ctx.fillStyle=b.c;ctx.beginPath();ctx.arc(b.x,b.y,b.r,0,Math.PI*2);ctx.fill();
    ctx.globalAlpha=b.al*.55;ctx.fillStyle=MARK;ctx.beginPath(); // a spot on the ball turns as it rolls
    ctx.arc(b.x+Math.cos(b.a)*b.r*.55,b.y+Math.sin(b.a)*b.r*.55,b.r*.16,0,Math.PI*2);ctx.fill();
    const s=Math.hypot(b.vx,b.vy);if(!SHOW||s<.3)continue;
    const L=Math.min(s*6,b.r*3),ux=b.vx/s,uy=b.vy/s,ex=b.x+ux*L,ey=b.y+uy*L;
    ctx.globalAlpha=b.al;ctx.beginPath();ctx.moveTo(b.x,b.y);ctx.lineTo(ex,ey);ctx.stroke();
    ctx.beginPath();ctx.moveTo(ex+ux*6,ey+uy*6);ctx.lineTo(ex-uy*4,ey+ux*4);ctx.lineTo(ex+uy*4,ey-ux*4);ctx.fill();
  }
  ctx.globalAlpha=1;
}
// Balls fade in (under reduced motion) and out (Reset, the end of Show me, the oldest of too many); a faded-out ball goes
function fade(ms){
  let gone=false;
  for(const b of balls)if(b.al!==b.to){b.al=b.to>b.al?Math.min(b.to,b.al+ms/FADE):Math.max(b.to,b.al-ms/FADE);if(!b.al&&!b.to)gone=true;}
  rings=rings.filter(r=>clock-r.t<RING);
  if(!gone)return;balls=balls.filter(b=>b.al||b.to);
  contacts=contacts.filter(c=>balls.includes(c.a)&&(c.b===WALL||balls.includes(c.b)));
  if(held&&!balls.includes(held))held=null;
  if(reduce.matches)settle();else wake(); // the balls that rested on it fall into the gap
}
const showHint=()=>hint.classList.toggle('gone',balls.some(b=>b.to>0));

// The loop draws at most once per 16 ms and runs as many 1/60 s steps as the time since the last drawn frame calls for (a third
// of it in slow motion; at most 50 ms and 3 steps a frame). It stops once the balls sleep and nothing fades, and wake() starts it.
function loop(ts){
  raf=requestAnimationFrame(loop);
  if(lastT===null){lastT=ts;return;}
  const dt=ts-lastT;if(dt<16)return;
  lastT=ts;
  const ms=Math.min(dt,50)*(slowTog.checked?1/3:1);
  clock+=ms;
  if(halves)return compareFrame(ms);
  if(awake){behind+=ms;for(let n=0;behind>=FRAME-1&&n<3;n++){step();behind-=FRAME;}}
  fade(ms);draw();
  if(!awake&&!held&&!rings.length&&balls.every(b=>b.al===b.to)){cancelAnimationFrame(raf);raf=null;}
}
function kick(){if(raf===null){lastT=null;behind=0;raf=requestAnimationFrame(loop);}}
function wake(){if(!reduce.matches){awake=true;calm=0;}kick();}
document.addEventListener('visibilitychange',()=>{lastT=null;behind=0;});

// A new ball at (x, y), in one size or a mix of sizes. Past the limit the oldest ball fades out.
// In Compare it drops one ball, the same size and color, at the same spot of both halves (half the limit each). k: a size that Mixed
// uses instead of a random one; pose: a ball a view's Show me run drops, which fades when Normal comes back.
const put=(x,y,r,c,pose)=>balls.push({x:clamp(x,r,W-r),y:clamp(y,r,H-r),vx:0,vy:0,r,im:(R0/r)**2,a:0,al:reduce.matches?0:1,to:1,c,pose});
function drop(x,y,k,pose){
  const r=R0*(MIX?k||.6+Math.random()*.8:1),c=COLORS[made++%COLORS.length];
  each(()=>{
    put(x,y,r,c,pose);
    const live=balls.filter(b=>b.to>0);
    if(live.length>(phone.matches?24:40)/(halves?2:1))live[0].to=0;
    if(reduce.matches)settle();else rings.push({x,y,t:clock});
    showHint();wake();
  });
}

// Pointer Events cover mouse, touch and pen. Pressing a ball picks up the nearest one in reach (its radius plus 8px, and at least
// a 44px circle); pressing anywhere else drops a new one. While a ball is held, the pointer's speed is followed (in px per
// 1/60 s), and letting go throws the ball with it if the pointer was still moving.
// In Compare the pointer is read in the second half's own place (x below 0 is the first half, which never grabs a ball), and a grab,
// move or release works on that half's world.
const local=e=>{const r=cvs.getBoundingClientRect(),x=e.clientX-r.left,y=e.clientY-r.top;return halves?{x:x-r.width/2,y:y-TOP}:{x,y};};
function grabAt(p,e){
  let b=null,best=Infinity;
  for(const o of balls){const d=Math.hypot(o.x-p.x,o.y-p.y);if(o.to>0&&d<=Math.max(o.r+8,22)&&d-o.r<best){best=d-o.r;b=o;}}
  if(!b)return false;
  held=b;grab.x=b.x-p.x;grab.y=b.y-p.y;target.x=b.x;target.y=b.y;pv.x=pv.y=0;Object.assign(last,p,{t:e.timeStamp});
  try{cvs.setPointerCapture(e.pointerId);}catch(_){}
  wake();return true;
}
function pmove(e){
  if(!held||!e.isPrimary)return;
  const p=local(e),dt=Math.max(1,e.timeStamp-last.t);
  pv.x=pv.x*.4+(p.x-last.x)/dt*FRAME*.6;pv.y=pv.y*.4+(p.y-last.y)/dt*FRAME*.6;Object.assign(last,p,{t:e.timeStamp});
  target.x=p.x+grab.x;target.y=p.y+grab.y;
  if(reduce.matches){held.x=clamp(target.x,held.r,W-held.r);held.y=clamp(target.y,held.r,H-held.r);return true;} // drawn by the listener, outside Compare's half
}
function release(e){
  if(!held||!e.isPrimary)return;
  const s=Math.hypot(pv.x,pv.y),k=e.timeStamp-last.t<80&&s?Math.min(1,R0*.9/s):0;
  held.vx=pv.x*k;held.vy=pv.y*k;held=null;
  if(reduce.matches){settle();kick();}else wake();
}
cvs.addEventListener('pointerdown',e=>{
  if(!e.isPrimary||e.button!==0)return;
  const p=local(e),first=halves&&p.x<0;
  if(!first&&second(grabAt,p,e))return;
  own=true;drop(first?p.x+cvs.clientWidth/2:p.x,p.y);
});
cvs.addEventListener('pointermove',e=>{if(second(pmove,e))redraw();});
['pointerup','pointercancel','lostpointercapture'].forEach(t=>cvs.addEventListener(t,e=>second(release,e)));
// The keyboard drops a ball at a random spot near the top
cvs.addEventListener('keydown',e=>{if((e.key==='Enter'||e.key===' ')&&!e.repeat){e.preventDefault();own=true;const[bw,bh]=box();drop(bw*(.2+Math.random()*.6),bh*.15);}});

// See inside and Compare. A world is the state of one box: Normal's own sits in the variables above; Compare's two (Pass through, then
// Bump and pile) are objects that take the variables' place for a moment (inWorld), so Normal's balls, rings, held ball and sleep stay
// untouched while Compare shows. Both take the same fixed steps from the one "behind", and each sleeps on its own.
const TOP=34; // Compare's worlds start below the halves' names
const world=pairs=>({balls:[],rings:[],held:null,awake:false,calm:0,contacts:[],pairs,w:W/2,h:H-TOP});
function inWorld(o,fn,p,q){
  const sb=balls,sr=rings,sh=held,sa=awake,sc=calm,sk=contacts,sp=pairs,sw=W,sH=H;
  ({balls,rings,held,awake,calm,contacts,pairs,w:W,h:H}=o);
  const out=fn(p,q);
  o.balls=balls;o.rings=rings;o.held=held;o.awake=awake;o.calm=calm;o.contacts=contacts;
  balls=sb;rings=sr;held=sh;awake=sa;calm=sc;contacts=sk;pairs=sp;W=sw;H=sH;
  return out;
}
const each=fn=>{if(halves)for(const o of halves)inWorld(o,fn);else fn();}; // fn works on the box on show: Compare's two halves, else the real one
const second=(fn,p,q)=>halves?inWorld(halves[1],fn,p,q):fn(p,q); // ... or on the half the visitor can grab from
const box=()=>halves?[W/2,H-TOP,W/4]:[W,H,W*(mode==='inside'&&phone.matches?.62:.5)]; // width, height and where a run drops (right of See inside's key on a phone)
const filled=()=>halves?halves.some(o=>o.balls.some(isLive)):balls.some(isLive);
const clearWorld=()=>{balls=[];rings=[];held=null;awake=false;contacts=[];};
const wakeIfAny=()=>{if(balls.length)wake();},settleIfAny=()=>{if(balls.length)settle();};
const isAwake=o=>o.awake,isLive=b=>b.to>0,stepAwake=()=>{if(awake)step();},fadeNow=()=>fade(fadeMs),isIdle=o=>!o.awake&&!o.held&&!o.rings.length&&o.balls.every(b=>b.al===b.to);
const redraw=()=>halves?drawHalves():draw();
function compareFrame(ms){
  if(halves.some(isAwake)){behind+=ms;for(let n=0;behind>=FRAME-1&&n<3;n++){for(const o of halves)inWorld(o,stepAwake);behind-=FRAME;}}
  fadeMs=ms;for(const o of halves)inWorld(o,fadeNow);
  drawHalves();
  if(halves.every(isIdle)){cancelAnimationFrame(raf);raf=null;}
}
// One canvas split down the middle, each half clipped to itself, with a thin line between
function drawHalves(){
  const hw=W/2;ctx.clearRect(0,0,W,H);
  for(let i=0;i<2;i++){ctx.save();ctx.beginPath();ctx.rect(i*hw,0,hw,H);ctx.clip();ctx.translate(i*hw,TOP);inWorld(halves[i],scene);ctx.restore();}
  ctx.fillStyle='#3a3c46';ctx.fillRect(hw-1,0,2,H);
}
// See inside, over the balls: a white line through each touching point of the last step, along the push, as long as the push it carried
// (one ball's weight on Earth is about 3/4 of a radius; a line never passes one and a half), with a head at each end pointing apart, or one head
// going into the ball for a wall or the floor; and the gravity arrow beside the key, as long as Gravity is strong. A dark edge keeps
// the lines clear on any ball.
function head(x,y,dx,dy,s=6){ctx.moveTo(x-dx*s-dy*s*.7,y-dy*s+dx*s*.7);ctx.lineTo(x,y);ctx.lineTo(x-dx*s+dy*s*.7,y-dy*s-dx*s*.7);}
function inside(){
  const unit=H/1000,calmNow=!awake&&!held&&balls.some(isLive);
  if(calmNow!==keyCalm){keyCalm=calmNow;key.classList.toggle('calm',calmNow);}
  ctx.lineCap=ctx.lineJoin='round';ctx.beginPath();
  for(const c of contacts){
    const{a,b,nx,ny}=c;if(c.j<=.001||Math.min(a.al,b.al??1)<.5)continue;
    const wall=b===WALL,L=clamp(R0*(.3+c.j/unit*.45),wall?8:R0*.9,R0*1.6),x=a.x+nx*a.r,y=a.y+ny*a.r;
    const hs=Math.min(6,L*.3);
    if(wall){ctx.moveTo(x,y);ctx.lineTo(x-nx*L,y-ny*L);head(x-nx*L,y-ny*L,-nx,-ny,hs);continue;}
    ctx.moveTo(x-nx*L/2,y-ny*L/2);ctx.lineTo(x+nx*L/2,y+ny*L/2);head(x+nx*L/2,y+ny*L/2,nx,ny,hs);head(x-nx*L/2,y-ny*L/2,-nx,-ny,hs);
  }
  const g=R0*(.7+.85*GRAV);ctx.moveTo(22,14);ctx.lineTo(22,14+g);head(22,14+g,0,1);
  ctx.strokeStyle='rgba(11,11,13,.85)';ctx.lineWidth=6;ctx.stroke();ctx.strokeStyle='#fff';ctx.lineWidth=2.5;ctx.stroke();
}
const key=stage.querySelector('.key');
const NAMES={normal:cvs.getAttribute('aria-label')};
NAMES.inside=NAMES.normal+'; inside, arrows show gravity and the pushes where balls touch';
NAMES.compare=NAMES.normal+'; two boxes side by side, balls that pass through each other and balls that pile up';
// The mode switch draws the mode at once. Leaving a mode stops its run; Compare starts two empty worlds and keeps Normal's color count,
// clock and steps owed; Normal fades out the balls a view's run posed (the visitor's own stay). Under reduced motion an empty box gets
// the plan's balls resting at once. The flag marks the Show me press that follows as the visitor's (cleared once it has run).
document.addEventListener('hb:mode',e=>{
  const was=mode;mode=e.detail.mode;
  flag=true;queueMicrotask(()=>{flag=false;});
  cvs.setAttribute('aria-label',NAMES[mode]);
  if(mode!==was){
    if(was==='normal'&&runTimers.length)for(const b of balls)b.to=0; // a Show me run cut short by a view ends as it would have: its balls fade
    stopRun();
    if(was==='compare'){made=kept.made;clock=kept.clock;behind=kept.behind;halves=null;}
    if(mode==='compare'){kept={made,clock,behind};made=behind=0;halves=[world(false),world(true)];}
    if(mode==='normal')for(const b of balls)if(b.pose)b.to=0;
  }
  if(mode!=='normal'&&reduce.matches&&!filled())deal();
  showHint();redraw();kick();
});
function deal(){ // the run's own plan, out of sight: a ball every 18 steps (0.3 s), then the steps run until all sleep
  made=0;own=false;each(clearWorld);const[bw,bh,cx]=box(); // balls still fading out go first, or they would stay unseen but solid
  for(const[x,y,k]of PLAN.concat(MORE)){const r=R0*(MIX?k:1),c=COLORS[made++%COLORS.length];each(()=>{put(cx+R0*x,bh*y,r,c,true);for(let n=0;n<18;n++)step();});}
  each(()=>{settle();for(const b of balls)b.al=b.to;});showHint();
}
// A setting changed while a view shows: Compare's balls feel a new Gravity at once; under reduced motion the steps run out of sight, or
// the plan's balls are dealt again when the visitor has dropped none
function changed(){
  if(mode==='normal')return;
  if(!reduce.matches){if(halves)each(wakeIfAny);}
  else if(own||!filled())each(settleIfAny);
  else{each(clearWorld);deal();}
  redraw();
}
// The notes as the markup writes them, and the same two under reduced motion, where nothing moves
const NOTES=[[document.querySelector('[data-hb-for="inside"]'),'Shown still: gravity pulls every ball down; where two touch (or a ball meets a wall) they push apart, and lower balls push harder, holding up the ones above. Click to drop a ball.'],
  [document.querySelector('[data-hb-for="compare"]'),'Shown still: Pass through lets balls sink into each other on the floor; Bump and pile pushes touching balls apart, so none overlap. Click to drop a ball in both.']];
for(const n of NOTES)n.push(n[0].textContent);
function notes(){for(const[el,still,moving]of NOTES){const t=reduce.matches?still:moving;if(el.textContent!==t)el.textContent=t;}}
reduce.addEventListener('change',notes);notes();

// Show me: five clicks' worth, 0.3 s apart and a ball's width or less from the middle, so each new ball lands on the ones before
// and they pile up; they fade out 1.7 s after the last one, so the box is empty again.
// Slow motion stretches the waits three times. Every timer of the run goes through later(); stopRun() cancels the rest of it.
function later(fn,ms){const id=setTimeout(()=>{runTimers=runTimers.filter(t=>t!==id);fn();},ms*(slowTog.checked?3:1));runTimers.push(id);}
function stopRun(){runTimers.forEach(clearTimeout);runTimers=[];}
// The press on arrival leaves alone a box the visitor has already filled (in a view, the box that run would clear); the press a mode
// sends right after its event counts as the visitor's own (flag). In See inside and Compare the run clears the box, drops the plan
// with fixed sizes and colors (the page's five balls and three more, so the pile is deep enough to show weight pressing down)
// and keeps them, so the view rests on a pile.
const PLAN=[[0,.18,1.15],[1.1,.12,.7],[-1.2,.22,1.3],[.5,.16,.85],[-.4,.1,1]],MORE=[[.8,.14,.9],[-.7,.2,1.1],[.1,.12,.95]];
document.getElementById('btn-demo').addEventListener('click',e=>{
  if(!e.isTrusted&&!flag&&filled())return;
  stopRun();each(clearWorld);own=false;showHint();redraw();
  const pose=mode!=='normal';if(pose)made=0;
  (pose?PLAN.concat(MORE):PLAN).forEach(([x,y,k],i)=>later(()=>{const[bw,bh,cx]=box();drop(cx+R0*x,bh*y,pose&&k,pose);},300*i));
  if(!pose)later(()=>{balls.forEach(b=>{b.to=0;});showHint();kick();},2900);
});
document.getElementById('btn-reset').addEventListener('click',()=>{stopRun();each(()=>{held=null;balls.forEach(b=>{b.to=0;});});showHint();kick();});
// The visitor pressed, typed or touched inside the stage: the run stops and its balls stay for the visitor to play with
document.addEventListener('hb:input',()=>{if(runTimers.length)stopRun();});

// Setting the canvas size wipes it, so a real change of the stage's size draws the balls again at once. Their places stretch with
// the box (the next step keeps them inside it), so a size that comes back finds them where they were. A resize that leaves the
// stage as it was (a phone's address bar sliding in or out) does nothing.
function resize(){
  const w=stage.clientWidth,h=stage.clientHeight;
  if(w===W&&h===H)return;
  if(W&&H)for(const b of balls){b.x*=w/W;b.y*=h/H;}
  const dpr=Math.min(window.devicePixelRatio||1,2);
  W=w;H=h;R0=clamp(Math.min(W,H)*.055,12,22);
  cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);ctx.setTransform(dpr,0,0,dpr,0,0);
  if(balls.length){wake();if(reduce.matches&&mode==='inside')settle();}
  if(halves)for(const o of halves){ // Compare's worlds stretch with the stage too
    const nw=W/2,nh=H-TOP;for(const b of o.balls){b.x*=nw/o.w;b.y*=nh/o.h;}
    o.w=nw;o.h=nh;inWorld(o,reduce.matches?settleIfAny:wakeIfAny);
  }
  redraw();
}

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);
  }));
}
// The next drop, throw or Show me shows a change; a new gravity also wakes the balls already in the box
choices('grav-seg',d=>{GRAV=+d.g;if(balls.length)wake();changed();});
choices('bounce-seg',d=>{E=+d.e;changed();});
choices('size-seg',d=>{MIX=d.mix==='1';changed();});
document.getElementById('speed-tog').addEventListener('change',e=>{SHOW=e.target.checked;redraw();});

resize();
window.addEventListener('resize',resize);
</script>
</body>
</html>