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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Abstract Geometric Motion — Animation Handbook</title>
<meta name="description" content="Shapes turn, spread and flow in a calm, endless pattern. Best for music players.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/abstract-geometric-motion/">
<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="Abstract Geometric Motion — Animation Handbook">
<meta property="og:description" content="Shapes turn, spread and flow in a calm, endless pattern. Best for music players.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/abstract-geometric-motion/">
<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="Abstract Geometric Motion — Animation Handbook">
<meta name="twitter:description" content="Shapes turn, spread and flow in a calm, endless pattern. Best for music players.">
<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": "Abstract Geometric Motion", "name": "Abstract Geometric Motion", "description": "Shapes turn, spread and flow in a calm, endless pattern. Best for music players.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/abstract-geometric-motion/", "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, Abstract Geometric Motion"}</script>
<style>
:root{--ui-accent:#ffce5a}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;overflow:hidden;background:#020408}
canvas{display:block;width:100%;height:100%}
/* See inside and Compare: plain words on a dark backing over the picture, each in a fixed place (See inside's corners follow the pattern: data-at is t or b, then l or r) */
.lbl{display:none;position:absolute;padding:4px 9px;border-radius:999px;background:rgba(2,4,8,.88);color:#f4f4f2;font:600 13px/1.2 var(--hb-font);white-space:nowrap}
.stage[data-hb-mode="inside"] .fl,.stage[data-hb-mode="compare"] .side{display:block}
.fl[data-at^="t"]{top:10px}.fl[data-at^="b"]{bottom:10px}.fl[data-at$="l"]{left:10px}.fl[data-at$="r"]{right:10px}
.side{top:10px}.side.first{left:10px}.side.second{left:calc(50% + 10px)}
@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="../grid-dot-pattern-parallax/" rel="prev" aria-label="Previous: Grid / Dot Pattern Parallax"><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>Grid / Dot Pattern Parallax</span></a><a href="../particle-constellation/" rel="next" aria-label="Next: Particle Constellation"><span><span class="hb-dir">Next: </span>Particle Constellation</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.12 · Ambient & Background</p>
<h1>Abstract Geometric Motion</h1>
<p class="hb-lede">Shapes turn, spread and flow in a calm, endless pattern. Best for music players.</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. Pause it to look closely, or 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">
<canvas id="cvs" aria-hidden="true"></canvas>
<p class="lbl fl one" data-at="tl" aria-hidden="true" data-hb-caption>Each shape turns at its own pace</p>
<p class="lbl fl two" data-at="bl" aria-hidden="true" data-hb-caption>The longer the arrow, the faster</p>
<p class="lbl side first" aria-hidden="true" data-hb-caption>Starts over</p>
<p class="lbl side second" aria-hidden="true" data-hb-caption>Never restarts</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, each arrow shows which way a shape turns and how fast: every shape keeps its own pace, slowest in the middle and fastest outside.</p>
<p data-hb-for="compare" hidden>Starts over jumps back to its first picture every few seconds, as its clock restarts. Never restarts runs on one clock that only grows, so it flows on.</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="pat-lbl">Pattern</p>
<div class="seg" id="pat-seg" role="group" aria-labelledby="pat-lbl">
<button type="button" data-idx="0" class="on" aria-pressed="true">Polygons</button>
<button type="button" data-idx="1" aria-pressed="false">Rings</button>
<button type="button" data-idx="2" aria-pressed="false">Bars</button>
<button type="button" data-idx="3" aria-pressed="false">Lines</button>
</div>
<p class="hb-hint">Four patterns, each an endless loop.</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 feels calm; keep it slow behind content.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="num-lbl">Number of shapes</p>
<div class="seg" id="num-seg" role="group" aria-labelledby="num-lbl">
<button type="button" data-n="3" aria-pressed="false">Few</button>
<button type="button" data-n="6" class="on" aria-pressed="true">Medium</button>
<button type="button" data-n="10" aria-pressed="false">Many</button>
</div>
<p class="hb-hint">More shapes make a busier pattern.</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="pal-lbl">Colors</p>
<div class="seg" id="pal-seg" role="group" aria-labelledby="pal-lbl">
<button type="button" data-pal="cool" class="on" aria-pressed="true">Cool</button>
<button type="button" data-pal="warm" aria-pressed="false">Warm</button>
<button type="button" data-pal="neon" aria-pressed="false">Neon</button>
<button type="button" data-pal="mono" aria-pressed="false">Gray</button>
</div>
<p class="hb-hint">The set of colors the shapes use.</p>
</div>
<div class="hb-setting" id="thick-setting">
<label class="hb-switch-row"><span>Thicker lines</span><input class="hb-switch" type="checkbox" role="switch" id="thick-tog" autocomplete="off"></label>
<p class="hb-hint">Bolder lines that stand out more.</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 an abstract geometric animation to [the section or screen you want a calm moving background in]. Draw simple shapes on a canvas and keep them moving in an endless loop: polygons slowly turning around the center, rings spreading out and fading, slanted bars sliding across, or wavy lines with color flowing along them. Drive everything from one steadily growing time value, so the loop never restarts or jumps. Colors from one family keep it calm. 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>Music and media players</li><li>Screensavers and kiosks</li><li>Loading screens</li><li>Art pages</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Forms</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'),thickSetting=document.getElementById('thick-setting');
const BG='#020408';
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 t=0; // the pattern's own time: it moves only while the loop runs
let rings=[],ringT=0; // the rings on screen, and the time the last one was sent out
let barX=0,dashPhase=0; // how far the bars and the dashes have traveled, so a Speed change never moves them
let PRESET=0,SPD=0.6,CX=6,PAL='cool',CONTRAST=false;
// See inside and Compare. S: the size of the shapes and of the rings' growth, 1 in Normal and HS in Compare's halves; rc gives a new ring its color there (Normal draws it at random); worlds: Compare's two halves; posed: they show their posed moment.
let mode='normal',S=1,HS=1,rc=null,worlds=null,posed=false;
const PALETTES={
cool:c=>`hsla(${200+c*10},70%,65%,`,
warm:c=>`hsla(${30+c*8},80%,60%,`,
neon:c=>`hsla(${(c*60)%360},100%,65%,`,
mono:c=>`hsla(0,0%,${60+c*5}%,`
};
function col(c,a){return PALETTES[PAL](c)+a+')';}
function lineW(){return CONTRAST?1.5:.8;}
// PRESET 0: Rotating polygons
function drawPolygons(){
const cx=W/2,cy=H/2;
for(let i=0;i<CX;i++){
const n=3+i%5; // 3 to 7 sides
const r=(30+i*20)*S;
const rot=t*(0.2+i*.08)+(i%2===0?0:Math.PI);
ctx.beginPath();
for(let j=0;j<=n;j++){const a=j/n*Math.PI*2+rot;ctx.lineTo(cx+Math.cos(a)*r,cy+Math.sin(a)*r);}
ctx.strokeStyle=col(i,.5+Math.sin(t*.5+i)*.3);
ctx.lineWidth=lineW()*(1+i*.1);ctx.stroke();
}
}
// PRESET 1: Concentric expanding rings
function drawRings(){
const cx=W/2,cy=H/2;
rings.forEach(r=>{
const life=1-r.r/Math.max(W,H);
ctx.beginPath();ctx.arc(cx,cy,r.r,0,Math.PI*2);
ctx.strokeStyle=col(r.c,life*.6);ctx.lineWidth=lineW()*(1+life);ctx.stroke();
});
}
// The rings on screen are colored by their place in the list, spread across Number of shapes (ring i of n gets color floor(i * CX / n)). The same count always gives the same picture, and a bigger count shows more of the color range even on the few rings that fit a wide, short stage.
function recolor(){rings.forEach((r,i)=>{r.c=Math.floor(i*CX/rings.length);});}
// Six rings spread across the stage, so the pattern shows at once and a stage that is paused still shows it
function seedRings(){
const m=Math.max(W,H);
ringT=t;
rings=Array.from({length:6},(_,i)=>({r:m*(i+1)/7,c:0}));
recolor();
}
// PRESET 2: Sliding bars
function drawBars(){
const count=CX;
for(let i=0;i<count;i++){
const x=(barX+i*(W/count)*1.1)%W;
const barW=W/count*.5;
const op=.15+.1*Math.sin(t+i*.7);
ctx.fillStyle=col(i,op);
// Diagonal slant
ctx.save();ctx.translate(x,0);
ctx.beginPath();
ctx.moveTo(-barW*.2,0);ctx.lineTo(barW*.8,0);ctx.lineTo(barW*.6,H);ctx.lineTo(-barW*.4,H);ctx.closePath();
ctx.fill();ctx.restore();
}
}
// PRESET 3: Color-flow lines
function drawLines(){
const count=CX*2;
for(let i=0;i<count;i++){
const y=H*(i+.5)/count;
const amp=15+i*3;
const phase=i*.4+t*.8;
ctx.beginPath();
for(let x=0;x<=W;x+=4){
const py=y+Math.sin(x/80+phase)*amp+Math.cos(x/120+phase*.7)*amp*.5;
x===0?ctx.moveTo(x,py):ctx.lineTo(x,py);
}
// Animated dash offset for color flow effect
const dashLen=40,gapLen=60;
ctx.setLineDash([dashLen,gapLen]);
ctx.lineDashOffset=-dashPhase+i*20;
ctx.strokeStyle=col(i%CX,.6+Math.sin(t+i)*.3);
ctx.lineWidth=lineW()*(1.5-i*.03);ctx.stroke();
ctx.setLineDash([]);
}
}
// One step of the loop. k is 1 for a frame at 60 Hz (more or less for a longer or shorter one), and a third of that in slow motion. t, the rings' growth and the bars' and dashes' travel all move by it, so slow motion slows all of them.
function advance(k){
const dt=SPD*.01*k;
t+=dt;barX+=SPD*40*dt;dashPhase+=SPD*30*dt;
if(PRESET!==1)return;
const m=Math.max(W,H);
if(t-ringT>=.5){ringT=t;rings.push({r:0,c:rc?rc():Math.floor(Math.random()*CX)});}
rings=rings.filter(r=>r.r<m);
rings.forEach(r=>{r.r+=SPD*2*k*S;});
}
// The whole picture for the current t; the canvas is painted over completely, so no trace of an earlier picture stays. See inside adds its marks on top; Compare draws its two halves instead.
function draw(){
if(mode==='compare')return drawCompare();
ctx.fillStyle=BG;ctx.fillRect(0,0,W,H);
pattern();
if(mode==='inside')drawInside();
}
function pattern(){[drawPolygons,drawRings,drawBars,drawLines][PRESET]();}
// See inside: marks over the pattern in white and gold, drawn from the numbers the pattern draws with, so they ride every frame and follow every setting; its two captions are page text in fixed corners. head() draws an arrowhead pointing at angle a.
const GOLD='#ffce5a',INK='rgba(244,244,242,.85)';
function head(x,y,a){ctx.moveTo(x-7*Math.cos(a-.5),y-7*Math.sin(a-.5));ctx.lineTo(x,y);ctx.lineTo(x-7*Math.cos(a+.5),y-7*Math.sin(a+.5));}
function drawInside(){
const cx=W/2,cy=H/2,caps=[capA,capB].map(c=>[c.offsetLeft-5,c.offsetTop-5,c.offsetLeft+c.offsetWidth+5,c.offsetTop+c.offsetHeight+5]);
const room=(x,y)=>x>5&&x<W-5&&y>5&&y<H-5&&!caps.some(([l,tp,r,b])=>x>l&&x<r&&y>tp&&y<b); // in the box, clear of the captions
ctx.save();ctx.setLineDash([]);ctx.lineWidth=1.5;ctx.lineCap=ctx.lineJoin='round';ctx.strokeStyle=GOLD;ctx.beginPath();
if(PRESET===0)for(let i=0;i<CX;i++){
// Polygons: just outside each shape, an arrow from a corner the way it turns, as far as it turns in four seconds: from its first corner, or, where that arrow would leave the box or meet a caption, from the corner behind it (and so on round); none where no corner has room
const n=3+i%5,R=37+i*20,rot=t*(.2+i*.08)+(i%2===0?0:Math.PI),sw=(.2+i*.08)*SPD*2.4,m=Math.ceil(R*sw/6);
for(let j=0;j<n;j++){
const a=rot-j*2*Math.PI/n,at=f=>[cx+R*Math.cos(a+sw*f),cy+R*Math.sin(a+sw*f)];
let fits=true;for(let s=0;s<=m&&fits;s++)fits=room(...at(s/m));
if(!fits)continue;
ctx.moveTo(...at(0));ctx.arc(cx,cy,R,a,a+sw);head(...at(1),a+sw+Math.PI/2);break;
}
}else if(PRESET===1){
// Rings: on the three newest rings that have room, an arrow out toward the bottom left corner, as far as a ring grows in a third of a second (all the same length); a bar as bright as a ring of each size, from the middle to the box's side; the dot where every ring starts
const L=SPD*2*20,d=Math.hypot(W,H),ux=-W/d,uy=H/d,g=ctx.createLinearGradient(cx,0,W,0);
[...rings].sort((p,q)=>p.r-q.r).slice(0,3).forEach(r=>{const f=r.r+3,e=f+L;
if(room(cx+ux*e,cy+uy*e)){ctx.moveTo(cx+ux*f,cy+uy*f);ctx.lineTo(cx+ux*e,cy+uy*e);head(cx+ux*e,cy+uy*e,Math.atan2(uy,ux));}});
ctx.stroke();
g.addColorStop(0,'rgba(244,244,242,.6)');g.addColorStop(1,`rgba(244,244,242,${.6*(1-(W-cx)/Math.max(W,H))})`);
ctx.fillStyle=g;ctx.fillRect(cx,cy-2,W-cx,4);
ctx.fillStyle=GOLD;ctx.beginPath();ctx.arc(cx,cy,5,0,7);ctx.fill();ctx.beginPath();
}else if(PRESET===2){
// Bars: each bar outlined where the page draws it; an arrow as far as they slide in eight seconds; the right edge, where a bar leaves, dashed; and the way back to the left edge along the bottom
ctx.strokeStyle=INK;
for(let i=0;i<CX;i++){const x=(barX+i*(W/CX)*1.1)%W,b=W/CX*.5;ctx.moveTo(x-b*.2,0);ctx.lineTo(x+b*.8,0);ctx.lineTo(x+b*.6,H);ctx.lineTo(x-b*.4,H);ctx.closePath();}
ctx.stroke();
const L=SPD*40*SPD*.01*480,y=H-46;
ctx.strokeStyle=GOLD;ctx.beginPath();ctx.moveTo(cx-L/2,cy);ctx.lineTo(cx+L/2,cy);head(cx+L/2,cy,0);
ctx.moveTo(W-9,y);ctx.lineTo(9,y);head(9,y,Math.PI);ctx.stroke();
ctx.setLineDash([5,6]);ctx.beginPath();ctx.moveTo(W-9,8);ctx.lineTo(W-9,y-6);
}else{
// Lines: a top line, the middle line and a low line traced faintly in full (the low one: the lowest whose bracket fits above the caption); on the middle line, an arrowhead just ahead of each colored dash, found by walking the line with the page's dash pattern; at the left edge, brackets as tall as the top and the low line's waves
const n=CX*2,i=CX,line=j=>{const yj=H*(j+.5)/n,aj=15+j*3,pj=j*.4+t*.8;return x=>yj+Math.sin(x/80+pj)*aj+Math.cos(x/120+pj*.7)*aj*.5;};
let a=0,b=n-1,s=0,next=((dashPhase-i*20+44)%100+100)%100;
while(H*(a+.5)/n-15-a*3<8)a++;
while(b>a&&H*(b+.5)/n+15+b*3>H-44)b--;
ctx.strokeStyle='rgba(244,244,242,.3)';ctx.lineWidth=1;
for(const j of new Set([a,i,b])){const yAt=line(j);ctx.moveTo(0,yAt(0));for(let x=4;x<=W;x+=4)ctx.lineTo(x,yAt(x));}
ctx.stroke();
ctx.strokeStyle=GOLD;ctx.lineWidth=1.5;ctx.beginPath();
for(let x=4,yAt=line(i),py=yAt(0);x<=W;x+=4){
const qy=yAt(x),d=Math.hypot(4,qy-py);
for(;next<=s+d;next+=100){const f=(next-s)/d;head(x-4+4*f,py+(qy-py)*f,Math.atan2(qy-py,4));}
s+=d;py=qy;
}
ctx.stroke();
ctx.strokeStyle=INK;ctx.beginPath();
for(const j of new Set([a,b])){const yj=H*(j+.5)/n,aj=15+j*3;ctx.moveTo(17,yj-aj);ctx.lineTo(10,yj-aj);ctx.lineTo(10,yj+aj);ctx.lineTo(17,yj+aj);}
}
ctx.stroke();ctx.restore();
}
// Compare: two worlds of their own side by side, each drawn as if the box were half as wide (on a narrow box the shapes and the rings' growth smaller, HS, so the middle-sized pattern still fits a half) and copied from Normal's at entry: its time, both counters, the rings and when the last ring started.
// Both take the same step each frame; Normal's own state waits in the page's variables, untouched. inWorld runs fn with a world in those variables.
const LAP=1.25,POSE=1.55; // the first half starts over each time its clock has run LAP: about every 3.5 seconds at Normal
function inWorld(w,fn){
const keep={t,barX,dashPhase,rings,ringT,W};
({t,barX,dashPhase,rings,ringT}=w);W/=2;S=HS;
rc=()=>{const f=Math.sin(++w.n*91.7)*4375.85;return Math.floor((f-Math.floor(f))*CX);}; // a new ring's color, from its number
fn();
Object.assign(w,{t,barX,dashPhase,rings,ringT});
({t,barX,dashPhase,rings,ringT,W}=keep);S=1;rc=null;
}
const copy=w=>({...w,rings:w.rings.map(r=>({...r}))});
// Entering Compare, and after a pattern or size change there, both worlds start at a posed moment, as if POSE had run since the picture on screen, in fixed steps of a 60th: the second has run on, the first started over LAP in and has run on from there. So the halves and their graphs differ at once, playing or paused, and the pose is the same on every screen.
function pose(){
HS=Math.min(1,(Math.min(W/2,H)/2-8)/130); // 130: the largest shape's size at the middle number of shapes
const e={t,barX,dashPhase,ringT,n:0,rings:rings.map(r=>({r:r.r*HS,c:r.c}))};
worlds=[POSE-LAP,POSE].map(u=>{const w=copy(e);inWorld(w,()=>{for(let i=Math.round(u/(SPD*.01));i>0;i--)advance(1);});return w;});
worlds[0].e=e;posed=true;
}
// One frame of Compare: both worlds take the same step, and the first goes back to its picture at entry once its clock has run LAP
function stepWorlds(k){
worlds.forEach(w=>inWorld(w,()=>advance(k)));
if(worlds[0].t-worlds[0].e.t>=LAP)worlds[0]=Object.assign(copy(worlds[0].e),{e:worlds[0].e});
if(k>0&&posed){posed=false;words();}
}
function drawCompare(){
const h=W/2;
ctx.fillStyle=BG;ctx.fillRect(0,0,W,H);
worlds.forEach((w,side)=>{
ctx.save();ctx.beginPath();ctx.rect(side*h,0,h,H);ctx.clip();ctx.translate(side*h,0);
inWorld(w,pattern);clock(w,side,h);
ctx.restore();
});
ctx.fillStyle='#2a2b33';ctx.fillRect(h-1,0,2,H);
}
// A small graph of each half's clock over the last seven seconds of play, now at its right end, as steep as Speed: the first half's climbs and drops back each time it starts over; the second half's only grows (an arrow)
function clock(w,side,h){
const gw=Math.min(h-24,200),x0=(h-gw)/2,L=x0+8,B=H-19,iw=gw-16,ih=22,V=LAP*1.25,px=iw/420,py=ih/V,sg=SPD*.01;
ctx.setLineDash([]);ctx.fillStyle='rgba(2,4,8,.88)';ctx.strokeStyle='rgba(244,244,242,.16)';ctx.lineWidth=1;ctx.beginPath();ctx.roundRect(x0,H-48,gw,36,8);ctx.fill();ctx.stroke();
ctx.strokeStyle='rgba(244,244,242,.35)';ctx.beginPath();ctx.moveTo(L,B);ctx.lineTo(L+iw,B);ctx.stroke();
ctx.strokeStyle=GOLD;ctx.lineWidth=1.5;ctx.lineJoin=ctx.lineCap='round';ctx.beginPath();
if(side){const x=Math.max(0,iw-V/sg*px);ctx.moveTo(L+x,B-(V-(iw-x)/px*sg)*py);ctx.lineTo(L+iw,B-ih);head(L+iw,B-ih,Math.atan2(-sg*py,px));}
else{let x=iw,v=w.t-w.e.t;ctx.moveTo(L+x,B-v*py);
for(;;){const back=v/sg*px;if(back>=x){ctx.lineTo(L,B-(v-x/px*sg)*py);break;}x-=back;ctx.lineTo(L+x,B);ctx.lineTo(L+x,B-LAP*py);v=LAP;}}
ctx.stroke();
}
// loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. The first frame after a start has no earlier frame to measure from, so it adds no time; a long gap between frames (a background tab) counts for at most 50 ms. At most one picture is drawn per 16 ms.
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;
const k=dt/FRAME*(slowTog.checked?1/3:1);
if(mode==='compare')stepWorlds(k);else advance(k);
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 picture on screen stays as it is while paused.
document.addEventListener('hb:pause',e=>{
paused=e.detail.paused;
if(paused){cancelAnimationFrame(raf);raf=null;}
else start();
words();
});
// What the captions and the notes say: See inside's for the pattern on show (the markup holds Polygons' words), and Compare's "Shown still" words only while it is paused on its posed moment (the markup's once it has run on, and after a later Pause)
const capA=stage.querySelector('.fl.one'),capB=stage.querySelector('.fl.two');
const noteIn=document.querySelector('[data-hb-for="inside"]'),noteCmp=document.querySelector('[data-hb-for="compare"]');
const SAY=[[capA.textContent,capB.textContent,noteIn.textContent,'tl','bl'],
['A new ring starts at the dot, on a steady beat','Each ring fades as it grows','Inside, a new ring starts at the center on a steady beat; every ring grows at the same pace and fades as it spreads.','tl','br'],
['All bars slide together','Out on the right, back in on the left','Inside, all the bars slide together at one pace; a bar that leaves on the right comes back in on the left, so the row never ends.','tl','bl'],
['The dashes slide along each line','Each line waves more than the one above','Inside, the colored dashes slide along each wavy line, and each line waves a little more than the one above it.','tr','bl']];
const CMP=noteCmp.textContent,STILL='Shown still: Starts over has just jumped back, while Never restarts has carried on. The small graphs show each clock: one drops back, the other keeps growing.';
const put=(el,s)=>{if(el.textContent!==s)el.textContent=s;};
function words(){
const [a,b,n,pa,pb]=SAY[PRESET];
put(capA,a);put(capB,b);put(noteIn,n);capA.dataset.at=pa;capB.dataset.at=pb;
put(noteCmp,paused&&posed?STILL:CMP);
}
// The mode switch draws the mode at once, playing or paused: entering Compare poses its two worlds (pressing it again keeps them), and Normal finds its own state as it left it. Nothing here scrolls, takes focus, presses a control or changes Pause.
document.addEventListener('hb:mode',e=>{
const was=mode;mode=e.detail.mode;
if(mode==='compare'&&was!=='compare')pose();
words();draw();
});
// Setting the canvas size wipes it, so a real change of the stage's size draws the picture again 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=w;H=h;
const dpr=Math.min(window.devicePixelRatio||1,2);
cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);
ctx.setTransform(dpr,0,0,dpr,0,0);
if(mode==='compare')pose(); // the halves are posed again for their new size
draw();
}
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);
}));
}
// A setting that changes the picture draws it again at once, so the change shows even while the loop is paused (or is paused a moment later, before the next frame). Speed changes no picture in Normal: the bars and the dashes carry on from where they are, so it needs no drawing (See inside's arrows and Compare's graphs follow it at once). Pressing the choice already made does nothing. In Compare a new pattern poses both halves again, and a new number of shapes recolors their rings as Normal's.
choices('pat-seg',d=>{
if(+d.idx===PRESET)return;
PRESET=+d.idx;
thickSetting.hidden=PRESET===2; // Bars are filled shapes with no lines, so Thicker lines changes nothing there; it keeps its choice
rings=[];
if(PRESET===1)seedRings();
if(mode==='compare')pose();
words();draw(); // the captions first: See inside's marks keep clear of them
});
choices('speed-seg',d=>{SPD=+d.spd;if(mode!=='normal')draw();});
choices('num-seg',d=>{
if(+d.n===CX)return;
CX=+d.n;
recolor(); // the rings on screen take their colors from the new count at once
if(mode==='compare')[...worlds,worlds[0].e].forEach(w=>inWorld(w,recolor));
draw();
});
choices('pal-seg',d=>{PAL=d.pal;draw();});
document.getElementById('thick-tog').addEventListener('change',e=>{CONTRAST=e.target.checked;draw();});
resize();
window.addEventListener('resize',resize);
start();
</script>
</body>
</html>