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): the Loop hold is a timer again, pieces placed from the card's exact box, a resize ends the play on the next card
matin3354b0a11a1a
diff --git a/animations/02-entrance-and-exit/shatter-effect/README.md b/animations/02-entrance-and-exit/shatter-effect/README.mdindex 3c9827ef..541adf3f 100644--- a/animations/02-entrance-and-exit/shatter-effect/README.md+++ b/animations/02-entrance-and-exit/shatter-effect/README.md@@ -23,7 +23,7 @@ for(let i=0;i<f.length;i++)rays.forEach((_,k)=>{const n=(k+1)%rays.length,q.edge=i===f.length-1;polys.push(q)});```-Every piece is a box around its polygon that holds a copy of the whole card (`cloneNode(true)`), shifted so the copy lines up with the original and clipped to the polygon with `clip-path`. Each polygon is grown by 0.7px so neighbors overlap and no seam shows, and each copy draws only its own cracks (its outline, less any side on the card's border). The pieces are built when the play starts, before anything moves, because that is the costly part; they wait under the card, which is hidden at the break by a zero-length animation, so it keeps time with the pieces' own animations however long that first frame takes:+Every piece is a box around its polygon that holds a copy of the whole card (`cloneNode(true)`), shifted so the copy lines up with the original and clipped to the polygon with `clip-path`. The card's box is measured once with `getBoundingClientRect()` relative to the deck, keeping the fractions of a pixel, so the copies sit exactly where the card is (rounded `offsetTop` and friends put them up to a pixel off). Each polygon is grown by 0.7px so neighbors overlap and no seam shows, and each copy draws only its own cracks (its outline, less any side on the card's border). The pieces are built when the play starts, before anything moves, because that is the costly part; they wait under the card, which is hidden at the break by a zero-length animation, so it keeps time with the pieces' own animations however long that first frame takes:```jsconst el=Object.assign(document.createElement('div'),{className:'shard'}),copy=card.cloneNode(true),o=q.edge?[...q.slice(3),...q.slice(0,3)]:q;@@ -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. While Loop is on, the next break is built as soon as a play ends and every one of its animations waits out the one-second hold (their delays grow by it), so the costly first frame falls while nothing moves, not just before the cracks.+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.## Key parametersdiff --git a/animations/02-entrance-and-exit/shatter-effect/index.html b/animations/02-entrance-and-exit/shatter-effect/index.htmlindex 99447323..7f267fbf 100644--- a/animations/02-entrance-and-exit/shatter-effect/index.html+++ b/animations/02-entrance-and-exit/shatter-effect/index.html@@ -29,8 +29,7 @@.card{grid-area:1/1;position:relative;display:flex;flex-direction:column;height:78cqh;aspect-ratio:4/5;padding:2.6cqh 2.6cqh 0;border-radius:3.4cqh;background:var(--card-bg);color:var(--card-ink);box-shadow:0 2.6cqh 6cqh -1.4cqh rgba(0,0,0,.65);opacity:0;visibility:hidden}.card.on{opacity:1;visibility:visible}- .pic{position:relative;flex:1;border-radius:2cqh;overflow:hidden}- .pic svg{position:absolute;inset:0;width:100%;height:100%}+ .pic{position:relative;flex:1;border-radius:2cqh;overflow:hidden}.pic svg{position:absolute;inset:0;width:100%;height:100%}.night{background:linear-gradient(#06112b,#0d2a55 52%,#16507a)}.dunes{background:linear-gradient(#3a1d5c,#b8476f 40%,#f39a5e 72%,#ffd59c)}.cove{background:linear-gradient(#3d9cf0,#a3d7ff 58%)}.cap{padding:2.2cqh .6cqh 2.8cqh}.cap b{display:block;font-size:max(15px,4.6cqh);font-weight:800;line-height:1.15;letter-spacing:-.01em}@@ -186,14 +185,14 @@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,held=0;- const 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);+ let pieces='medium',from='center',gravity='normal',ms=1100,cur=0,playing=-1,size='',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// on it, and evenly between), rings cross them, and each crack between rings bends once, at most a third of the way to the// nearest other crack (no piece folds over itself). A piece lies between two rays and two rings; neighbors share corners.- function cut(W,H){- const TAU=2*Math.PI,p=from==='center'?[W*rnd(.45,.55),H*rnd(.36,.46)]:[W*.15,H*.13];+ function cut(box){+ const [,,W,H]=box,TAU=2*Math.PI,p=from==='center'?[W*rnd(.45,.55),H*rnd(.36,.46)]:[W*.15,H*.13];const extra=pieces==='many'&&phone.matches?8:CUTS[pieces][0],f=RINGS[CUTS[pieces][1]];const corners=[[0,0],[W,0],[W,H],[0,H]].map(([x,y])=>(Math.atan2(y-p[1],x-p[0])+TAU)%TAU).sort((a,b)=>a-b);const rays=corners.flatMap((c,i)=>{const span=(corners[(i+1)%4]-c+TAU)%TAU,m=Math.round(extra*span/TAU);@@ -207,12 +206,12 @@const polys=[];for(let i=0;i<f.length;i++)rays.forEach((_,k)=>{const n=(k+1)%rays.length,q=[grid[i][k],bends[i][k],grid[i+1][k],grid[i+1][n],bends[i][n]].concat(i?[grid[i][n]]:[]);q.edge=i===f.length-1;polys.push(q)}); // edge: the stretch from its third to its fourth corner is the card's own border- return {p,grid,bends,polys};+ return {p,grid,bends,polys,box};}// The cracks, ring by ring from the break outward (the card's own edge is not a crack)- function crackLines(card,{grid,bends},crack){+ function crackLines(card,{grid,bends,box},crack){const svg=card.querySelector('.cracks');- svg.setAttribute('viewBox','0 0 '+card.offsetWidth+' '+card.offsetHeight);+ svg.setAttribute('viewBox','0 0 '+box[2]+' '+box[3]);svg.innerHTML=bends.map((ring,i)=>{const d=ring.map((v,k)=>'M'+P(grid[i][k])+'L'+P(v)+'L'+P(grid[i+1][k])).join('')+(i<bends.length-1?'M'+grid[i+1].map(P).join('L')+'Z':'');return '<g><path class="dk" d="'+d+'"/><path class="lt" d="'+d+'"/></g>';@@ -223,8 +222,8 @@// 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.- function shatter(card,{p,polys},start){- const W=card.offsetWidth,H=card.offsetHeight,X=card.offsetLeft,Y=card.offsetTop,T=slow(ms),wave=slow(ms*.16);+ 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'});const mid=polys.map(q=>q.reduce((m,v)=>[m[0]+v[0]/q.length,m[1]+v[1]/q.length],[0,0]));const far=Math.max(...mid.map(c=>Math.hypot(c[0]-p[0],c[1]-p[1])));@@ -253,21 +252,23 @@}// 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=[];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=''});}// One play: a flash where it is hit, the cracks, then the card breaks and flies apart while the next card fades in behind- // the pieces. Under reduced motion the next card fades in over it instead. While Loop is on the next card holds for wait- // ms, then breaks in turn. Slow motion stretches the play, not the hold. Replay or a setting change starts it over.- function play(wait=0){- reset();held=performance.now()+wait;- const old=cards[cur],next=(cur+1)%cards.length,card=cards[next];+ // the pieces. Under reduced motion the next card fades in over it instead. While Loop is on the next card holds, then+ // breaks in turn. Slow motion stretches the play, not the hold. Replay or a setting change starts it over.+ function play(){+ reset();+ const old=cards[cur],next=playing=(cur+1)%cards.length,card=cards[next];deck.insertBefore(card,flashEl);card.classList.add('on'); // the next card goes on top of the othersif(reduce.matches)card.animate([{opacity:0},{opacity:1}],{duration:500});else{- const T=slow(ms),crack=cracksTog.checked?slow(ms*.32):0,plan=cut(old.offsetWidth,old.offsetHeight);- flashEl.style.left=old.offsetLeft+plan.p[0]+'px';flashEl.style.top=old.offsetTop+plan.p[1]+'px';+ const r=old.getBoundingClientRect(),o=deck.getBoundingClientRect(),T=slow(ms),crack=cracksTog.checked?slow(ms*.32):0;+ const plan=cut([r.left-o.left,r.top-o.top,r.width,r.height]); // the card's box in the deck, fractions kept, so the pieces line up exactly+ flashEl.style.left=plan.box[0]+plan.p[0]+'px';flashEl.style.top=plan.box[1]+plan.p[1]+'px';flashEl.animate([{opacity:0,transform:'scale(.25)'},{opacity:.85,offset:.18},{opacity:0,transform:'scale(1.5)'}],{duration:Math.max(crack,slow(260)),easing:'ease-out'});if(crack){crackLines(old,plan,crack);old.animate([{transform:'none'},{transform:'scale(.97)',offset:.2},{transform:'none'}],{duration:crack*.7,easing:'ease-out'})}// The pieces are built before anything moves and wait under the card; it goes at the break by an animation, on their clock@@ -275,15 +276,15 @@old.animate([{opacity:0},{opacity:0}],{duration:0,delay:crack,fill:'forwards'});card.animate([{opacity:0,transform:'scale(.9)'},{opacity:1,transform:'none'}],{duration:T*.5,delay:crack+T*.45,easing:'cubic-bezier(.2,.7,.3,1)',fill:'backwards'});}- // A Loop hold is spent waiting by every animation of the next break, so it is built while nothing moves- if(wait)deck.getAnimations({subtree:true}).forEach(a=>a.effect.updateTiming({delay:a.effect.getTiming().delay+wait}));// 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){cur=i;reset();if(loopTog.checked&&!reduce.matches)play(1000)}+ 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+ new ResizeObserver(()=>{const now=deck.offsetWidth+'x'+deck.offsetHeight;if(size&&now!==size&&playing>=0)settle(playing);size=now}).observe(deck);- document.getElementById('btn-play').addEventListener('click',()=>play());- loopTog.addEventListener('change',()=>{if(loopTog.checked)play();else if(performance.now()<held)reset()}); // off in a hold: no next break+ 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',()=>{