Moon Orbits · How it works & code
The square of the period is proportional to the cube of the orbital radius (Kepler's Third Law), so T²/r³ is the same for each moon.
Runtime logic
Visiting `/threejs/orbit-moons`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'orbit-moons' })`. mount resolves the variant to family `orbit-moons`; `withControls` builds the control panel (6 presets + sliders). `createThreeStage` sets up WebGLRenderer, Scene, PerspectiveCamera and OrbitControls, plus 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; WebGL (three.js): WebGL through three.js WebGLRenderer; 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, three 0.170.0
Dependencies
three 0.170.0, three/addons/controls/OrbitControls.js 0.170.0, 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; createThreeStage — Shared three.js stage: WebGLRenderer, scene, perspective camera, OrbitControls, auto framing and render loop; addLights — Adds ambient + key + fill lights; THREE — three.js namespace; fmt — Number formatting (fixed decimals); deg — Radians → degrees text; F — Formula block HTML; S.setFitRadius — Sets the 3D framing radius; S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; THREE.MeshStandardMaterial — PBR standard material; THREE.Mesh — Mesh (geometry + material); THREE.SphereGeometry — Sphere geometry; THREE.Color — Colour object; THREE.Group — Group container; THREE.LineLoop — Closed polyline; THREE.BufferGeometry — Custom vertex geometry; THREE.Vector3 — 3D vector; THREE.LineBasicMaterial — Basic line material
Source code
frontend/src/demos/kits/kit-three.js: FAM.orbit-moons (L368-L403)
Code walkthrough
L369-369: Control definitions (colour/speed/size/count/extras): Planet color, Orbital Speed, Planet radius, Number of moons, Innermost orbit r₁. L370-370: Preset buttons: name → parameter overrides (6). L371-371: 3D framing radius. L371-371: Initial camera direction. L372-380: Info-panel content: live rows(v), formula, notes, params, steps. L381-402: Init function: sets up state and returns the per-frame callback: creates three.js objects `MeshStandardMaterial`, `Mesh`, `SphereGeometry`, `Color`, `Group`, `LineLoop` …; math via `Math.sin`, `Math.cos`, `Math.max`, `Math.min`, `Math.round`; returns readouts `n`, `r1`, `rn`, `T1`, `Tn`, `k`, `a1` … to the info panel.
Principles
rᵢ = r₁ · (1 + 0.35·i) ωᵢ = ω₀ · rᵢ^(−3/2) ⇒ Tᵢ² / rᵢ³ = (2π/ω₀)² Many satellites orbit around the same planet, the slower the distance, and the slower: Cycle squared relative to the cube of the orbital radius (The third law of Kepler T²/r³ For every satellite. The orbit of each satellite is slightly tilted and the orbital line is drawn from a light colour circle, which allows for a comparison of the differential speed of internal and external circles.