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: Shatter Effect (02.14): reduced motion switched on mid-play ends the play at once, the Loop waits while the stage is out of view
matind5592afe61bd
diff --git a/animations/02-entrance-and-exit/shatter-effect/README.md b/animations/02-entrance-and-exit/shatter-effect/README.mdindex 541adf3f..295faca4 100644--- a/animations/02-entrance-and-exit/shatter-effect/README.md+++ b/animations/02-entrance-and-exit/shatter-effect/README.md@@ -45,7 +45,7 @@ for(let k=0;k<=12;k++){el.animate(frames,{duration:T,delay:start+wave*d/far,fill:'both'});```-Each piece waits until the break (`start`, the time the cracks take), then a little longer the farther it is from the impact, so the break spreads outward; a short flash marks the impact. With the cracks shown, the crack lines are first drawn in an SVG over the card, ring by ring, while the card gives a small jolt. They are a soft dark line under a light one, so they show on both the picture and the caption, and every copy carries the same lines along its own edges: nothing changes at the break, and each piece keeps a light edge where it broke (lines this soft and wide stay clean when a piece tilts; a thin, sharp pair breaks up into a sawtooth). The next card fades in from 45% of the flight. When the last piece has faded, the pieces are removed; Replay or a setting change cancels everything still running (`getAnimations({subtree:true})`) and starts the same break over. Under reduced motion the next card simply fades in over the old one, with no pieces. Slow motion makes the play three times longer; the hold between plays while Loop is on stays the same. If the stage changes size during a play (a resized window, a phone turned sideways), the play ends at once on the next card, because its pieces were placed for the old size.+Each piece waits until the break (`start`, the time the cracks take), then a little longer the farther it is from the impact, so the break spreads outward; a short flash marks the impact. With the cracks shown, the crack lines are first drawn in an SVG over the card, ring by ring, while the card gives a small jolt. They are a soft dark line under a light one, so they show on both the picture and the caption, and every copy carries the same lines along its own edges: nothing changes at the break, and each piece keeps a light edge where it broke (lines this soft and wide stay clean when a piece tilts; a thin, sharp pair breaks up into a sawtooth). The next card fades in from 45% of the flight. When the last piece has faded, the pieces are removed; Replay or a setting change cancels everything still running (`getAnimations({subtree:true})`) and starts the same break over. Under reduced motion the next card simply fades in over the old one, with no pieces. Slow motion makes the play three times longer; the hold between plays while Loop is on stays the same. If the stage changes size during a play (a resized window, a phone turned sideways), the play ends at once on the next card, because its pieces were placed for the old size; reduced motion switched on during a play ends it the same way. While Loop is on and the stage is scrolled out of view, the next play waits until the stage is back in view (an `IntersectionObserver`), so no copies are built while nobody can see them.## Key parameters@@ -61,6 +61,7 @@ Each piece waits until the break (`start`, the time the cracks take), then a lit## Production notes- **Copies are the cost.** Each piece holds a full copy of the card, so sixty pieces mean sixty copies of its markup. Keep the card light (one picture and a short caption), make the copies only for the card that is about to break, give each copy only its own crack lines (a full crack pattern in every copy is stroked again for every piece) and remove the copies once they have faded. The demo cuts Many a little coarser on phones (a screen up to 600px wide or 500px tall) and skips slivers under 4px² there.- **Composited, not repainted.** The pieces animate only `transform` and `opacity`; each `clip-path` is set once and never animated. `contain: layout paint` on each piece keeps its layer to its own small box instead of the whole card.+- **Build nothing out of view.** Building the copies takes one long frame on a slow phone, so the demo's Loop holds its next play while the stage is off screen; otherwise that frame would land every few seconds while the visitor reads or taps elsewhere on the page.- **Build early, swap on the same clock.** Making the copies and starting their animations takes the most time, so do it before anything moves (here while the card is still whole) and keep the pieces under the card. Hide the card with an animation too, not a timer: animations made together start together, while a timer keeps running during a slow first frame and would hide the card too early.- **Pictures and video.** A copied `img` shows the same cached picture, but a copied `video` or `canvas` does not carry its current frame: draw that frame to an image first. For any element, html2canvas can turn it into one bitmap that the pieces share as a background, which is also lighter than copying markup.- **Accessibility.** The pieces are decoration: keep them hidden from assistive technology (here they sit inside the stage's `role="img"`) and move focus to whatever comes next yourself.diff --git a/animations/02-entrance-and-exit/shatter-effect/index.html b/animations/02-entrance-and-exit/shatter-effect/index.htmlindex 7f267fbf..2293237c 100644--- a/animations/02-entrance-and-exit/shatter-effect/index.html+++ b/animations/02-entrance-and-exit/shatter-effect/index.html@@ -42,8 +42,7 @@/* A piece is a box around its polygon, holding a copy of the whole card clipped to that polygon. The pieces wait underthe cracking card (z-index 2) until the break and fly in front of the next card. */.shards{position:absolute;z-index:1;inset:0;perspective:900px;pointer-events:none}- .shard{position:absolute;contain:layout paint;will-change:transform,opacity}- .shard .card{position:absolute;box-shadow:none}+ .shard{position:absolute;contain:layout paint;will-change:transform,opacity}.shard .card{position:absolute;box-shadow:none}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=6"><script src="../../../assets/js/demo-page.js?v=6" defer></script>@@ -185,7 +184,7 @@const CUTS={few:[3,2],medium:[6,3],many:[11,4]},RINGS={2:[.45,1],3:[.24,.56,1],4:[.15,.36,.64,1]};// One flight, in card heights: how far the pieces burst outward, how high they are thrown and how far gravity pulls themconst PULL={none:[1.1,0,0],normal:[.75,.6,2.3],heavy:[.55,.3,4.4]};- let pieces='medium',from='center',gravity='normal',ms=1100,cur=0,playing=-1,size='',timers=[];+ let pieces='medium',from='center',gravity='normal',ms=1100,cur=0,playing=-1,size='',seen=true,waiting=false,timers=[];const later=(fn,wait)=>timers.push(setTimeout(fn,wait)),slow=v=>slowTog.checked?v*3:v,rnd=(a,b)=>a+Math.random()*(b-a),P=v=>v[0].toFixed(1)+' '+v[1].toFixed(1);// Cuts a W×H card like glass hit at one point: rays run to the edge (through the four corners, so outer pieces end exactly@@ -219,9 +218,8 @@[...svg.children].forEach((g,i)=>g.animate([{opacity:0},{opacity:1}],{duration:crack*.3,delay:crack*.14*i,fill:'backwards'}));}// Every piece is a box around its polygon holding a copy of the whole card, clipped to the polygon (grown by 0.7px so- // neighbors overlap and no seam shows), with only its own cracks drawn. Its flight is sampled into keyframes: a burst that slows- // down, an upward throw and the pull of gravity; it turns all the way and fades over the second half. It starts at the- // break (start), and nearer pieces go first.+ // neighbors overlap and no seam shows), with only its own cracks drawn. Its flight is sampled into keyframes: a burst that+ // slows, an upward throw and gravity; it turns all the way and fades over its second half. It starts at the break, nearer pieces first.function shatter(card,{p,polys,box:[X,Y,W,H]},start){const T=slow(ms),wave=slow(ms*.16);const [push,lift,grav]=PULL[gravity],tumble=tumbleTog.checked,layer=Object.assign(document.createElement('div'),{className:'shards'});@@ -252,7 +250,7 @@}// Puts every card back at rest: the pieces, cracks and running animations go, and only the card on show is visible.function reset(){- timers.forEach(clearTimeout);timers=[];+ timers.forEach(clearTimeout);timers=[];waiting=false;deck.getAnimations({subtree:true}).forEach(a=>a.cancel());deck.querySelectorAll('.shards').forEach(n=>n.remove());cards.forEach((c,i)=>{c.classList.toggle('on',i===cur);c.style.zIndex='';c.querySelector('.cracks').textContent=''});@@ -279,12 +277,14 @@// Once all of it has played, the next card is the one on show (a reset in between cancels the play, and this with it)Promise.all(deck.getAnimations({subtree:true}).map(a=>a.finished)).then(()=>settle(next),()=>{});}- function settle(i){playing=-1;cur=i;reset();if(loopTog.checked&&!reduce.matches)later(()=>{if(loopTog.checked)play()},1000)}- // The pieces are placed for the stage's size when the play starts: if the stage changes size during a play, it ends at once+ // Loop's next play comes a second after a play ends, or, with the stage scrolled out of view, once it is back in view+ function settle(i){playing=-1;cur=i;reset();if(loopTog.checked&&!reduce.matches)later(()=>{if(loopTog.checked)seen?play():waiting=true},1000)}+ new IntersectionObserver(es=>{seen=es[es.length-1].isIntersecting;if(seen&&waiting&&loopTog.checked&&!reduce.matches)play()}).observe(deck);+ // A play ends at once on the next card when the stage changes size (its pieces were placed for the old size) or reduced motion comes onnew ResizeObserver(()=>{const now=deck.offsetWidth+'x'+deck.offsetHeight;if(size&&now!==size&&playing>=0)settle(playing);size=now}).observe(deck);+ reduce.addEventListener('change',()=>{if(reduce.matches&&playing>=0)settle(playing)});- document.getElementById('btn-play').addEventListener('click',play);- loopTog.addEventListener('change',()=>{if(loopTog.checked)play()});+ document.getElementById('btn-play').addEventListener('click',play);loopTog.addEventListener('change',()=>{if(loopTog.checked)play()});function choices(id,pick){const buttons=document.querySelectorAll('#'+id+' button');buttons.forEach(b=>b.addEventListener('click',()=>{