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>SVG Line Draw on Scroll — Animation Handbook</title>
<meta name="description" content="A line draws itself along a route as you scroll. Best for timelines.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/svg-line-draw/">
<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="SVG Line Draw on Scroll — Animation Handbook">
<meta property="og:description" content="A line draws itself along a route as you scroll. Best for timelines.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/svg-line-draw/">
<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="SVG Line Draw on Scroll — Animation Handbook">
<meta name="twitter:description" content="A line draws itself along a route as you scroll. Best for timelines.">
<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": "SVG Line Draw on Scroll", "name": "SVG Line Draw on Scroll", "description": "A line draws itself along a route as you scroll. Best for timelines.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/svg-line-draw/", "inLanguage": "en", "articleSection": "Scroll-Based", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Scroll-Based, SVG Line Draw on Scroll"}</script>
<style>
:root{--bg:#0b0b0d;--ui-accent:#6ea8ff;--ui-text:#f4f4f2;--ui-muted:#8a8a92}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.stage{position:relative;overflow-y:auto;scrollbar-width:none;background:#0b0b0d}
.stage::-webkit-scrollbar{display:none}
/* The route lives IN the scrolling content — no pinning. Scrolling travels down it. */
.scene{padding:clamp(10px,2vw,20px)}
.scene svg{display:block;width:100%;height:auto;overflow:visible}
.scene-cap{text-align:center;font-size:13px;color:var(--ui-muted);padding:8px 0 16px}
.guide{fill:none;stroke:rgba(255,255,255,.35);stroke-width:2;stroke-dasharray:5 7;stroke-linecap:round}
.trail{fill:none;stroke:var(--ui-accent);stroke-width:3;stroke-linecap:round;stroke-linejoin:round}
.dot{fill:var(--bg);stroke:var(--ui-accent);stroke-width:2.5}
/* paint-order draws a stage-coloured outline under each label, so text stays
legible where the line runs behind it */
.wp-label{font-size:14px;font-weight:600;fill:var(--ui-text);
paint-order:stroke;stroke:var(--bg);stroke-width:5px;stroke-linejoin:round}
.wp-sub{font-size:12px;fill:var(--ui-muted);
paint-order:stroke;stroke:var(--bg);stroke-width:5px;stroke-linejoin:round}
.pop{opacity:0;transform:scale(.6);transform-box:fill-box;transform-origin:center;
transition:transform .35s cubic-bezier(.34,1.56,.64,1),opacity .25s ease-out}
.wp.on .pop{opacity:1;transform:scale(1)}
/* a jump (Back to top, Play starting again) changes the stops at once, so none lingers after its line has gone */
.jump .pop{transition:none}
.no-wps .wps{display:none}
@media(prefers-reduced-motion:reduce){.pop{transition:none}}
/* See inside and Compare draw in one zero-tall layer that sticks to the top of the box, so the scroll length never changes.
It ignores the pointer, so a wheel turn or a touch over it still scrolls the box. The ticks and the pen tip are drawn
inside the route's own drawing, so they scroll with it. */
.fx,.fx-in,.fx-cmp,.cp,.ticks,.tip{display:none}
.fx{position:sticky;top:0;height:0;z-index:6;pointer-events:none}
.fx-box{position:absolute;left:0;top:0;width:100%;height:var(--bh,300px);overflow:hidden}
.fx-in,.fx-cmp{position:absolute;inset:0}
.stage[data-hb-mode="inside"] .fx,.stage[data-hb-mode="compare"] .fx,.stage[data-hb-mode="inside"] .fx-in,.stage[data-hb-mode="compare"] .fx-cmp{display:block}
.stage[data-hb-mode="inside"],.stage[data-hb-mode="compare"]{overflow-anchor:none}
.cap{position:absolute;padding:2px 7px;border-radius:999px;white-space:nowrap;background:rgba(11,11,13,.95);color:#f4f4f2;
border:1px solid rgba(244,244,242,.3);font:600 12.5px/1.3 var(--hb-font)}
/* See inside: the route cut into tenths, the pen tip, and a gauge of two bars that share one length */
.stage[data-hb-mode="inside"] .ticks,.stage[data-hb-mode="inside"] .tip{display:inline}
.ticks line{stroke:#f4f4f2;stroke-width:2.5;stroke-linecap:round;vector-effect:non-scaling-stroke}
.tip circle{fill:#fff;stroke:var(--ui-accent);stroke-width:3.5}
.gauge{position:absolute;left:8px;top:8px;width:130px;display:flex;flex-direction:column;align-items:flex-start;gap:3px}
@media(min-width:601px){.gauge{width:160px}}
.gauge .cap{position:static}
.gauge .cap+.bar{margin-bottom:5px}
.bar{align-self:stretch;height:8px;border-radius:4px;overflow:hidden;background:rgba(11,11,13,.95);box-shadow:0 0 0 1px rgba(244,244,242,.4)}
.bar i{display:block;height:100%;transform-origin:left;transform:scaleX(0)}
.bar-s i{background:#b8b8c0}
.bar-d i{background:var(--ui-accent)}
.gauge .cap-tk{display:flex;align-items:center;gap:6px;border-radius:10px;padding:3px 7px}
.cap-tk i{width:15px;height:14px;flex:none;background:repeating-linear-gradient(90deg,#f4f4f2 0 2px,transparent 2px 6.5px)}
/* Compare: an inert copy of the drawing lies exactly over the real one, clipped to the left half of the box: every part of
the route solid, every stop shown. It adds no height, and the real drawing shows through on the right. */
.stage[data-hb-mode="compare"] .scene{position:relative}
.stage[data-hb-mode="compare"] .cp{display:block;position:absolute;inset:0;z-index:1;pointer-events:none;background:var(--bg);
padding:clamp(10px,2vw,20px);clip-path:inset(0 50% 0 0)}
.cp .pop{transition:none}
.cm-top{position:absolute;left:0;right:0;top:0;height:34px;background:rgba(11,11,13,.96)}
.cm-div{position:absolute;left:50%;top:0;bottom:0;width:2px;margin-left:-1px;background:#f4f4f2}
.cm-n1{top:8px;left:8px}
.cm-n2{top:8px;left:calc(50% + 8px)}
</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="scroll" 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="../scroll-velocity-skew/" rel="prev" aria-label="Previous: Scroll Velocity Skew"><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>Scroll Velocity Skew</span></a><a href="../scrollspy-nav/" rel="next" aria-label="Next: Scrollspy Navigation"><span><span class="hb-dir">Next: </span>Scrollspy Navigation</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">01.24 · Scroll-Based</p>
<h1>SVG Line Draw on Scroll</h1>
<p class="hb-lede">A line draws itself along a route as you scroll. Best for timelines.</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">Scroll it</h2><p>Scroll inside the box, or press Play and it scrolls for you.</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" id="stage" tabindex="0" role="region" aria-label="A route that draws itself as you scroll">
<div class="fx" inert aria-hidden="true"><div class="fx-box">
<div class="fx-in"><div class="gauge">
<span class="cap" data-hb-caption>Scrolled</span><i class="bar bar-s"><i></i></i>
<span class="cap" data-hb-caption>Line drawn</span><i class="bar bar-d"><i></i></i>
<span class="cap cap-tk" data-hb-caption><i></i><span>Ticks: every tenth<br>of the route</span></span>
</div></div>
<div class="fx-cmp"><i class="cm-top"></i><i class="cm-div"></i>
<span class="cap cm-n1" data-hb-caption>Already drawn</span><span class="cap cm-n2" data-hb-caption>Draws on scroll</span>
</div>
</div></div>
<div class="scene">
<svg id="map" viewBox="0 0 600 2200" preserveAspectRatio="xMidYMin meet" aria-hidden="true">
<!-- faint dashed guide shows the full route before it is drawn -->
<path class="guide" d="M 300 80 C 510 160 530 300 340 400 C 150 500 90 620 260 730 C 440 840 520 960 340 1080 C 160 1200 100 1310 290 1410 C 490 1510 510 1650 310 1760 C 110 1870 150 2020 300 2120"/>
<path id="trail" class="trail" d="M 300 80 C 510 160 530 300 340 400 C 150 500 90 620 260 730 C 440 840 520 960 340 1080 C 160 1200 100 1310 290 1410 C 490 1510 510 1650 310 1760 C 110 1870 150 2020 300 2120"/>
<g id="wps" class="wps">
<g class="wp" data-f="0.05"><g class="pop"><circle class="dot" r="7"/><text class="wp-label" x="-20" y="1" text-anchor="end">Depart</text><text class="wp-sub" x="-20" y="17" text-anchor="end">km 0 — trailhead</text></g></g>
<g class="wp" data-f="0.27"><g class="pop"><circle class="dot" r="7"/><text class="wp-label" x="20" y="1">River crossing</text><text class="wp-sub" x="20" y="17">km 12 — footbridge</text></g></g>
<g class="wp" data-f="0.50"><g class="pop"><circle class="dot" r="7"/><text class="wp-label" x="20" y="1">Basecamp</text><text class="wp-sub" x="20" y="17">km 27 — overnight</text></g></g>
<g class="wp" data-f="0.73"><g class="pop"><circle class="dot" r="7"/><text class="wp-label" x="-20" y="1" text-anchor="end">Summit ridge</text><text class="wp-sub" x="-20" y="17" text-anchor="end">km 39 — 2 410 m</text></g></g>
<g class="wp" data-f="0.95"><g class="pop"><circle class="dot" r="7"/><text class="wp-label" x="20" y="1">Arrive</text><text class="wp-sub" x="20" y="17">km 46 — journey's end</text></g></g>
</g>
<g class="tip"><circle r="7"/></g>
<g class="ticks"><line/><line/><line/><line/><line/><line/><line/><line/><line/></g>
</svg>
<div class="scene-cap">— end of route —</div>
</div>
</div>
<div class="hb-player">
<button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>Play</button>
<button class="hb-play" type="button" id="btn-top" data-hb-top><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>Back to top</button>
</div>
<div class="hb-mode-notes" aria-live="polite">
<p data-hb-for="inside" hidden>Inside, ticks cut the route into tenths and the bright dot is the pen tip; the bars compare how far you scrolled with how much is drawn. Scroll the box or press Play to watch.</p>
<p data-hb-for="compare" hidden>Already drawn shows the whole route from the start; Draws on scroll draws the line as you go. Scroll the box or press Play to watch.</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 scroll again or press Play.</p></div>
</div>
<div class="hb-settings">
<div class="hb-setting">
<p class="hb-setting-name" id="feel-lbl">Feel</p>
<div class="seg" id="feel-seg" role="group" aria-labelledby="feel-lbl">
<button type="button" data-ease="linear" class="on" aria-pressed="true">Even</button>
<button type="button" data-ease="out" aria-pressed="false">Smooth</button>
</div>
<p class="hb-hint">Even keeps the tip with your scroll; Smooth runs ahead.</p>
</div>
<div class="hb-setting">
<label class="hb-switch-row"><span>Shows the stops</span><input class="hb-switch" type="checkbox" role="switch" id="wp-tog" checked autocomplete="off"></label>
<p class="hb-hint">Labeled points pop in as the line reaches them.</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="finish-lbl">Line finishes</p>
<div class="seg" id="finish-seg" role="group" aria-labelledby="finish-lbl">
<button type="button" data-at="0.7" aria-pressed="false">Early</button>
<button type="button" data-at="0.85" aria-pressed="false">Near the end</button>
<button type="button" data-at="1" class="on" aria-pressed="true">At the end</button>
</div>
<p class="hb-hint">Early leaves the whole route on show for a while.</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 scroll-drawn route to [your timeline, journey or process steps]. Draw a long winding path down the page with a faint dashed copy underneath, and reveal the solid line in step with the scroll so its tip travels down with the visitor. Measure the path's length once and reveal it by sliding a single dash that is exactly as long as the path. When the settings include them, place labeled stops along the path and pop each one in as the line reaches it; scrolling back up erases the line and hides them again. If the visitor has reduced motion turned on, show each stop without the pop. 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>Timelines</li><li>Journey maps</li><li>Process steps</li><li>Delivery tracking</li></ul>
<h3>Avoid on</h3>
<ul class="hb-tags hb-avoid"><li>Short 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 stage=document.getElementById('stage'),trail=document.getElementById('trail'),map=document.getElementById('map');
const wpTog=document.getElementById('wp-tog');
const clamp=(v,a,b)=>Math.min(Math.max(v,a),b);
const EASES={linear:t=>t,out:t=>1-Math.pow(1-t,3)};
// See inside and Compare. The mode is drawn at once, wherever the box is; nothing here scrolls, moves focus or presses a control.
let mode='normal',easeMode='linear',completeAt=1;
const LABELS={
normal:'A route that draws itself as you scroll',
inside:'A route that draws itself as you scroll, cut into tenths, with its pen tip and bars for how far you scrolled and how much is drawn',
compare:'The route split down the middle: already drawn on the left, drawing as you scroll on the right'
};
const compareNote=document.querySelector('[data-hb-for="compare"]'),COMPARE_NOTE=compareNote.textContent;
const AHEAD_NOTE='Here the line runs ahead of the box, so Already drawn and Draws on scroll often match where you look. Pick Even and At the end in Try it to see the tip.';
function setNote(){compareNote.textContent=easeMode==='linear'&&completeAt===1?COMPARE_NOTE:AHEAD_NOTE}
document.addEventListener('hb:mode',e=>{
mode=e.detail.mode;
stage.setAttribute('aria-label',LABELS[mode]);
setNote();measure();update();
});
// Waypoints are placed AT known fractions of the path, so their trigger points are
// just LEN * fraction — no per-scroll geometry lookups ever.
const wps=[...document.querySelectorAll('.wp')].map(g=>({g,f:parseFloat(g.dataset.f),len:0,y:0,on:false}));
// Layout reads live here only — at init, after the fonts load and on resize, never per scroll.
// k is how many drawing units make one pixel on screen: every stop is scaled by it, so its dot and labels keep the same
// size on every screen while the route itself scales. One pixel less than the box scrolls: both sizes are rounded, so the
// true end can fall a fraction short of the measured one, and the line must still finish there.
// w.y is where each stop's dot sits in the scrolling content, measured from the inside of the box's border.
// Compare's copy of the drawing is made once: no ids, inert, hidden from screen readers, every stop shown and none popping
const cp=document.createElement('div');
cp.className='cp';cp.setAttribute('inert','');cp.setAttribute('aria-hidden','true');
const copy=map.cloneNode(true);
copy.removeAttribute('id');
copy.querySelectorAll('[id]').forEach(el=>el.removeAttribute('id'));
copy.querySelectorAll('.ticks,.tip').forEach(el=>el.remove());
const copyWps=[...copy.querySelectorAll('.wp')];
copyWps.forEach(g=>g.classList.add('on'));
// the copy's opaque half also holds the end caption, so each half shows it whole
cp.append(copy,document.querySelector('.scene-cap').cloneNode(true));map.parentNode.append(cp);
// The tip and the ticks come from points sampled along the route here, once, so a scroll only reads a table
const ticks=[...map.querySelectorAll('.ticks line')],tip=map.querySelector('.tip'),NS=240;
const gauge=stage.querySelector('.gauge'),barS=gauge.querySelector('.bar-s i'),barD=gauge.querySelector('.bar-d i');
let PTS=[];
let LEN=1,MAXS=1,BOX=1,k=1;
function measure(){
LEN=trail.getTotalLength();
const rect=map.getBoundingClientRect();
k=rect.width>0?600/rect.width:1;
MAXS=Math.max(1,stage.scrollHeight-stage.clientHeight-1);
BOX=stage.clientHeight;
const svgTop=rect.top-stage.getBoundingClientRect().top-stage.clientTop+stage.scrollTop;
trail.style.strokeDasharray=LEN;
stage.style.setProperty('--bh',BOX+'px');
wps.forEach((w,i)=>{
w.len=LEN*w.f;
const pt=trail.getPointAtLength(w.len);
w.y=svgTop+pt.y/k;
const place=`translate(${pt.x.toFixed(1)} ${pt.y.toFixed(1)}) scale(${k.toFixed(4)})`;
w.g.setAttribute('transform',place);
copyWps[i].setAttribute('transform',place);
});
PTS=[];
for(let i=0;i<=NS;i++){const pt=trail.getPointAtLength(LEN*i/NS);PTS.push(pt.x,pt.y)}
ticks.forEach((ln,i)=>{
const at=LEN*(i+1)/10,a=trail.getPointAtLength(at-1),b=trail.getPointAtLength(at+1),c=trail.getPointAtLength(at);
const dx=b.x-a.x,dy=b.y-a.y,n=Math.hypot(dx,dy)||1,h=17*k;
ln.setAttribute('x1',(c.x+dy/n*h).toFixed(1));ln.setAttribute('y1',(c.y-dx/n*h).toFixed(1));
ln.setAttribute('x2',(c.x-dy/n*h).toFixed(1));ln.setAttribute('y2',(c.y+dx/n*h).toFixed(1));
});
}
let ticking=false,jumped=false,lastTop=0;
function update(){
const top=stage.scrollTop;
// a jump is a click on Back to top or Play, or any move of more than half a box in one frame
const jump=jumped||Math.abs(top-lastTop)>BOX/2;
jumped=false;lastTop=top;
const scrolled=clamp(top/MAXS,0,1),p=EASES[easeMode](scrolled);
// By default the line finishes as the scroll does, so its tip stays in the box while it is drawn. A smaller completeAt
// finishes it earlier and puts the tip ahead of the box: the rest of the scroll shows the finished route.
// Clamp, don't gate: jumping to the end draws everything, back to top erases it.
const drawn=clamp(LEN*p/completeAt,0,LEN);
trail.style.strokeDashoffset=LEN-drawn;
if(mode==='inside'){
// both bars and the tip come from the same drawn length and share, so a jump draws them at once
barS.style.transform=`scaleX(${scrolled})`;
barD.style.transform=`scaleX(${drawn/LEN})`;
const f=drawn/LEN*NS,i=Math.min(NS-1,Math.floor(f)),t=f-i;
tip.setAttribute('transform',`translate(${(PTS[2*i]+(PTS[2*i+2]-PTS[2*i])*t).toFixed(1)} ${(PTS[2*i+1]+(PTS[2*i+3]-PTS[2*i+1])*t).toFixed(1)}) scale(${k.toFixed(4)})`);
}
if(jump)map.classList.add('jump');
// A stop shows once the line has reached it and it has come into view, so its pop happens where the visitor is looking:
// the line can be drawn well ahead of the box, and a stop that pops below its bottom edge would pop unseen. The 16px
// is how far the stop's label reaches above its dot.
for(const w of wps){
const on=drawn>=w.len&&w.y-16<top+BOX;
if(on!==w.on){w.on=on;w.g.classList.toggle('on',on)} // toggle only on change
}
if(jump){void map.getBoundingClientRect();map.classList.remove('jump')} // apply the change now, without a transition
ticking=false;
}
stage.addEventListener('scroll',()=>{if(!ticking){requestAnimationFrame(update);ticking=true}},{passive:true});
// The shared script moves the box while it handles the click, before the click reaches the document: draw the new place now,
// in the same task, so not even one frame shows the old line or stops.
document.addEventListener('click',e=>{if(e.target.closest('#btn-top,#btn-scroll')){jumped=true;update()}});
// the box or the drawing changes size under the same scroll position, so measure again and draw at once
window.addEventListener('resize',()=>{measure();update()});
if(document.fonts&&document.fonts.ready)document.fonts.ready.then(()=>{measure();update()});
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('feel-seg',d=>{easeMode=d.ease;setNote();update()});
choices('finish-seg',d=>{completeAt=+d.at;setNote();update()});
wpTog.addEventListener('change',()=>{map.classList.toggle('no-wps',!wpTog.checked);copy.classList.toggle('no-wps',!wpTog.checked)});
measure();update();
</script>
</body>
</html>