StealThis .dev
Patterns Medium

Spring Physics

Interactive spring physics simulation where you drag an element and it springs back with configurable stiffness and damping. Uses Hooke's law (F = -kx - cv) with requestAnimationFrame.

Open in Lab
css javascript vue svelte
Targets: TS JS HTML React Vue Svelte

Code

Spring Physics

A hands-on spring physics demo. Drag the element and release it — it bounces back to its origin following real spring dynamics.

How it works

  1. Hooke’s LawF = -kx - cv where k is stiffness, c is damping, x is displacement, v is velocity
  2. On each requestAnimationFrame tick the force is calculated, velocity updated, and position integrated
  3. Mouse/touch drag overrides the position; on release the spring simulation takes over
  4. Configurable sliders let you tune stiffness (how snappy) and damping (how quickly oscillations die)

Use cases

  • Pull-to-refresh interactions
  • Draggable cards that snap back
  • Bouncy UI transitions
  • Physics-based scroll indicators