Spring Follow
A ball that moves to wherever you click using real spring physics.
AI prompt
Create a 300x160px box with a 36px circular ball. Clicking or tapping anywhere in the box sets a target position (clamped inside the box) and the ball springs to it. Implement the spring in requestAnimationFrame for x and y independently with stiffness 170, damping 14 and mass 1: force = -k*(pos-target) - c*velocity, update velocity and position with a time step capped at 1/30 second, and stop the loop when the distance and the speed are below 0.1. A new click while moving changes the target but keeps the current velocity. Add Left, Centre and Right buttons below that set the target to those positions. Show the stiffness and damping values as small text. The ball has aria-hidden because it is decorative; the buttons are the accessible controls. Under prefers-reduced-motion move instantly to the target.About this pattern
Definition
A spring simulation moves an element toward a target. It accelerates, overshoots slightly and settles, because speed is computed from stiffness and damping instead of a fixed curve.
Purpose & UX principles
- Springs respond naturally to interruption: clicking again mid-motion redirects the ball smoothly without restarting.
- Two numbers control the feel: stiffness (how fast it pulls) and damping (how quickly wobble dies).
- Suits draggable and interruptible elements better than fixed-duration curves.
Interaction states
- At rest, moving, retargeted mid-flight, focus-visible on buttons.
Motion specification
- Stiffness 170, damping 14, mass 1, integrated each frame with a capped time step; the motion stops when speed and distance are both tiny.
- Reduced motion: the ball jumps straight to the target.
Responsive behaviour
- The ball moves inside a responsive box; the target is clamped to the box.
- Buttons for Left, Centre and Right provide non-pointer input.
Code
<div class="box" id="box"><div class="ball" id="ball" aria-hidden="true"></div></div>
<div class="bt"><button data-p="0">Left</button><button data-p=".5">Centre</button><button data-p="1">Right</button></div>
<p class="meta">stiffness 170 · damping 14</p>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.box{position:relative;width:min(300px,100%);height:160px;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden;cursor:pointer;touch-action:manipulation}
.ball{position:absolute;left:0;top:0;width:36px;height:36px;border-radius:50%;background:var(--accent);will-change:transform}
.bt{display:flex;gap:6px;justify-content:center;margin-top:10px}
.bt button{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.bt button:hover{border-color:var(--accent)}
.bt button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.meta{text-align:center;font-size:11px;color:var(--muted);margin-top:8px}const box=document.getElementById('box'),ball=document.getElementById('ball');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const K=170,C=14;let x=132,y=62,vx=0,vy=0,tx=132,ty=62,run=false,last=0;
const draw=()=>{ball.style.transform='translate('+x+'px,'+y+'px)';};draw();
function step(t){const dt=Math.min(1/30,(t-last)/1000||0.016);last=t;
vx+=(-K*(x-tx)-C*vx)*dt;vy+=(-K*(y-ty)-C*vy)*dt;x+=vx*dt;y+=vy*dt;draw();
if(Math.hypot(x-tx,y-ty)<.1&&Math.hypot(vx,vy)<.1){x=tx;y=ty;draw();run=false;return;}requestAnimationFrame(step);}
function go(px,py){const w=box.clientWidth-36,h=box.clientHeight-36;tx=Math.max(0,Math.min(w,px));ty=Math.max(0,Math.min(h,py));
if(reduce){x=tx;y=ty;vx=vy=0;draw();return;}if(!run){run=true;last=performance.now();requestAnimationFrame(step);}}
box.addEventListener('pointerdown',e=>{const r=box.getBoundingClientRect();go(e.clientX-r.left-18,e.clientY-r.top-18);});
document.querySelectorAll('.bt button').forEach(b=>b.addEventListener('click',()=>go((box.clientWidth-36)*(+b.dataset.p),(box.clientHeight-36)/2)));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Spring Follow — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.box{position:relative;width:min(300px,100%);height:160px;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden;cursor:pointer;touch-action:manipulation}
.ball{position:absolute;left:0;top:0;width:36px;height:36px;border-radius:50%;background:var(--accent);will-change:transform}
.bt{display:flex;gap:6px;justify-content:center;margin-top:10px}
.bt button{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12.5px;cursor:pointer}
.bt button:hover{border-color:var(--accent)}
.bt button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.meta{text-align:center;font-size:11px;color:var(--muted);margin-top:8px}
</style></head>
<body><div class="stage">
<div class="box" id="box"><div class="ball" id="ball" aria-hidden="true"></div></div>
<div class="bt"><button data-p="0">Left</button><button data-p=".5">Centre</button><button data-p="1">Right</button></div>
<p class="meta">stiffness 170 · damping 14</p>
</div>
<script>
const box=document.getElementById('box'),ball=document.getElementById('ball');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const K=170,C=14;let x=132,y=62,vx=0,vy=0,tx=132,ty=62,run=false,last=0;
const draw=()=>{ball.style.transform='translate('+x+'px,'+y+'px)';};draw();
function step(t){const dt=Math.min(1/30,(t-last)/1000||0.016);last=t;
vx+=(-K*(x-tx)-C*vx)*dt;vy+=(-K*(y-ty)-C*vy)*dt;x+=vx*dt;y+=vy*dt;draw();
if(Math.hypot(x-tx,y-ty)<.1&&Math.hypot(vx,vy)<.1){x=tx;y=ty;draw();run=false;return;}requestAnimationFrame(step);}
function go(px,py){const w=box.clientWidth-36,h=box.clientHeight-36;tx=Math.max(0,Math.min(w,px));ty=Math.max(0,Math.min(h,py));
if(reduce){x=tx;y=ty;vx=vy=0;draw();return;}if(!run){run=true;last=performance.now();requestAnimationFrame(step);}}
box.addEventListener('pointerdown',e=>{const r=box.getBoundingClientRect();go(e.clientX-r.left-18,e.clientY-r.top-18);});
document.querySelectorAll('.bt button').forEach(b=>b.addEventListener('click',()=>go((box.clientWidth-36)*(+b.dataset.p),(box.clientHeight-36)/2)));
</script>
</body></html>