Orbiting Cubes · How it works & code
Angle of the i-th cube θᵢ = 2πi/n
Runtime logic
Visiting `/threejs/polycubes`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'polycubes' })`. mount resolves the variant to family `polycubes`; `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.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; THREE.Group — Group container; THREE.BoxGeometry — Box geometry; THREE.Mesh — Mesh (geometry + material); THREE.MeshStandardMaterial — PBR standard material
Source code
frontend/src/demos/kits/kit-three.js: FAM.polycubes (L110-L134)
Code walkthrough
L111-111: Control definitions (colour/speed/size/count/extras): Color, Cube side length, Number of cubes n. L112-112: Preset buttons: name → parameter overrides (6). L113-113: 3D framing radius. L114-122: Info-panel content: live rows(v), formula, notes, params, steps. L123-133: Init function: sets up state and returns the per-frame callback: creates three.js objects `Group`, `BoxGeometry`, `Mesh`, `MeshStandardMaterial`; math via `Math.max`, `Math.min`, `Math.round`, `Math.sin`, `Math.cos`; returns readouts `th`, `n`, `a`, `y1` to the info panel.
Principles
θᵢ = 2π·i / n xᵢ = R·cos(θᵢ + ωt),zᵢ = R·sin(θᵢ + ωt) yᵢ = 0.3·sin(2t + θᵢ) Split a circle equally n Here, put a square in each Round average :第 i Angle of diamonds θᵢ = 2πi/n。Again cos、sin Change angle to x、z Coordinates. The height of each square rises by the sine and the phase is staggered θᵢ, So a circle of squares“The crowd”Just like that.