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: make Elastic Transition's live spring bounce the same at every frame rate
matin9af9e414f8a1
diff --git a/animations/03-page-transitions/elastic-transition/README.md b/animations/03-page-transitions/elastic-transition/README.mdindex 38a1c581..f453221f 100644--- a/animations/03-page-transitions/elastic-transition/README.md+++ b/animations/03-page-transitions/elastic-transition/README.md@@ -10,23 +10,30 @@ An elastic transition slides the new page in so that it goes a little past its p- Cases where a gesture may be interrupted — a live spring reacts to interruption, a keyframe cannot## How it works-This demo offers two ways to bounce. The planned path writes a `@keyframes` rule at runtime whose intermediate percentages push past the endpoint before returning, with the overshoot amount set by Bounce size. The live spring integrates an actual spring each frame: force equals `-stiffness × displacement − damping × velocity`, and the loop runs until both position and velocity fall below a threshold.+This demo offers two ways to bounce. The planned path writes a `@keyframes` rule at runtime whose intermediate percentages push past the endpoint before returning, with the overshoot amount set by Bounce size. The live spring integrates an actual spring in fixed steps of 1/120 of a second: force equals `-stiffness × displacement − damping × velocity`, each frame takes as many steps as the time that has passed needs, and the loop runs until both position and velocity fall below a threshold.```js-function doSpring(o, n, stiffness, damping) { // o: the old page, n: the new page+const STEP = 1 / 120; // the spring moves in steps of 1/120 of a second+function doSpring(o, n, stiffness, damping) { // o: the old page, n: the new pagelet pos = 100, vel = 0, target = 0; // start off-screen right- let lastTime = null;+ let before = pos, acc = 0, lastTime = null; // acc: time the steps have yet to coverfunction tick(now) {- if (!lastTime) lastTime = now;- const dt = Math.min((now - lastTime) / 1000, 0.05);+ if (lastTime !== null) acc += Math.min((now - lastTime) / 1000, 0.05);lastTime = now;- const f = -stiffness * (pos - target) - damping * vel; // Hooke's law + damping- vel += f * dt;- pos += vel * dt;- n.style.transform = `translateX(${pos.toFixed(2)}%)`;- if (Math.abs(pos - target) < 0.5 && Math.abs(vel) < 0.5) {+ let settled = false;+ for (let i = 0; acc > 0 && i < 8 && !settled; i++) { // at most 8 steps in a frame+ const f = -stiffness * (pos - target) - damping * vel; // Hooke's law + damping+ before = pos;+ vel += f * STEP;+ pos += vel * STEP;+ acc -= STEP;+ settled = Math.abs(pos - target) < 0.5 && Math.abs(vel) < 0.5;+ }+ if (settled) {n.style.transform = ''; // settled — clean up} else {+ const shown = pos + (before - pos) * (-acc / STEP); // the place at this frame's own time+ n.style.transform = `translateX(${shown.toFixed(2)}%)`;requestAnimationFrame(tick);}}@@ -34,19 +41,19 @@ function doSpring(o, n, stiffness, damping) { // o: the old page, n: the new p}```-The `dt` is clamped to 50ms so a dropped frame or a backgrounded tab cannot inject a huge time step that would make the spring explode. On the demo page, Speed and Bounce size set both ways of bouncing: the planned path's length (1400, 900 or 550ms) and overshoot (30, 60 or 100), or the live spring's stiffness (70, 180 or 500) and damping. The damping values were found by running this loop at 60 frames a second, so the spring goes about as far past its place as the planned path does (about 3.6%, 7.2% and 12% of the width). Moving the spring one step per frame calms it more than the usual spring formula expects, so the values were measured rather than worked out.+A frame adds the time since the last one to `acc`, never more than 50ms, so a dropped frame or a backgrounded tab cannot inject a huge time step that would make the spring explode, and then spends it in whole steps. The first frame adds nothing, because there is no earlier frame to measure from. The last step can end a little after the frame, so the page is drawn between the last two steps, at the frame's own time; a frame that fits no whole step still moves the page. On the demo page, Speed and Bounce size set both ways of bouncing: the planned path's length (1400, 900 or 550ms) and overshoot (30, 60 or 100), or the live spring's stiffness (75, 200 or 640) and damping. These values were found by running this loop, so the spring goes about as far past its place as the planned path does (about 3.6%, 7.2% and 12% of the width). Stepping the spring in whole steps calms it more than the usual spring formula expects, so the values were measured rather than worked out.## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| How it bounces | Planned path | A planned path plays a bounce decided in advance, which is cheap; a live spring works out the movement on every frame, which feels physical and can react to interruptions |+| How it bounces | Planned path | A planned path plays a bounce decided in advance, which is cheap; a live spring works out the movement in small fixed time steps, which feels physical and can react to interruptions || Bounce size | Medium | How far the page goes past its place before settling; with a live spring, a bigger bounce means less damping, so it swings for longer || Speed | Normal | How quickly it settles: on the planned path slow is 1400ms, normal 900ms and fast 550ms; with a live spring, a faster speed is a stiffer spring |## Production notes- **Stiffness and damping interact.** Below critical damping (`damping < 2√stiffness`) the spring oscillates; at or above it, it eases in without bounce. Tune the pair together — raising stiffness usually needs more damping to stay tasteful.-- **Clamp the time step.** Integrating with the raw frame delta is the classic spring bug: one long frame and `pos` shoots to infinity. The `Math.min(dt, 0.05)` clamp is not optional.-- **Frame rate.** The demo moves its spring one step per frame, so how far it bounces depends on the frame rate: at 30 frames a second the small bounce hardly shows, and at 120 it is a little larger. A spring that steps by time instead — fixed small steps, as many as the time that has passed needs — bounces the same at any frame rate.+- **Clamp the time a frame adds.** Integrating with the raw frame delta is the classic spring bug: one long frame and `pos` shoots to infinity. The 50ms clamp (`0.05` seconds in the snippet) is not optional, and it also keeps a frame to a handful of steps, so a long pause cannot stall the page.+- **Frame rate.** The demo moves its spring in fixed steps of 1/120 of a second, as many as the time that has passed needs, so it bounces the same at any frame rate. A spring that takes one step per frame does not: at 30 frames a second a small bounce hardly shows, and at 120 it is a little larger. Draw the page between the last two steps, or a frame that fits no whole step shows the spring standing still.- **CSS keyframe overshoot can clip.** If a page translates fully to its edge before the bounce completes, the overshoot slides content out of the visible bounds. Reserve a little slack or let the container overflow during the animation.- **Library equivalents.** Framer Motion and React Spring take `stiffness`/`damping` directly and handle interruption for you — reach for them rather than hand-rolling the integrator in production. GSAP's `elastic.out` easing approximates the CSS-keyframe feel. CSS `linear()` easing can now encode a sampled spring curve without JavaScript at all.diff --git a/animations/03-page-transitions/elastic-transition/index.html b/animations/03-page-transitions/elastic-transition/index.htmlindex 6452dddb..956a9772 100644--- a/animations/03-page-transitions/elastic-transition/index.html+++ b/animations/03-page-transitions/elastic-transition/index.html@@ -117,7 +117,7 @@<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 an elastic transition to [the pages or panels you want to move between]. The new page should slide in from the side, go a little past its place and spring back before settling, as if pulled by a rubber band, while the old page slides away. The bounce can be planned ahead as a fixed path, which is cheap, or worked out live with a simple spring, which can react if the visitor interrupts it; with a live spring, limit each time step so one slow frame cannot throw it off. If the visitor has reduced motion turned on, switch pages without movement. Match the settings listed below.</p>+ <p class="hb-prompt">Add an elastic transition to [the pages or panels you want to move between]. The new page should slide in from the side, go a little past its place and spring back before settling, as if pulled by a rubber band, while the old page slides away. The bounce can be planned ahead as a fixed path, which is cheap, or worked out live with a simple spring, which can react if the visitor interrupts it; with a live spring, advance it in small fixed time steps so it moves the same on every screen, and limit the time one frame adds so a slow frame cannot throw it off. If the visitor has reduced motion turned on, switch pages without movement. 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>@@ -158,9 +158,10 @@const kfStyle=document.getElementById('kf-style');const motionOk=!window.matchMedia('(prefers-reduced-motion:reduce)').matches;// Bounce size (Small, Medium, Large) and Speed (Slow, Normal, Fast) each pick one value for both ways of bouncing- const OVERSHOOT=[30,60,100],DURATION=[1400,900,550],STIFFNESS=[70,180,500];- // The live spring's damping for each size (row) and speed (column), found by running its loop so that it goes about as far past its place as the planned path does- const DAMPING=[[12,18,28],[10,16,25],[9,14,23]];+ const OVERSHOOT=[30,60,100],DURATION=[1400,900,550],STIFFNESS=[75,200,640];+ // The live spring's damping for each size (row) and speed (column); it and the stiffness above were found by running its loop so that it goes about as far past its place as the planned path does+ const DAMPING=[[12.8,19.7,33.6],[10.6,17.4,29.7],[9.5,15.2,27.2]];+ const STEP=1/120,STEPS_MAX=8; // the live spring moves in steps of 1/120 s, and takes at most 8 of them in one framelet current=0,animating=false,mode='css',size=1,speed=1,rafId=0,afterMove=null,wayBack=0,timers=[];const pages=[],navBtns=[];@@ -253,22 +254,25 @@later(()=>{rest(o,false);rest(n,true);finish()},d+50);}- // Live spring: the old page slides away simply, the new one is pulled in from the right by a spring worked out on every frame+ // Live spring: the old page slides away simply, the new one is pulled in from the right by a spring worked out in fixed steps: each frame adds up the time+ // that has passed and takes the steps it covers, so the spring moves the same on every screen. The last step can end a little after the frame, so the page is+ // drawn between the last two steps, at the frame's own time.function doSpring(o,n,k,stiffness,damping){const out=300*k;o.style.transition=`transform ${out}ms ease-in, opacity ${out}ms ease-in`;o.style.transform='translateX(-40%)';o.style.opacity='0';- let pos=100,vel=0,target=0,lastTime=null;+ let pos=100,before=pos,vel=0,target=0,acc=0,lastTime=null;n.style.transform=`translateX(${pos}%)`;n.classList.add('active');function tick(now){- if(!lastTime)lastTime=now;- const dt=Math.min((now-lastTime)/1000,0.05)/k; // the 50ms limit first, then a third of the step in slow motion+ if(lastTime!==null)acc+=Math.min(now-lastTime,50)/1000/k; // the 50ms limit first, then a third of the time in slow motion; the first frame adds nonelastTime=now;- const f=-stiffness*(pos-target)-damping*vel;- vel+=f*dt;pos+=vel*dt;- n.style.transform=`translateX(${pos.toFixed(2)}%)`;- if(Math.abs(pos-target)<0.5&&Math.abs(vel)<0.5){rafId=0;rest(o,false);rest(n,true);finish()}- else rafId=requestAnimationFrame(tick);+ for(let i=0;acc>0&&i<STEPS_MAX;i++){+ before=pos;vel+=(-stiffness*(pos-target)-damping*vel)*STEP;pos+=vel*STEP;acc-=STEP;+ if(Math.abs(pos-target)<0.5&&Math.abs(vel)<0.5){rafId=0;rest(o,false);rest(n,true);finish();return}+ }+ const shown=pos+(before-pos)*(-acc/STEP);+ n.style.transform=`translateX(${shown.toFixed(2)}%)`;+ rafId=requestAnimationFrame(tick);}later(()=>{rafId=requestAnimationFrame(tick)},100*k);}