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

Helix Tube · How it works & code

Parameter u goes from 0 to 1, x and z rotate k times around a circle, while y rises uniformly by h.

Runtime logic

Visiting `/threejs/helix`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'helix' })`. mount resolves the variant to family `helix`; `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.Curve — Parametric curve base class; THREE.Vector3 — 3D vector; THREE.TubeGeometry — Tube along a curve; THREE.Mesh — Mesh (geometry + material); THREE.MeshStandardMaterial — PBR standard material

Source code

frontend/src/demos/kits/kit-three.js: FAM.helix (L135-L156)

Code walkthrough

L136-136: Control definitions (colour/speed/size/count/extras): Color, Zoom, Number of Turns, Tube Thickness. L137-137: Preset buttons: name → parameter overrides (6). L138-138: 3D framing radius. L139-147: Info-panel content: live rows(v), formula, notes, params, steps. L148-155: Init function: sets up state and returns the per-frame callback: creates three.js objects `Curve`, `Vector3`, `TubeGeometry`, `Mesh`, `MeshStandardMaterial`; math via `Math.cos`, `Math.sin`, `Math.round`, `Math.hypot`; returns readouts `th`, `k`, `pitch`, `L` to the info panel.

Principles

x = R·cos(2πk·u),y = h·(u − ½),z = R·sin(2πk·u),u∈[0,1] L = k · √((2πR)² + p²) The spiral line is one Parameter Curve :Params u 从 0 Come on 1,x、z Round k Circle, at the same time y Even up h。把它展开,每一圈就像斜坡上的一段直线,所以弧长可以用勾股定理算。 TubeGeometry Follow this curve“套”The last tube becomes a visible spring/DNA Shape.

Open the interactive demo · GitHub