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): one crack line on the card and its pieces, the next Loop break built during the hold, bends that never fold a piece, no slivers on phones
matin109b3908b6b1
diff --git a/animations/02-entrance-and-exit/shatter-effect/README.md b/animations/02-entrance-and-exit/shatter-effect/README.mdindex 139643e0..3c9827ef 100644--- a/animations/02-entrance-and-exit/shatter-effect/README.md+++ b/animations/02-entrance-and-exit/shatter-effect/README.md@@ -12,23 +12,25 @@ A shatter effect breaks an element into sharp pieces that fly apart and fade, as- Moments that happen once per visit or per action, not things people see over and over## How it works-The card is cut like glass hit at one point. Rays run from the point of impact to the card's edge, always through the four corners (so the outer pieces end exactly on the edge) and spread evenly in between; rings cross the rays at growing fractions of the distance to the edge, and every crack from one ring to the next bends once, part way along. A piece is the area between two neighboring rays and two neighboring rings. Neighbors share their corners and bends, so the pieces tile the card with no gaps:+The card is cut like glass hit at one point. Rays run from the point of impact to the card's edge, always through the four corners (so the outer pieces end exactly on the edge) and spread evenly in between; rings cross the rays at growing fractions of the distance to the edge, and every crack from one ring to the next bends once, part way along, by at most a third of its room to the nearest ray or ring line, so no piece folds over itself. A piece is the area between two neighboring rays and two neighboring rings. Neighbors share their corners and bends, so the pieces tile the card with no gaps (each piece also notes whether one of its sides is the card's own border):```jsconst grid=[rays.map(()=>p),...f.map((k,i)=>rays.map(a=>{const r=edge(a)*(i<f.length-1?k*rnd(.88,1.12):1);return [p[0]+Math.cos(a)*r,p[1]+Math.sin(a)*r]}))];-for(let i=0;i<f.length;i++)rays.forEach((_,k)=>{const n=(k+1)%rays.length;- polys.push([grid[i][k],bends[i][k],grid[i+1][k],grid[i+1][n],bends[i][n]].concat(i?[grid[i][n]]:[]))});+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)});```-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. 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`. 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:```js-const el=document.createElement('div'),copy=card.cloneNode(true);+const el=Object.assign(document.createElement('div'),{className:'shard'}),copy=card.cloneNode(true),o=q.edge?[...q.slice(3),...q.slice(0,3)]:q;el.style.cssText='left:'+(X+bx)+'px;top:'+(Y+by)+'px;width:'+(Math.ceil(Math.max(...xs))-bx)+'px;height:'+(Math.ceil(Math.max(...ys))-by)+'px;'+'transform-origin:'+(c[0]-bx)+'px '+(c[1]-by)+'px;clip-path:polygon('+pts.map(([x,y])=>(x-bx).toFixed(1)+'px '+(y-by).toFixed(1)+'px')+')';copy.style.cssText='left:'+(-bx)+'px;top:'+(-by)+'px;width:'+W+'px;height:'+H+'px';+if(start)copy.querySelector('.cracks').innerHTML=['dk','lt'].map(cls=>'<path class="'+cls+'" d="M'+o.map(P).join('L')+(q.edge?'':'Z')+'"/>').join('');```Each flight is worked out once and sampled into 13 keyframes for the Web Animations API, so the browser plays it by elapsed time and only moves, turns and fades the pieces. Over the flight time `u` (0 to 1) the burst away from the impact slows down (`e` is an ease-out), the upward throw is part of the starting speed, and gravity adds `g·u²/2`; the piece turns all the way and fades over the second half:@@ -43,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; the copies carry the lines, so every piece keeps a light edge where it broke (on a flying piece only a soft light line, since a thin two-tone line breaks up into a sawtooth once the piece tilts). 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.+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.## Key parameters@@ -57,7 +59,7 @@ Each piece waits until the break (`start`, the time the cracks take), then a lit| Tumbles in 3D | on | The pieces flip over and fly toward the viewer; off, they spin flat |## 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 and remove them once they have faded. The demo cuts Many a little coarser on phones (a screen up to 600px wide or 500px tall).+- **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 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.diff --git a/animations/02-entrance-and-exit/shatter-effect/index.html b/animations/02-entrance-and-exit/shatter-effect/index.htmlindex 4d719971..99447323 100644--- a/animations/02-entrance-and-exit/shatter-effect/index.html+++ b/animations/02-entrance-and-exit/shatter-effect/index.html@@ -35,9 +35,9 @@.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}.cap span{display:block;margin-top:.5cqh;font-size:max(12px,3.1cqh);color:var(--card-dim)}- /* The cracks: a dark line under a light one, so they show on the picture and on the caption */+ /* The cracks: a soft dark line under a light one, seen on picture and caption; the pieces carry the same lines, so nothing changes at the break */.cracks{position:absolute;inset:0;width:100%;height:100%;border-radius:inherit;pointer-events:none}- .cracks path{fill:none;stroke-linecap:round;stroke-linejoin:round}.cracks .dk{stroke:rgba(6,8,16,.6);stroke-width:2.8}.cracks .lt{stroke:rgba(255,255,255,.9);stroke-width:1.2}+ .cracks path{fill:none;stroke-linecap:round;stroke-linejoin:round}.cracks .dk{stroke:rgba(6,8,16,.36);stroke-width:3.6}.cracks .lt{stroke:rgba(255,255,255,.85);stroke-width:1.8}.flash{position:absolute;z-index:3;width:34cqh;aspect-ratio:1;margin:-17cqh 0 0 -17cqh;border-radius:50%;opacity:0;pointer-events:none;background:radial-gradient(closest-side,#fff,rgba(255,255,255,.55) 35%,rgba(255,255,255,0))}/* A piece is a box around its polygon, holding a copy of the whole card clipped to that polygon. The pieces wait under@@ -45,8 +45,6 @@.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}- /* On a flying piece the crack is one soft light edge: a thin two-tone line breaks up into a sawtooth once the piece tilts */- .shard .cracks .dk{display:none}.shard .cracks .lt{stroke:rgba(255,255,255,.75);stroke-width:2.6}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=6"><script src="../../../assets/js/demo-page.js?v=6" defer></script>@@ -188,12 +186,12 @@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,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);+ 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);- // Cuts a W×H card like glass hit at one point: rays run from the point to the edge (through the four corners, so the outer- // pieces end exactly on the edge, and spread evenly in between), rings cross them, and each crack from one ring to the next- // bends once. A piece lies between two rays and two rings; neighbors share corners and bends, so the pieces tile the card.+ // 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];const extra=pieces==='many'&&phone.matches?8:CUTS[pieces][0],f=RINGS[CUTS[pieces][1]];@@ -202,16 +200,18 @@return [c,...[...Array(m)].map((_,j)=>c+span*(j+1+rnd(-.3,.3))/(m+1))]});const edge=a=>{const c=Math.cos(a),s=Math.sin(a);return Math.min(c>1e-9?(W-p[0])/c:c<-1e-9?-p[0]/c:1e9,s>1e-9?(H-p[1])/s:s<-1e-9?-p[1]/s:1e9)};const grid=[rays.map(()=>p),...f.map((k,i)=>rays.map(a=>{const r=edge(a)*(i<f.length-1?k*rnd(.88,1.12):1);return [p[0]+Math.cos(a)*r,p[1]+Math.sin(a)*r]}))];- const bends=grid.slice(1).map((ring,i)=>ring.map((b,k)=>{const a=grid[i][k],t=rnd(.4,.6),j=rnd(-.08,.08);- return [a[0]+(b[0]-a[0])*t-(b[1]-a[1])*j,a[1]+(b[1]-a[1])*t+(b[0]-a[0])*j]}));+ const off=(m,b,c)=>{const x=c[0]-b[0],y=c[1]-b[1],l=Math.hypot(x,y);return l?Math.abs(x*(m[1]-b[1])-y*(m[0]-b[0]))/l:1e9}; // m to line bc+ const bends=grid.slice(1).map((ring,i)=>ring.map((b,k)=>{const a=grid[i][k],t=rnd(.4,.6),m=[a[0]+(b[0]-a[0])*t,a[1]+(b[1]-a[1])*t],N=ring.length;+ const room=Math.min(...[p,b,a].flatMap((o,s)=>[k+1,k+N-1].map(n=>off(m,o,(s<2?ring:grid[i])[n%N])))); // neighbor rays, ring chords+ const j=rnd(-1,1)*Math.min(.08,room/3/(Math.hypot(b[0]-a[0],b[1]-a[1])||1));return [m[0]-(b[1]-a[1])*j,m[1]+(b[0]-a[0])*j]}));const polys=[];- for(let i=0;i<f.length;i++)rays.forEach((_,k)=>{const n=(k+1)%rays.length;- polys.push([grid[i][k],bends[i][k],grid[i+1][k],grid[i+1][n],bends[i][n]].concat(i?[grid[i][n]]:[]))});+ 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 borderreturn {p,grid,bends,polys};}// The cracks, ring by ring from the break outward (the card's own edge is not a crack)function crackLines(card,{grid,bends},crack){- const svg=card.querySelector('.cracks'),P=v=>v[0].toFixed(1)+' '+v[1].toFixed(1);+ const svg=card.querySelector('.cracks');svg.setAttribute('viewBox','0 0 '+card.offsetWidth+' '+card.offsetHeight);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':'');@@ -220,23 +220,23 @@[...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). Its flight is sampled into keyframes: a burst away from the break that slows+ // 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);- const [push,lift,grav]=PULL[gravity],tumble=tumbleTog.checked,layer=document.createElement('div');+ 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])));- layer.className='shards';polys.forEach((q,i)=>{+ if(phone.matches&&Math.abs(q.reduce((s,v,j)=>s+v[0]*q[(j+1)%q.length][1]-q[(j+1)%q.length][0]*v[1],0))<8)return; // a sliver under 4px² is not worth a layer on a phoneconst c=mid[i],pts=q.map(([x,y])=>{const l=Math.hypot(x-c[0],y-c[1])||1;return [x+(x-c[0])/l*.7,y+(y-c[1])/l*.7]});const xs=pts.map(v=>v[0]),ys=pts.map(v=>v[1]),bx=Math.floor(Math.min(...xs)),by=Math.floor(Math.min(...ys));- const el=document.createElement('div'),copy=card.cloneNode(true);- el.className='shard';+ const el=Object.assign(document.createElement('div'),{className:'shard'}),copy=card.cloneNode(true),o=q.edge?[...q.slice(3),...q.slice(0,3)]:q;el.style.cssText='left:'+(X+bx)+'px;top:'+(Y+by)+'px;width:'+(Math.ceil(Math.max(...xs))-bx)+'px;height:'+(Math.ceil(Math.max(...ys))-by)+'px;'+'transform-origin:'+(c[0]-bx)+'px '+(c[1]-by)+'px;clip-path:polygon('+pts.map(([x,y])=>(x-bx).toFixed(1)+'px '+(y-by).toFixed(1)+'px')+')';copy.style.cssText='left:'+(-bx)+'px;top:'+(-by)+'px;width:'+W+'px;height:'+H+'px';+ if(start)copy.querySelector('.cracks').innerHTML=['dk','lt'].map(cls=>'<path class="'+cls+'" d="M'+o.map(P).join('L')+(q.edge?'':'Z')+'"/>').join('');el.appendChild(copy);layer.appendChild(el);const d=Math.hypot(c[0]-p[0],c[1]-p[1]),a=Math.atan2(c[1]-p[1],c[0]-p[0])+rnd(-.35,.35);const v=H*push*rnd(.6,1.15)*(1.2-.45*d/far),vx=Math.cos(a)*v,vy=Math.sin(a)*v-H*lift*rnd(.6,1.2),g=H*grav;@@ -253,16 +253,15 @@}// 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, then- // breaks in turn. Slow motion stretches the play, not the hold. Replay or a setting change starts it over.- function play(){- reset();+ // 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];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});@@ -271,19 +270,20 @@flashEl.style.left=old.offsetLeft+plan.p[0]+'px';flashEl.style.top=old.offsetTop+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 now, before anything moves (the costly part), and wait under the card. The card goes at the- // break by an animation too, so it keeps time with the pieces however long the first frame takes.+ // The pieces are built before anything moves and wait under the card; it goes at the break by an animation, on their clockshatter(old,plan,crack);old.style.zIndex=2;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)later(()=>{if(loopTog.checked)play()},1000)}+ function settle(i){cur=i;reset();if(loopTog.checked&&!reduce.matches)play(1000)}- 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();else if(performance.now()<held)reset()}); // off in a hold: no next breakfunction choices(id,pick){const buttons=document.querySelectorAll('#'+id+' button');buttons.forEach(b=>b.addEventListener('click',()=>{