asafefe.dev
Lab

Lab/21

Rope Physics

Each point remembers where it was a step ago, so velocity comes for free. Links are relaxed back to length a few times per step, and fewer passes make the rope stretchier.

  • Canvas
  • Physics
  • Interaction
Grab a rope and swing it
1.00g
80%

Installation

Add Rope Physics to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-rope-physics

Or by URL: https://asafefe.dev/r/lab-rope-physics.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx9 lines
import { RopePhysics } from "@/components/lab/rope-physics"

export function Example() {
  return (
    <div className="h-[420px] touch-none">
      <RopePhysics ropes={4} gravity={1.2} />
    </div>
  )
}

Props

PropTypeDefaultDescription
anchor"pinned" | "pointer""pinned"Pins along the top, or one rope hanging from the pointer.
ropesnumber5Pinned ropes. Fewer fit on narrow screens.
segmentsnumber22Links per rope.
gravitynumber1Gravity multiplier, 0 to 2.
stiffnessnumber0.80 is stretchy, 1 is a stiff chain.
dampingnumber0.99Velocity kept per step.
collidebooleantrueThe pointer pushes ropes aside.
color / accentColorstring"var(--foreground)", "var(--primary)"Rope color, and the weights and grab ring.
lineWidthnumber1.5Rope thickness in px.
refRef<RopePhysicsHandle>-reset() rebuilds the ropes.