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>Text on a Path — Animation Handbook</title>
<meta name="description" content="Text travels along a wave, an arc or a circle. Best for badges and seals.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-on-path/">
<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="Text on a Path — Animation Handbook">
<meta property="og:description" content="Text travels along a wave, an arc or a circle. Best for badges and seals.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-on-path/">
<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="Text on a Path — Animation Handbook">
<meta name="twitter:description" content="Text travels along a wave, an arc or a circle. Best for badges and seals.">
<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": "Text on a Path", "name": "Text on a Path", "description": "Text travels along a wave, an arc or a circle. Best for badges and seals.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/05-text-typography/text-on-path/", "inLanguage": "en", "articleSection": "Text & Typography", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Text & Typography, Text on a Path"}</script>
<style>
:root{--ui-accent:#ff6f8b;--mark:#ffd166}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;display:flex;align-items:center;justify-content:center;background:radial-gradient(circle at 50% 45%,#101018,#04060c);padding:16px;overflow:hidden}
.stage svg{width:100%;height:100%;max-width:640px}
.flow-text{font-weight:800;font-size:34px;letter-spacing:2px;fill:#f4f4f2}
.guide{fill:none;stroke:var(--ui-accent);stroke-width:1.5;stroke-dasharray:5 6;opacity:.5;transition:opacity .2s}
.guide.hidden{opacity:0}
@media(prefers-reduced-motion:reduce){.guide{transition:none}}
/* Nothing below shows in Normal: the drawn parts and the captions are for See inside, the second drawing for Compare */
.inx,.cap,.cmp-a,.cmp-nb{display:none}
.bold,.slide{fill:none;stroke-linejoin:round}
.bold{stroke:var(--ui-accent);stroke-width:calc(5*var(--u,1));stroke-linecap:round}
.slide{stroke:var(--mark);stroke-width:calc(4*var(--u,1))}
.dot{fill:#f4f4f2;stroke:#08080a;stroke-width:calc(2*var(--u,1))}
.head{fill:var(--mark);stroke:#08080a;stroke-width:calc(1.5*var(--u,1));stroke-linejoin:round}
.tick line{stroke:var(--mark);stroke-width:calc(3*var(--u,1));stroke-linecap:round}
.cap,.cmp-name{padding:4px 9px;border:1px solid rgba(255,255,255,.14);border-radius:7px;background:rgba(8,8,10,.92);color:#f4f4f2;font:600 12px/15px var(--hb-font);white-space:nowrap}
@media(min-width:601px){.cap,.cmp-name{font-size:13px}}
.cap{position:absolute;left:0;top:0}
.cap::before{content:"";flex:none;margin-right:7px}
.c-curve::before{width:16px;height:5px;border-radius:3px;background:var(--ui-accent)}
.c-tick::before{width:3px;height:14px;border-radius:2px;background:var(--mark)}
.c-dot::before{width:10px;height:10px;border-radius:50%;background:#f4f4f2}
.c-slide::before{width:16px;height:9px;background:var(--mark);clip-path:polygon(0 33%,58% 33%,58% 0,100% 50%,58% 100%,58% 67%,0 67%)}
.gone{display:none!important}
/* See inside: the curve drawn bold, the dots where it starts and ends, a tick where each copy of the phrase starts and a bar as
long as one second's slide; the thin guide the Shows the path switch controls runs along the middle of the bold curve */
.stage[data-hb-mode="inside"] .inx{display:inline}
.stage[data-hb-mode="inside"] .cap{display:flex;align-items:center}
.stage[data-hb-mode="inside"] .guide{stroke:#f4f4f2;stroke-width:calc(2*var(--u,1));opacity:.9}
.stage[data-hb-mode="inside"] .guide.hidden{opacity:0}
/* Compare: the same words on a straight line (above, or on the left) and on the curve, two drawings of one size */
.stage[data-hb-mode="compare"]{display:grid;grid-template-columns:minmax(0,1fr);grid-template-rows:auto minmax(0,max-content) auto minmax(0,max-content);align-content:center;gap:6px 0;padding:10px}
.stage[data-hb-mode="compare"] .cmp-a{display:grid;grid-template-rows:subgrid;grid-row:1/3;row-gap:6px;border-bottom:1px solid rgba(255,255,255,.09)}
.stage[data-hb-mode="compare"] .cmp-nb{display:block;grid-row:3;justify-self:center}
.stage[data-hb-mode="compare"] .cmp-na{justify-self:center}
.stage[data-hb-mode="compare"]>svg{grid-row:4;max-width:none;min-height:0}
@media(min-width:601px){
.stage[data-hb-mode="compare"]{grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:auto minmax(0,max-content)}
.stage[data-hb-mode="compare"] .cmp-a{grid-column:1;border-bottom:0;border-right:1px solid rgba(255,255,255,.09)}
.stage[data-hb-mode="compare"] .cmp-nb{grid-row:1;grid-column:2}
.stage[data-hb-mode="compare"]>svg{grid-row:2;grid-column:2}
}
</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="../glitch-text/" rel="prev" aria-label="Previous: Glitch Text"><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>Glitch Text</span></a><a href="../wavy-text/" rel="next" aria-label="Next: Wavy Text"><span><span class="hb-dir">Next: </span>Wavy Text</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">05.13 · Text & Typography</p>
<h1>Text on a Path</h1>
<p class="hb-lede">Text travels along a wave, an arc or a circle. Best for badges and seals.</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">
<svg viewBox="0 0 640 360" xmlns="http://www.w3.org/2000/svg" aria-label="animated text following a curved path">
<defs>
<path id="curve" d="M20,180 C120,60 240,300 340,180 S560,60 620,180"/>
</defs>
<use href="#curve" class="bold inx"/>
<use href="#curve" class="guide" id="guide"/>
<text class="flow-text">
<textPath href="#curve" id="tp" startOffset="0%">FOLLOW THE CURVE · </textPath>
</text>
<g class="marks inx">
<circle class="dot"/><circle class="dot"/>
<path class="slide"/><path class="head"/>
<g class="tick"><line/></g>
</g>
</svg>
<div class="cmp-a" aria-hidden="true" inert>
<span class="cmp-name cmp-na" data-hb-caption>Straight line</span>
<svg viewBox="0 0 640 360" xmlns="http://www.w3.org/2000/svg">
<defs><path id="ln-path" d="M20,180 H620"/></defs>
<use href="#ln-path" class="guide" id="ln-guide"/>
<text class="flow-text"><textPath href="#ln-path" id="ln-tp" startOffset="0">FOLLOW THE CURVE · </textPath></text>
</svg>
</div>
<span class="cmp-name cmp-nb" data-hb-caption>On a path</span>
<span class="cap c-curve" data-hb-caption aria-hidden="true">The text is attached to this curve</span>
<span class="cap c-tick" data-hb-caption aria-hidden="true">Each tick starts one copy of the phrase</span>
<span class="cap c-dot" data-hb-caption aria-hidden="true">Letters show only between the dots</span>
<span class="cap c-slide" data-hb-caption aria-hidden="true">Slides a quarter of the curve a second</span>
</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 text is attached to a curve, drawn bold here. The phrase is repeated, and each tick marks where one copy starts; they all slide along the curve together.</p>
<p data-hb-for="compare" hidden>Straight line slides the same words along a flat line, like a ticker. On a path bends them around the curve, so every letter follows its shape.</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="shape-lbl">Path shape</p>
<div class="seg" id="shape-seg" role="group" aria-labelledby="shape-lbl">
<button type="button" data-shape="wave" class="on" aria-pressed="true">Wave</button>
<button type="button" data-shape="arc" aria-pressed="false">Arc</button>
<button type="button" data-shape="circle" aria-pressed="false">Circle</button>
</div>
<p class="hb-hint">The curve the text follows.</p>
</div>
<div class="hb-setting">
<p class="hb-setting-name" id="speed-lbl">Speed</p>
<div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
<button type="button" data-speed="0.25" aria-pressed="false">Slow</button>
<button type="button" data-speed="0.4" class="on" aria-pressed="true">Normal</button>
<button type="button" data-speed="0.65" aria-pressed="false">Fast</button>
</div>
<p class="hb-hint">How fast the text travels along the curve.</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 path</span><input class="hb-switch" type="checkbox" role="switch" id="guide-tog" checked autocomplete="off"></label>
<p class="hb-hint">A dashed line marks the curve under the text.</p>
</div>
<div class="hb-setting">
<label class="hb-setting-name" for="text-inp">Your text</label>
<input class="hb-text" type="text" id="text-inp" value="FOLLOW THE CURVE · " maxlength="40" autocomplete="off">
<p class="hb-hint">End with a mark such as · so the repeats read well.</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 text that travels along a curve to [your short phrase]. Define the curve once and attach the text to it so every letter sits on the line, then keep moving the point where the text starts so the letters travel along the curve like a ticker bent into shape. Repeat the phrase, with a separator such as a dot, until it covers the whole curve, and wrap around by exactly one copy so the loop has no jump. Keep the curve gentle enough that letters do not crowd on sharp bends. If the visitor has reduced motion turned on, show the text still on the curve. 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>Badges and seals</li><li>Playful titles</li><li>Decorations</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Long text</li><li>Key information</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 tp=document.getElementById('tp');
const curve=document.getElementById('curve');
const guide=document.getElementById('guide');
const txtIn=document.getElementById('text-inp');
const slowTog=document.getElementById('slow-tog');
const PATHS={
wave:'M20,180 C120,60 240,300 340,180 S560,60 620,180',
arc:'M20,300 Q320,20 620,300',
circle:'M320,60 a120,120 0 1,1 -0.1,0'
};
const DEFAULT_TEXT='FOLLOW THE CURVE · ';
const FRAME=1000/60; // the speeds are a share of the path per 1/60 s, so a 120 Hz screen is no faster than a 60 Hz one
let offset=0,speed=0.4,raf=null,last=null,unit=100,total=700,mode='normal';
// Glyphs past the end of the path are not drawn, so repeat the string until it
// covers the path plus one extra copy, then wrap the offset over that one copy.
// One copy's length is measured as (two copies - one copy) so collapsed
// leading/trailing spaces cancel out and the wrap point lines up exactly.
function measure(){ const n=tp.getNumberOfChars(); return n?tp.getSubStringLength(0,n):0; }
function fill(){
// a blank field falls back to the default phrase; a typed phrase is kept as typed, including its trailing space
const s=txtIn.value.trim()?txtIn.value:DEFAULT_TEXT;
tp.textContent=s; const one=measure();
tp.textContent=s+s; const two=measure();
total=curve.getTotalLength();
unit=Math.max((two-one)/total*100,1);
tp.textContent=s.repeat(Math.ceil(100/unit)+1);
// The copy length and the number of copies just changed. Keep the start point within one copy and write it
// now: loop() would do it, but it is not running while paused, and a stale start point can leave the end of the curve empty.
offset%=unit;
tp.setAttribute('startOffset',(offset-unit)+'%');
if(mode!=='normal')refreshView();
}
function step(dt){ return (slowTog.checked?speed/3:speed)*dt/FRAME; }
function loop(now){
// The first frame after start() has no earlier frame to measure from, so it stays put; a long gap
// between frames (a background tab) counts for at most 50 ms.
const dt=last===null?0:Math.min(now-last,50);
last=now;
offset=(offset+step(dt))%unit;
tp.setAttribute('startOffset',(offset-unit)+'%');
if(mode!=='normal')drawLive();
raf=requestAnimationFrame(loop);
}
function start(){ if(raf)return; last=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. loop() requests exactly one next frame and stores it
// in raf; Pause cancels that frame and clears raf, and Play calls start(), which continues from the current
// offset and forgets the time of the last frame, so the pause is not counted as movement. start() keeps its
// if(raf)return guard, so Play never starts a second loop.
document.addEventListener('hb:pause',e=>{
if(e.detail.paused){cancelAnimationFrame(raf);raf=null;}
else start();
paused=e.detail.paused;describe();
});
// The mode switch: the stage is already marked, so the view is drawn at once, also while the loop is paused
document.addEventListener('hb:mode',e=>{mode=e.detail.mode;refreshView()});
txtIn.addEventListener('input',fill);
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);
}));
}
choices('shape-seg',d=>{curve.setAttribute('d',PATHS[d.shape]);fill()});
choices('speed-seg',d=>{speed=+d.speed;if(mode==='inside')refreshView()});
slowTog.addEventListener('change',()=>{if(mode==='inside')refreshView()});
document.getElementById('guide-tog').addEventListener('change',e=>{
guide.classList.toggle('hidden',!e.target.checked);
ln.guide.classList.toggle('hidden',!e.target.checked);
});
// ---- See inside and Compare ----
const stage=document.querySelector('.stage'),svg=stage.querySelector('svg'),reduceMQ=matchMedia('(prefers-reduced-motion: reduce)');
const ln={svg:stage.querySelector('.cmp-a svg'),path:document.getElementById('ln-path'),tp:document.getElementById('ln-tp'),guide:document.getElementById('ln-guide')};
const marks=svg.querySelector('.marks'),dots=[...marks.querySelectorAll('.dot')],head=marks.querySelector('.head'),ticks=[marks.querySelector('.tick')];
const bar=marks.querySelector('.slide'),caps=[...stage.querySelectorAll('.cap')],NAME=svg.getAttribute('aria-label');
const LABEL={normal:NAME,inside:NAME+', with the curve drawn bold and a tick where each copy of the phrase starts',
compare:'the same words on a straight line and on a curve'};
const BAR=24; // how far below the curve the slide bar runs, in pixels
let k=1,fadeAt=15,tickN=0,tip={x:0,y:0}; // k: screen pixels in a drawing unit; fadeAt: how far from an end a tick starts to show, in pixels
// Under reduced motion the loop starts paused, so a note says "Shown still" only while it is paused
const notes=[...document.querySelectorAll('[data-hb-for]')],moving=notes.map(n=>n.textContent),still=[
'Shown still: the text is attached to the bold curve, and each tick marks where one copy of the phrase starts. Played, they all slide along the curve together.',
'Shown still: Straight line sets the words on a flat line, while On a path bends the same words around the curve.'];
let paused=reduceMQ.matches;
function describe(){
notes.forEach((n,i)=>{const words=reduceMQ.matches&&paused?still[i]:moving[i];if(n.textContent!==words)n.textContent=words});
}
reduceMQ.addEventListener('change',describe);
function pt(L){ return curve.getPointAtLength(L); }
function share(){ return (slowTog.checked?speed/3:speed)*60/100; } // the part of the curve the text covers in a second
function slideWord(f){ // that part in a beginner's words
const W=[[.05,'a twentieth'],[.083,'a twelfth'],[.125,'an eighth'],[.167,'a sixth'],[.25,'a quarter'],[.4,'two fifths']];
return W.reduce((a,b)=>Math.abs(b[0]-f)<Math.abs(a[0]-f)?b:a)[1];
}
// The height of drawing that holds the curve and its letters; Compare crops both drawings to it, so they share one scale
function span(){
let y0=360,y1=0;
for(let i=0;i<=48;i++){const y=pt(total*i/48).y;y0=Math.min(y0,y);y1=Math.max(y1,y)}
y0=Math.max(0,y0-36);y1=Math.min(360,y1+36);
return [y0,y1-y0];
}
function layoutCompare(){
const [y,h]=span(),box='0 '+y.toFixed(1)+' 640 '+h.toFixed(1);
svg.setAttribute('viewBox',box);ln.svg.setAttribute('viewBox',box);
ln.path.setAttribute('d','M20,'+(y+h/2).toFixed(1)+' H620');
if(ln.tp.textContent!==tp.textContent)ln.tp.textContent=tp.textContent;
}
function layoutInside(){
k=svg.getScreenCTM().a;svg.style.setProperty('--u',1/k);
const u=n=>n/k,a=pt(0),b=pt(total),closed=Math.hypot(a.x-b.x,a.y-b.y)*k<20,d=closed?u(11):0;
[pt(d),pt(total-d)].forEach((p,i)=>{dots[i].setAttribute('cx',p.x);dots[i].setAttribute('cy',p.y);dots[i].setAttribute('r',u(7))});
fadeAt=closed?26:15;
const gap=unit/100*total;
tickN=gap*k<22?0:Math.min(Math.ceil(total/gap)+2,30); // copies too short to tell apart get no ticks
while(ticks.length<tickN){const c=ticks[0].cloneNode(true);marks.appendChild(c);ticks.push(c)}
ticks.forEach(t=>{const l=t.firstChild,top=Math.max(22,34*k);l.setAttribute('y1',-u(top));l.setAttribute('y2',u(10));t.o=-1});
// the bar runs beside the curve, on the side away from the letters, as long as the text slides in a second
const f=share(),len=f*total,n=Math.max(4,Math.ceil(len*k/14)),side=[];
for(let i=0;i<=n;i++){
const L=len*i/n,p=pt(L),a2=pt(Math.max(L-2,0)),b2=pt(Math.min(L+2,total)),m=Math.hypot(b2.x-a2.x,b2.y-a2.y)||1;
side.push([p.x-(b2.y-a2.y)/m*u(BAR),p.y+(b2.x-a2.x)/m*u(BAR)]);
}
const [x,y]=side[n],[x0,y0]=side[n-1];
tip={x:x,y:y};
bar.setAttribute('d','M'+side.map(c=>c[0].toFixed(1)+','+c[1].toFixed(1)).join(' L'));
head.setAttribute('d','M0,0 L'+-u(13)+','+-u(8)+' L'+-u(13)+','+u(8)+'Z');
head.setAttribute('transform','translate('+x.toFixed(1)+' '+y.toFixed(1)+') rotate('+(Math.atan2(y-y0,x-x0)*180/Math.PI).toFixed(1)+')');
const words='Slides '+slideWord(f)+' of the curve a second';
if(caps[3].textContent!==words)caps[3].textContent=words;
placeCaps();
}
// Each caption goes to the free spot nearest its part: clear of the curve with the letters and ticks along it, the dots, the
// bar and the other captions, inside the box. Measured here, on a mode, a setting or a resize, never in the frame step.
function placeCaps(){
const m=svg.getScreenCTM(),r=stage.getBoundingClientRect(),W=stage.clientWidth,H=stage.clientHeight;
const to=p=>[m.a*p.x+m.e-r.left-stage.clientLeft,m.d*p.y+m.f-r.top-stage.clientTop];
const up=Math.max(22,34*k)+8,down=18,keep=[],N=100,done=[],len=share()*total;
for(let i=0;i<=N;i++){
const L=total*i/N,[x,y]=to(pt(L)),[x1,y1]=to(pt(Math.max(L-2,0))),[x2,y2]=to(pt(Math.min(L+2,total))),n=Math.hypot(x2-x1,y2-y1)||1;
for(let s=L<=len+16/k?-BAR-14:-down;s<=up;s+=7)keep.push([x+(y2-y1)/n*s,y+(x1-x2)/n*s]);
}
const anchors=[to(pt(total*.5)),to(pt(total*.3)),to(pt(0)),to(tip)];
const spot=(w,h,[ax,ay])=>{ // the free place nearest the part, for a box of this size
const cand=[];
for(let y=4;y<=H-h-4;y+=6)for(let x=4;x<=W-w-4;x+=6)cand.push([Math.hypot(x+w/2-ax,y+h/2-ay),x,y]);
cand.sort((a,b)=>a[0]-b[0]);
return cand.find(([,x,y])=>!keep.some(([a,b])=>a>x-5&&a<x+w+5&&b>y-5&&b<y+h+5)&&!done.some(([a,b,c2,d])=>x<a+c2+4&&x+w>a-4&&y<b+d+4&&y+h>b-4));
};
[3,2,0,1].forEach(i=>{ // the slide and the dots first: their parts are the most crowded
const c=caps[i];
c.classList.remove('gone');c.style.whiteSpace=c.style.maxWidth='';
if(i===1&&!tickN){c.classList.add('gone');return}
let w=c.offsetWidth,f=spot(w,c.offsetHeight,anchors[i]);
if(!f){ // no room on one line: two lines
c.style.whiteSpace='normal';c.style.maxWidth=Math.min(W-8,Math.round(w*.68))+'px';
w=c.offsetWidth;f=spot(w,c.offsetHeight,anchors[i]);
}
if(!f){c.classList.add('gone');return}
c.style.transform='translate('+f[1]+'px,'+f[2]+'px)';done.push([f[1],f[2],w,c.offsetHeight]);
});
}
// The ticks sit where each copy of the phrase starts, worked out from the same offset and copy length as the text itself
function drawTicks(){
const gap=unit/100*total,first=(offset-unit)/100*total;
for(let i=0;i<ticks.length;i++){
const t=ticks[i],L=first+i*gap,o=i<tickN?Math.max(0,Math.min((L*k-fadeAt)/14,(total*k-fadeAt-L*k)/14,1)):0;
if(o!==t.o){t.setAttribute('opacity',o.toFixed(2));t.o=o}
if(o>0){const p=pt(L),q=pt(L+3);t.setAttribute('transform','translate('+p.x.toFixed(1)+' '+p.y.toFixed(1)+') rotate('+(Math.atan2(q.y-p.y,q.x-p.x)*180/Math.PI).toFixed(1)+')')}
}
}
function drawLive(){
if(mode==='compare')ln.tp.setAttribute('startOffset',((offset-unit)/100*total).toFixed(1));
else if(mode==='inside')drawTicks();
}
function refreshView(){
if(svg.getAttribute('aria-label')!==LABEL[mode])svg.setAttribute('aria-label',LABEL[mode]);
if(mode!=='compare'&&svg.getAttribute('viewBox')!=='0 0 640 360')svg.setAttribute('viewBox','0 0 640 360');
if(mode==='compare')layoutCompare();
else if(mode==='inside')layoutInside();
describe();drawLive();
}
new ResizeObserver(()=>{if(mode!=='normal')refreshView()}).observe(stage);
fill();
start();
if(document.fonts)document.fonts.ready.then(fill); // the site font can still be loading when fill() first runs
</script>
</body>
</html>