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
fix: pop SVG Line Draw's stops where the visitor can see them and clear them at once after a jump
matinad7fb84cab6f
diff --git a/animations/01-scroll-based/svg-line-draw/README.md b/animations/01-scroll-based/svg-line-draw/README.mdindex 5cd93733..6555ed5c 100644--- a/animations/01-scroll-based/svg-line-draw/README.md+++ b/animations/01-scroll-based/svg-line-draw/README.md@@ -30,19 +30,22 @@ path.style.strokeDashoffset = LEN - drawn; // reveal the travelled poWaypoints are placed at known fractions of the path, so their positions and trigger points are computed once from those fractions — never per scroll event:```js-// at init: position each waypoint and store its length threshold+// at init: position each waypoint and store its length threshold and where its dot sits in the scrolling contentwp.len = LEN * wp.fraction;const pt = path.getPointAtLength(wp.len);wp.group.setAttribute('transform', `translate(${pt.x} ${pt.y}) scale(${k})`);+wp.y = svgTop + pt.y / k;-// per frame: a cheap comparison, class toggled only when the state changes-const on = drawn >= wp.len;+// per frame: two cheap comparisons, class toggled only when the state changes+const on = drawn >= wp.len && wp.y - 16 < scrollTop + boxHeight;if (on !== wp.on) { wp.on = on; wp.group.classList.toggle('on', on); }```Each stop is also scaled by `k`, which is 600 divided by the drawing's width on screen, so its dot and labels keep the same size on every screen while the route itself scales.-The pop itself is CSS — `.on` transitions the waypoint from `scale(.6)` and `opacity: 0` to full size, and scrolling back past a waypoint removes the class so it un-pops. Progress is clamped, never gated: jumping straight to the bottom draws the full route, returning to the top erases it, and there is no state to get stuck in between. A faint dashed copy of the path sits underneath the drawing stroke, so the reader always sees where the route is going before the line gets there.+A stop also waits until it has come into view, meaning its top edge is above the bottom edge of the box. The route is many boxes tall, so the line can be drawn well ahead of the box, and a stop that popped below the bottom edge would pop where nobody can see it; this way its pop plays as it scrolls in. A still picture is the same either way, because a stop that is below the box cannot be seen.++The pop itself is CSS — `.on` transitions the waypoint from `scale(.6)` and `opacity: 0` to full size, and scrolling back past a waypoint removes the class so it un-pops. After a jump — Back to top, or Play starting again from the top — the transition is switched off for that one update, so no stop fades out after the line that led to it has gone. Progress is clamped, never gated: jumping straight to the bottom draws the full route, returning to the top erases it, and there is no state to get stuck in between. A faint dashed copy of the path sits underneath the drawing stroke, so the reader always sees where the route is going before the line gets there.## Key parametersdiff --git a/animations/01-scroll-based/svg-line-draw/index.html b/animations/01-scroll-based/svg-line-draw/index.htmlindex 3fa9b624..1f9f18e7 100644--- a/animations/01-scroll-based/svg-line-draw/index.html+++ b/animations/01-scroll-based/svg-line-draw/index.html@@ -41,6 +41,8 @@.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}}</style>@@ -167,7 +169,7 @@// 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,on:false}));+ const wps=[...document.querySelectorAll('.wp')].map(g=>({g,f:parseFloat(g.dataset.f),len:0,y:0,on:false}));let easeMode='linear',completeAt=0.85;@@ -175,37 +177,53 @@// 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.- let LEN=1,MAXS=1,k=1;+ // w.y is where each stop's dot sits in the scrolling content, measured from the inside of the box's border.+ let LEN=1,MAXS=1,BOX=1,k=1;function measure(){LEN=trail.getTotalLength();- const width=map.getBoundingClientRect().width;- k=width>0?600/width:1;+ 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;for(const w of wps){w.len=LEN*w.f;const pt=trail.getPointAtLength(w.len);+ w.y=svgTop+pt.y/k;w.g.setAttribute('transform',`translate(${pt.x.toFixed(1)} ${pt.y.toFixed(1)}) scale(${k.toFixed(4)})`);}}- let ticking=false;+ let ticking=false,jumped=false,lastTop=0;function update(){- const p=EASES[easeMode](clamp(stage.scrollTop/MAXS,0,1));+ 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 p=EASES[easeMode](clamp(top/MAXS,0,1));// The line finishes before the scroll does (completeAt < 1), so the reader sees// the route completed rather than leaving it clipped at the viewport edge.// 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(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;+ 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 transitionticking=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 oncewindow.addEventListener('resize',()=>{measure();update()});if(document.fonts&&document.fonts.ready)document.fonts.ready.then(()=>{measure();update()});