Soft Body · How it works & code
Jello-like soft body with spring masses and internal gas pressure.
Runtime logic
Visiting `/physics/softbody`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'softbody' })`. mount resolves the variant to family `softbody`; `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; rng — Seeded deterministic PRNG (same seed, same result); 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.quadraticCurveTo() — Adds a quadratic Bézier segment; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle)
Source code
frontend/src/demos/kits/kit-physics.js: FAM.softbody (L691-L743)
Code walkthrough
L693-693: Control definitions (colour/speed/size/count/extras): Soft Color, Soft Body Radius, Soft Body Count, Spring Stiffness k, Internal Pressure, gravity g. L694-694: Preset buttons: name → parameter overrides (6). L695-703: Info-panel content: live rows(v), formula, notes, params, steps. L704-742: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `beginPath`, `moveTo`, `quadraticCurveTo`, `closePath`, `fill`, `stroke` …; math via `Math.cos`, `Math.sin`, `Math.abs`, `Math.round`, `Math.min`, `Math.max` …; returns readouts `pts`, `r`, `A0`, `h`, `n`, `ar`, `p` … to the info panel.
Principles
Outer Circle 24 Prototypes, adjacent to each other, linked with springs: F = k · (|d| − L₀) Desired gas: p ∝nRT / A (the smaller the pressure), here p = p0 · (A₀ / A − 0.7) Pressure follows the extralegal line of each side: F = p · Side length · n̂ The molluscs (like Jell-O, balloons) consist of a circle of protons and springs. Only when the spring collapses, so add it inside Gas pressure :面积被压小时压强变大,把边界撑回去,就像气球。 Blinker k Control of borders“Hardness”,Pressure control“Drum Zoom”。软体落地会被压扁再弹回;软体之间会互相挤压。 Press the edge of the mollusk to pull it and throw it out.