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.

24.5 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>Particle Constellation — Animation Handbook</title>
  <meta name="description" content="Drifting dots link up with lines whenever they come close. Best for tech sites.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/particle-constellation/">
  <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="Particle Constellation — Animation Handbook">
  <meta property="og:description" content="Drifting dots link up with lines whenever they come close. Best for tech sites.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/particle-constellation/">
  <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="Particle Constellation — Animation Handbook">
  <meta name="twitter:description" content="Drifting dots link up with lines whenever they come close. Best for tech sites.">
  <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": "Particle Constellation", "name": "Particle Constellation", "description": "Drifting dots link up with lines whenever they come close. Best for tech sites.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/particle-constellation/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Particle Constellation"}</script>
  <style>
    :root{--ui-accent:#ffce5a}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow:hidden;background:#06070c}
    canvas{display:block;width:100%;height:100%}
    /* See inside and Compare: plain words over the picture; the rings, links and arrow are drawn on the canvas itself */
    .caps,.pull,.side{display:none;position:absolute}
    .lbl{padding:4px 9px;border-radius:999px;background:rgba(6,7,12,.9);color:#f4f4f2;font:600 13px/1.2 var(--hb-font);white-space:nowrap}
    .caps{left:10px;top:10px;flex-direction:column;align-items:flex-start;gap:6px}
    .pull{left:10px;bottom:10px}
    .side{top:10px}
    .side.first{left:10px}
    .side.second{left:calc(50% + 10px)}
    .stage[data-hb-mode="inside"] .caps{display:flex}
    .stage[data-hb-mode="inside"] .pull:not([hidden]),.stage[data-hb-mode="compare"] .side{display:block}
    @media(max-width:360px){.lbl{font-size:12px;padding:3px 7px}.side.second{left:calc(50% + 6px)}}
  </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="loop">
<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="../abstract-geometric-motion/" rel="prev" aria-label="Previous: Abstract Geometric Motion"><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>Abstract Geometric Motion</span></a><a href="../flow-field/" rel="next" aria-label="Next: Flow Field"><span><span class="hb-dir">Next: </span>Flow Field</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">07.13 · Ambient &amp; Background</p>
    <h1>Particle Constellation</h1>
    <p class="hb-lede">Drifting dots link up with lines whenever they come close. Best for tech sites.</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 moves by itself, and the dots gather toward your pointer or finger. Pause it to look closely, or turn on slow motion.</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">
      <canvas id="cvs" aria-hidden="true"></canvas>
      <div class="caps" aria-hidden="true">
        <p class="lbl in" data-hb-caption>Ring: any dot inside it gets a line</p>
        <p class="lbl in" data-hb-caption>Closer dots, brighter line</p>
      </div>
      <p class="lbl pull" aria-hidden="true" data-hb-caption hidden>Pointer: dots inside drift toward it</p>
      <p class="lbl side first" aria-hidden="true" data-hb-caption>Hard lines</p>
      <p class="lbl side second" aria-hidden="true" data-hb-caption>Fading lines</p>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-pause" data-hb-pause data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></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 ring around one dot shows how far it reaches: any dot inside gets a line, brighter the closer it is. The arrow shows which way that dot drifts.</p>
      <p data-hb-for="compare" hidden>Hard lines joins close dots at full strength, so lines blink on and off. Fading lines dims each line as its dots part, so the web forms and breaks smoothly.</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 see the difference as it moves.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="count-lbl">Number of dots</p>
        <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">
          <button type="button" data-n="50" aria-pressed="false">Few</button>
          <button type="button" data-n="90" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-n="140" aria-pressed="false">Many</button>
        </div>
        <p class="hb-hint">More dots, more links; phones show at most 60.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="link-lbl">Link distance</p>
        <div class="seg" id="link-seg" role="group" aria-labelledby="link-lbl">
          <button type="button" data-px="90" aria-pressed="false">Short</button>
          <button type="button" data-px="130" class="on" aria-pressed="true">Medium</button>
          <button type="button" data-px="180" aria-pressed="false">Long</button>
        </div>
        <p class="hb-hint">How close two dots must be to link up.</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-spd="0.35" aria-pressed="false">Slow</button>
          <button type="button" data-spd="0.6" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-spd="1" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">Slow drifting stays calm.</p>
      </div>
    </div>
    <details class="hb-options">
      <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
      <div class="hb-settings">
        <div class="hb-setting">
          <p class="hb-setting-name" id="color-lbl">Color</p>
          <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
            <button type="button" class="on" aria-pressed="true" aria-label="Cyan" data-col="cyan" style="background:#5ad1ff"></button>
            <button type="button" aria-pressed="false" aria-label="Violet" data-col="violet" style="background:#a882ff"></button>
            <button type="button" aria-pressed="false" aria-label="Mint" data-col="mint" style="background:#7affc8"></button>
            <button type="button" aria-pressed="false" aria-label="Gray" data-col="mono" style="background:#dcdce1"></button>
          </div>
          <p class="hb-hint">The color of the dots and lines.</p>
        </div>
        <div class="hb-setting">
          <label class="hb-switch-row"><span>Dots follow the pointer</span><input class="hb-switch" type="checkbox" role="switch" id="mouse-tog" checked autocomplete="off"></label>
          <p class="hb-hint">Dots gather where you point or touch.</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 particle constellation background to [the section you want it behind]. Scatter dots on a canvas, let each drift slowly in its own direction and bounce off the edges, and on every frame join any two dots that are close with a thin line that fades as they move apart, so a network keeps forming and breaking. When the settings include it, let the dots drift toward the pointer, or toward a finger on touch screens. Checking every pair of dots gets slow fast, so use fewer dots on phones. If the visitor has reduced motion turned on, show one still frame. 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>Tech and data sites</li><li>Hero sections</li><li>Network and security products</li><li>Loading screens</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Busy layouts</li><li>Long reading pages</li></ul>
    </div>
  </section>

  <section class="hb-related" aria-labelledby="hb-related-title" hidden>
    <h2 id="hb-related-title">Similar animations</h2>
    <div class="hb-rel-list"></div>
  </section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
  const stage=document.querySelector('.stage');
  const slowTog=document.getElementById('slow-tog');
  const PAL={cyan:[90,209,255],violet:[168,130,255],mint:[122,255,200],mono:[220,220,225]};
  const PHONE_DOTS=60; // phones and short screens draw at most this many dots: every pair is tested each frame
  const phone=matchMedia('(max-width:600px),(max-height:500px)');
  const FRAME=1000/60; // one step is what a 60 Hz frame adds, so a faster screen or a slow frame does not change the speed
  let W=0,H=0,raf=null,lastT=null,paused=false;
  let COUNT=90,LINK=130,SPD=0.6,COL='cyan',MOUSE=true;
  let nodes=[],mx=-999,my=-999;
  // See inside and Compare. follow: the dot See inside tracks (a reference, dropped when the dots are dealt again).
  const PULL2=26000; // the pointer pulls the dots within this squared distance
  const TAU=Math.PI*2,GOLD='#ffce5a',INK='#f4f4f2',DASH=[6,6],SOLID=[];
  const ARROW=150; // the arrow is as long as the dot drifts in 2.5 seconds (150 sixtieths of a second)
  const pullCap=document.querySelector('.pull'),cmpNote=document.querySelector('[data-hb-for="compare"]');
  const MOVING=cmpNote.textContent; // the markup's words: lines blink on and off. Reduced motion, paused, shows the still words.
  const STILL='Shown still: Hard lines joins every close pair at full strength, a harsh web; Fading lines dims each line as its dots part, so the long links are faint.';
  const reduced=matchMedia('(prefers-reduced-motion: reduce)');
  let mode='normal',follow=null;

  function init(){
    const n=phone.matches?Math.min(COUNT,PHONE_DOTS):COUNT;
    nodes=Array.from({length:n},()=>{
      const vx=Math.random()-.5,vy=Math.random()-.5;
      // cvx and cvy are the velocity the dot cruises at and eases back to, so the drift never dies
      return{x:Math.random()*W,y:Math.random()*H,vx,vy,cvx:vx,cvy:vy,r:Math.random()*1.4+1};
    });
    follow=null;
  }

  // k is how many 60 Hz frames this step stands for (1 at 60 Hz, 2 at 30 Hz), or a third of that in slow motion: the drift,
  // the pull toward the pointer and the easing back to the cruising velocity all grow and shrink with it
  function step(k){
    for(const p of nodes){
      p.x+=p.vx*SPD*k;p.y+=p.vy*SPD*k;
      if(MOUSE&&mx>-500){
        const dx=mx-p.x,dy=my-p.y,d2=dx*dx+dy*dy;
        if(d2<PULL2&&d2>1){const f=.04/Math.sqrt(d2)*k;p.vx+=dx*f;p.vy+=dy*f;}
      }
      p.vx+=(p.cvx-p.vx)*.01*k;p.vy+=(p.cvy-p.vy)*.01*k;
      // a bounce points the velocity and the cruising velocity away from the wall, so a dot pulled against it cannot turn back in
      if(p.x<0){p.vx=Math.abs(p.vx);p.cvx=Math.abs(p.cvx);}else if(p.x>W){p.vx=-Math.abs(p.vx);p.cvx=-Math.abs(p.cvx);}
      if(p.y<0){p.vy=Math.abs(p.vy);p.cvy=Math.abs(p.cvy);}else if(p.y>H){p.vy=-Math.abs(p.vy);p.cvy=-Math.abs(p.cvy);}
      p.x=Math.max(0,Math.min(W,p.x));p.y=Math.max(0,Math.min(H,p.y));
    }
  }

  // The links the dots make now. Each fades with distance (hard: all at full strength), drawn at dim of that strength, none for the
  // dot skip, and only those that reach into the band of the box from lo to hi (Compare's halves). Every link has the same color and
  // is faded with globalAlpha: a new color string for each link costs several times more.
  function web(hard,dim,skip,lo,hi){
    const L2=LINK*LINK;
    for(let i=0;i<nodes.length;i++){
      const a=nodes[i];
      for(let j=i+1;j<nodes.length;j++){
        const b=nodes[j],dx=a.x-b.x,dy=a.y-b.y,d2=dx*dx+dy*dy;
        if(d2<L2&&a!==skip&&b!==skip&&Math.max(a.x,b.x)>=lo&&Math.min(a.x,b.x)<=hi){
          ctx.globalAlpha=(hard?1:1-Math.sqrt(d2)/LINK)*.55*dim;
          ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.lineTo(b.x,b.y);ctx.stroke();
        }
      }
    }
    ctx.globalAlpha=1;
  }
  // Compare: one band of the box with its own kind of line
  function half(x0,w,hard){
    ctx.save();ctx.beginPath();ctx.rect(x0,0,w,H);ctx.clip();
    web(hard,1,null,x0-1,x0+w+1);
    ctx.restore();
  }
  // See inside, drawn over the page's picture: the followed dot's reach as a dashed ring, its links in gold at their own strength,
  // an arrow for its drift, and the pointer's pull as a second ring. All of it comes from the numbers the dots move by.
  function inside(){
    const f=follow,L2=LINK*LINK,pull=MOUSE&&mx>-500;
    ctx.setLineDash(DASH);ctx.lineWidth=1.5;
    ctx.strokeStyle=GOLD;ctx.beginPath();ctx.arc(f.x,f.y,LINK,0,TAU);ctx.stroke();
    if(pull){
      ctx.strokeStyle=INK;ctx.beginPath();ctx.arc(mx,my,Math.sqrt(PULL2),0,TAU);ctx.stroke();
      ctx.setLineDash(SOLID);ctx.fillStyle=INK;ctx.beginPath();ctx.arc(mx,my,3,0,TAU);ctx.fill();
    }
    ctx.setLineDash(SOLID);
    if(pull===pullCap.hidden)pullCap.hidden=!pull;
    ctx.strokeStyle=GOLD;ctx.lineWidth=2;
    for(const b of nodes){
      const dx=f.x-b.x,dy=f.y-b.y,d2=dx*dx+dy*dy;
      if(b!==f&&d2<L2){
        ctx.globalAlpha=(1-Math.sqrt(d2)/LINK)*.55;
        ctx.beginPath();ctx.moveTo(f.x,f.y);ctx.lineTo(b.x,b.y);ctx.stroke();
      }
    }
    ctx.globalAlpha=1;
    ctx.fillStyle=GOLD;ctx.beginPath();ctx.arc(f.x,f.y,f.r+2.5,0,TAU);ctx.fill();
    // the arrow starts at the dot's edge and is cut short where it would leave the box
    const sp=Math.hypot(f.vx,f.vy)*SPD;
    if(sp>.01){
      const ux=f.vx*SPD/sp,uy=f.vy*SPD/sp,off=f.r+5;
      const room=Math.min(ux>0?(W-4-f.x)/ux:ux<0?(4-f.x)/ux:1e9,uy>0?(H-4-f.y)/uy:uy<0?(4-f.y)/uy:1e9);
      const len=Math.min(off+sp*ARROW,room);
      if(len>off+4){
        const tx=f.x+ux*len,ty=f.y+uy*len,a=Math.atan2(uy,ux);
        ctx.lineWidth=2.5;ctx.lineCap='round';ctx.beginPath();
        ctx.moveTo(f.x+ux*off,f.y+uy*off);ctx.lineTo(tx,ty);
        ctx.moveTo(tx-Math.cos(a-.5)*7,ty-Math.sin(a-.5)*7);ctx.lineTo(tx,ty);ctx.lineTo(tx-Math.cos(a+.5)*7,ty-Math.sin(a+.5)*7);
        ctx.stroke();ctx.lineCap='butt';
      }
    }
  }
  // The dot nearest the middle of the box
  function nearestMiddle(){
    let best=null,bd=Infinity;
    for(const p of nodes){const d=(p.x-W/2)**2+(p.y-H/2)**2;if(d<bd){bd=d;best=p;}}
    return best;
  }

  // The whole picture for where the dots are; the canvas is cleared first, so no trace of an earlier picture stays.
  // Normal: links, then dots. See inside: the same at a third of the strength, then the marks. Compare: the left half joins every
  // close pair at full strength, the right half fades, then the dots and the divider.
  function draw(){
    ctx.clearRect(0,0,W,H);
    const c=PAL[COL];
    if(mode==='inside'&&!follow)follow=nearestMiddle();
    ctx.strokeStyle=`rgb(${c[0]},${c[1]},${c[2]})`;ctx.lineWidth=1;
    if(mode==='compare'){half(0,W/2,true);half(W/2,W/2,false);}
    else web(false,mode==='inside'?1/3:1,mode==='inside'?follow:null,-1,W+1);
    ctx.fillStyle=`rgba(${c[0]},${c[1]},${c[2]},.9)`;
    for(const p of nodes){ctx.beginPath();ctx.arc(p.x,p.y,p.r,0,Math.PI*2);ctx.fill();}
    if(mode==='compare'){ctx.fillStyle='#2a2b33';ctx.fillRect(W/2-1,0,2,H);}
    else if(mode==='inside'&&follow)inside();
  }

  // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one picture per
  // 16 ms, and each moves the dots by the time since the last one in 60ths of a second, never more than 50 ms (a background
  // tab does not leap). The first frame after a start has nothing to measure from and moves nothing.
  function loop(ts){
    raf=requestAnimationFrame(loop);
    if(lastT!==null&&ts-lastT<16)return;
    const dt=lastT===null?0:Math.min(ts-lastT,50);
    lastT=ts;
    step(dt/FRAME*(slowTog.checked?1/3:1));draw();
  }
  function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}

  // Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the
  // loop on arrival, which this listener must catch. The dots on screen stay where they are while paused.
  document.addEventListener('hb:pause',e=>{
    paused=e.detail.paused;
    if(paused){cancelAnimationFrame(raf);raf=null;}
    else start();
    words();
  });
  // The Compare note says the lines blink on and off; reduced motion, paused, shows a still, so it says what the still shows
  function words(){const t=paused&&reduced.matches?STILL:MOVING;if(cmpNote.textContent!==t)cmpNote.textContent=t;}
  // The mode switch draws the mode at once, playing or paused, and keeps the dots as they are. See inside follows the dot nearest the
  // middle when it is entered; Normal draws the whole box with fading lines again.
  document.addEventListener('hb:mode',e=>{
    const was=mode;mode=e.detail.mode;
    if(mode==='inside'&&was!=='inside')follow=null;
    draw();
  });
  // A tab that comes back after being hidden has no earlier frame to measure from, so its first frame moves nothing
  document.addEventListener('visibilitychange',()=>{lastT=null;});

  // Setting the canvas size wipes it, so a real change of the stage's size scatters the dots and draws them at once,
  // playing or paused. 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;
    W=cvs.width=w;H=cvs.height=h;
    init();draw();
  }

  // The pointer's position comes from pointermove and pointerdown, so a finger resting on the stage draws the dots too
  // See inside's pull ring follows the pointer, so while paused it is drawn again here
  function aim(e){if(!e.isPrimary)return;const r=stage.getBoundingClientRect();mx=e.clientX-r.left;my=e.clientY-r.top;if(paused&&mode==='inside')draw();}
  function away(e){if(e.isPrimary){mx=my=-999;if(paused&&mode==='inside')draw();}}
  stage.addEventListener('pointermove',aim);
  stage.addEventListener('pointerdown',aim);
  stage.addEventListener('pointerleave',away);
  stage.addEventListener('pointercancel',away);

  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      if(b.classList.contains('on'))return; // pressing the choice already made does nothing
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
    }));
  }
  // A setting that changes the picture draws it at once, so the change shows while the loop is paused (or is paused a
  // moment later, before the next frame). Speed and the pointer switch change no picture: the dots carry on from where they are.
  choices('count-seg',d=>{COUNT=+d.n;init();draw();});
  choices('link-seg',d=>{LINK=+d.px;draw();});
  choices('speed-seg',d=>{SPD=+d.spd;if(paused&&mode==='inside')draw();}); // the arrow grows with it
  choices('color-sw',d=>{COL=d.col;draw();});
  document.getElementById('mouse-tog').addEventListener('change',e=>{MOUSE=e.target.checked;if(paused&&mode==='inside')draw();});

  resize();
  window.addEventListener('resize',resize);
  start();
  phone.addEventListener('change',()=>{init();draw();}); // a window that crosses the phone size changes how many dots it shows
</script>
</body>
</html>