Menu

I built a swinging lanyard badge in React with no physics library. Here is the whole method
📰

I built a swinging lanyard badge in React with no physics library. Here is the whole method

DEV Community: react·Blog Relay·9 days ago
#i4nro50g
#devtoreact#devto#dev#card#const
Long article (883 words) — no AI summary yet.
Reading 0:00
15s threshold

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 }

Enter fullscreen mode

Exit fullscreen mode

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.