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

Tunnel Flight · How it works & code

Visual projection r = f · R / z's fake 3D tunnel.

Runtime logic

Visiting `/canvas/tunnel`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'tunnel' })`. mount resolves the variant to family `tunnel`; `withControls` builds the control panel (3 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; 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.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; ctx.closePath() — Closes the current sub-path; ctx.stroke() — Strokes the current path

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.tunnel (L1034-L1067)

Code walkthrough

L1035-1035: Control definitions (colour/speed/size/count/extras): Tunnel Color, Tube Radius R, Ring Count, 1 round, 2 sides, 3-6 sides, Pointer Rotation, Rainbow Ring. L1036-1036: Preset buttons: name → parameter overrides (3). L1037-1045: Info-panel content: live rows(v), formula, notes, params, steps. L1046-1066: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `lineTo`, `moveTo`, `closePath`, `stroke`; math via `Math.round`, `Math.min`, `Math.floor`, `Math.cos`, `Math.sin`, `Math.max`; returns readouts `v`, `r`, `n`, `shape`, `f`, `R`, `z` … to the info panel.

Principles

amend: Screen Radius r = f · R / z Forward: z ← z − v·Δt, z Turn: Circle deviation Δ = s·(pointer − Center)·(1 − z_min/z) 隧道由一串相同大小的环组成,只是离我们的距离 z Different. Based on Visual Projection , The distance looks smaller: the screen radius = Focal length × Real Radius / Distance. Let all the rings z 不断减小,就有了向前飞行的感觉;穿过镜头的环被移到最远处循环使用。 The far-off centre tends more in the direction of the pointer, forming the effect of a tunnel turning and simulating fog with brightness.

Open the interactive demo · GitHub