Skip to content
IndexRequest a UIAboutContact

Spring Follow

A ball that moves to wherever you click using real spring physics.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
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

HTML
<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>

Related patterns

Easing Comparison

↗
Easing ComparisonMotion & Transitions

Magnetic Button

↗
Magnetic ButtonButtons & Actions

Tilt Card

↗
Tilt CardCards & Containers