Orbiting Particles · How it works & code
Particles orbiting according to Kepler's Third Law.
Runtime logic
Visiting `/canvas/orbits`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'orbits' })`. mount resolves the variant to family `orbits`; `withControls` builds the control panel (9 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; 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.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.ellipse() — Adds an elliptical arc; 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-canvas.js: FAM.orbits (L233-L273)
Code walkthrough
L235-235: Control definitions (colour/speed/size/count/extras): Particle Color, Point size, Particle Count N, Number of orbits, Pointer perturbation. L236-236: Preset buttons: name → parameter overrides (9). L237-245: Info-panel content: live rows(v), formula, notes, params, steps. L246-272: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `ellipse`, `stroke`, `arc`, `fill`; math via `Math.round`, `Math.min`, `Math.floor`, `Math.pow`, `Math.cos`, `Math.sin` …; returns readouts `n`, `rings`, `T1`, `Tn`, `k`, `r1`, `rn` … to the info panel.
Principles
Mindful speed = 0 · (r / r₀)^(−3 / 2) Period T = 2π / ω, T² ∝ r³ (The Law of the Exchequer Disturbing: · e^(−2t)+push The particles rotate around centres in several layers. Like planets, The slower the outer layer turns : Angle speed and radius −3/2 That's the difference, so T²/r³ Each layer is the same constant. The pointer moves the particle imagined along the path and slowly returns to its original orbit (index decay) like a spring. The ellipse is to flatten the circle in the vertical direction 0.6 Multiply, simulate a glimpse from above the slope.