加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Multiple Gravity · How it works & code

500 particles in multiple softened square gravitational wells.

Runtime logic

Visiting `/physics/multigrav`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'multigrav' })`. mount resolves the variant to family `multigrav`; `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. 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; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; 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.createRadialGradient() — Creates a radial gradient; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle

Source code

frontend/src/demos/kits/kit-physics.js: FAM.multigrav (L263-L295)

Code walkthrough

L265-265: Control definitions (colour/speed/size/count/extras): Particle Color, Particle Size, number of gravity wells, gravity strength G (negative = repulsion), softening length ε, global gravity x. L266-266: Preset buttons: name → parameter overrides (6). L267-275: Info-panel content: live rows(v), formula, notes, params, steps. L276-294: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `createRadialGradient`, `beginPath`, `arc`, `fill`, `moveTo`, `lineTo` …; math via `Math.round`, `Math.cos`, `Math.sin`, `Math.sqrt`, `Math.hypot`; returns readouts `k`, `n`, `vm`, `ap`, `rs`, `G`, `eps` … to the info panel.

Principles

a = Σᵢ G · Mᵢ · (cᵢ − p) / (|cᵢ − p|² + ε²)^(3 / 2) + g global ε Soft: Avoid distance→0 The speed of time is infinite Semi-hidden Eurasia:v ←v + a · Δt,p ← p + v · Δt Every gravitational well looks like an invisible planet. Press Inverse square Attract the surrounding particles: double the distance, gravity becomes 1/4。几个井同时作用,粒子会画出复杂的轨迹,甚至在井之间被“Throw it around”。 The real formula is close 0 It's gonna be huge. It's gonna fly a bullet in numbers softening length ε 。 The pointer is also a gravity well; G 调成负数就变成斥力。逃出画面太远或撞进井心的粒子会在边缘重生。

Open the interactive demo · GitHub