Orbital Physics · How it works & code
Kepler's orbit using Verlet symplectic integration.
Runtime logic
Visiting `/physics/orbit-p`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'orbit-p' })`. mount resolves the variant to family `orbit-p`; `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; 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.get — Reads current control values; S.clear — Clears with the background colour (translucent = trails); 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
Source code
frontend/src/demos/kits/kit-physics.js: FAM.orbit-p (L650-L689)
Code walkthrough
L652-652: Control definitions (colour/speed/size/count/extras): Planet color, Star Mass M, Number of Planets, Initial velocity multiplier (1 = round orbit), Orbital Radius, Trail. L653-653: Preset buttons: name → parameter overrides (6). L654-662: Info-panel content: live rows(v), formula, notes, params, steps. L663-688: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `createRadialGradient`, `beginPath`, `arc`, `fill`, `moveTo`, `lineTo` …; math via `Math.round`, `Math.min`, `Math.sqrt`, `Math.cos`, `Math.sin`, `Math.hypot` …; returns readouts `n`, `M`, `ecc`, `dist`, `tr` to the info panel.
Principles
Gravity: a = −GM·r / |r|³ Speed Verlet (cincidence): v1=v2=2a⁄t,r+r+v=v1=2Δt,v=v1=2=1=2⁄r)Δt The third law of Kepler: T² = 4π²a³ / (GM); e = √(1 + 2EL²/(GM)²) Planets are gravity only. The initial speed is exactly the same √(GM/r) Time Round orbit ; A little bit smaller Ellipse ,近日点快、远日点慢(开普勒第二定律:相同时间扫过相同面积);超过 √2 Double escape. The larger the orbit, sect T² ∝ a³ (The third law. Component Speed Verlet ,This kind of“Cyclical points”It'll keep the energy from drifting ΔE Should be close Discussion 0。按住行星拖到新位置松手,它会以当前切向速度重新入轨。