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

Torus Ring · How it works & code

A torus (doughnut) can be viewed as a small circle of radius r rotating around an axis at distance R, sweeping out a surface.

Runtime logic

Visiting `/threejs/torus`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'torus' })`. mount resolves the variant to family `torus`; `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; triCount — Counts triangles in a geometry; 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.TorusGeometry — Torus geometry; THREE.Mesh — Mesh (geometry + material); THREE.MeshStandardMaterial — PBR standard material

Source code

frontend/src/demos/kits/kit-three.js: FAM.torus (L56-L80)

Code walkthrough

L57-57: Control definitions (colour/speed/size/count/extras): Color, Zoom, Tube Radius r, Roughness. L58-58: Preset buttons: name → parameter overrides (6). L59-59: 3D framing radius. L60-68: Info-panel content: live rows(v), formula, notes, params, steps. L69-79: Init function: sets up state and returns the per-frame callback: creates three.js objects `TorusGeometry`, `Mesh`, `MeshStandardMaterial`; math via `Math.sin`; returns readouts `th`, `Rr`, `r`, `A`, `V`, `tris` to the info panel.

Principles

x = (R + r·cos v)·cos u,y = (R + r·cos v)·sin u,z = r·sin v S = 4π²Rr,V = 2π²Rr² The circle can be seen as a radius r Little circle around distance R The axle turns around and sweeps out the curve. Parameters u Control the position around the big circle v Controls the position around the small circle. Its surface area and volume formula are very neat: equal to“Small circle/Area × Big circle” ("Paps Theorem." Metalness 和 Roughness Yes PBR 材质的两个关键参数:粗糙度低时高光又小又亮。

Open the interactive demo · GitHub