I wanted an ID badge that hangs from a strap, swings when you grab it and flies when you throw it. Every example I found pulled in a physics engine and a 3D renderer for it. That is a lot of code to ship for one card.
It turns out the whole thing is a rope, a weight and one spring. Here is how it works. You can try the finished badge here first: grab it and throw it.
1. The strap is 14 links
A rope is a chain of points that are not allowed to drift apart. I store each point with its current and its previous position:
type Pt = { x: number; y: number; px: number; py: number } There is no velocity field. The velocity is the difference between the two positions. This is Verlet integration, and it is why throwing works without any extra code: when you let go, the last two positions already contain the speed of your hand.
Each step moves every point by its own velocity, plus gravity:
const vx = (p.x - p.px) * 0.99 // a little air drag const vy = (p.y - p.py) * 0.99 p.px = p.x p.py = p.y p.x += vx p.y += vy + GRAVITY * dt * dt 2. Then pull the chain back together
After that step the points are too far apart. So for each neighbouring pair, measure the distance and move both points toward the right length. Repeat a few times:
const dx = b.x - a.x const dy = b.y - a.y const dist = Math.hypot(dx, dy) const pull = (dist - segment) / dist a.x += dx * pull * wa a.y += dy * pull * wa b.x -= dx * pull * wb b.y -= dy * pull * wb wa and wb decide who gives way. Normally both are 0.5. Three cases are different:
- The top point is fixed, so it never moves (
wa = 0). - The card is heavy, so on the last link the rope does most of the moving (
0.88against0.12). This one number is what makes it hang straight and swing like a pendulum instead of flapping like a ribbon. - While you hold the card, it does not give at all.
Ten passes per step are enough for 14 links.
3. The card over-swings on its own spring
If the card simply took the angle of the last link, it would look glued to the rope. A real badge lags behind and overshoots. So the card has its own angle and its own angular speed, pulled toward the rope's direction by a spring:
const want = -Math.atan2(end.x - prev.x, end.y - prev.y) spin += ((want - angle) * 90 - spin * 9) * dt angle += spin * dt The card also leans in 3D with its horizontal speed (rotateY), and a band of light moves across it by the same amount. Both are one line each and they sell the effect more than anything else.
4. Dragging, and the two bugs I hit
While dragging, the last point follows the pointer. Two things went wrong at first.
The rope stretched forever. The top is fixed and so is your hand, so the constraints cannot be satisfied. The fix is to limit the hand: past the strap's length the card only follows 12% of the extra distance. It feels like the strap going taut.
A fast flick launched the card into orbit. Verlet keeps whatever speed the last two positions imply, and a mouse can move very fast between two frames. I cap the release speed.
5. Do not run when nothing moves
A physics loop that runs forever drains batteries. This one counts frames in which the card is slow, the spin is near zero and nobody is holding it. After half a second of that it stops, and a pointer event starts it again. It also stops off screen (IntersectionObserver) and in a hidden tab.
With prefers-reduced-motion the simulation never starts: the badge hangs straight and still flips on click.
What it costs
About 500 lines in one file, styles and markup included, with an SVG path for the strap and a div for the card. The physics uses no library at all. The only import besides React is one hook from Motion that reads the reduced-motion setting.
The finished component, with a flip side and keyboard control, is part of goodui, a React component library I am building. 133 of the components are free and MIT licensed, installed as source through the shadcn CLI. The lanyard badge is in the paid set.
