Elastic Rope · How it works & code
Hanging rope with Verlet integration + distance constraints
Runtime logic
Visiting `/physics/rope`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'rope' })`. mount resolves the variant to family `rope`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21
Dependencies
react 18.3.1
Imported APIs
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; S.onUp — Pointer up; S.clear — Clears with the background colour (translucent = trails); S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path
Source code
frontend/src/demos/kits/kit-physics.js: FAM.rope (L558-L600)
Code walkthrough
L560-560: Control definitions (colour/speed/size/count/extras): Rope color, Rope thickness, Node count, Constraint iteration count, gravity g, Looseness. L561-561: Preset buttons: name → parameter overrides (6). L562-570: Info-panel content: live rows(v), formula, notes, params, steps. L571-599: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `arc`, `fill`; math via `Math.round`, `Math.hypot`, `Math.max`; returns readouts `n`, `len`, `L0`, `str`, `sag`, `ten`, `it` … to the info panel.
Principles
Verlet:xₙ₊₁ = xₙ + (xₙ − xₙ₋₁) · Blocked + a · Δt² Distance constraint: = (|d| − L) / |d|, fixes for each end = 1 · Δ · d The more it's coming closer, the more it can't stretch; the hanger y = a · cosh(x / a) The rope was broken into a string of nodes. Verlet Integral It's not visible. It's used“This frame position − Previous Frame Position”Consider it speed, very stable, very common in games. After each step, the adjacent node is repeatedly pulled back to the fixed space ( Distance limit )。迭代次数少,绳子像橡皮筋一样能拉长;迭代多,就几乎不可伸长。静止时绳子形状接近 Chainline 。 The end is fixed and the pointer will push the rope; hold the rope and drag the nearest node.