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

Kaleidoscope · How it works & code

Kaleidoscope with rotation and reflection symmetry.

Runtime logic

Visiting `/canvas/kaleido`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'kaleido' })`. mount resolves the variant to family `kaleido`; `withControls` builds the control panel (4 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.save() — Saves the drawing state (transform, styles, clip); ctx.translate() — Translates the coordinate system; ctx.rotate() — Rotates the coordinate system; ctx.scale() — Scales the coordinate system; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.restore() — Restores the saved drawing state

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.kaleido (L846-L879)

Code walkthrough

L848-848: Control definitions (colour/speed/size/count/extras): pen color, Line width, sector count n, reflections within sector, rainbow trail, pen duration s. L849-849: Preset buttons: name → parameter overrides (4). L850-858: Info-panel content: live rows(v), formula, notes, params, steps. L859-878: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `save`, `translate`, `rotate`, `scale`, `beginPath`, `moveTo` …; math via `Math.round`, `Math.hypot`, `Math.cos`, `Math.sin`, `Math.atan2`; returns readouts `n`, `ang`, `ord`, `mir`, `pts`, `r`, `th` to the info panel.

Principles

Rotate copy: θk = θk · 2π / n,k = 0…n−1 Mirror: → θ (rerotation) pts (r, θ) → (r·cos θₖ, r·sin θₖ) The mirrors in the Kalescope replicate a small piece of footage in a circle. This is where we turn the pointer to the center Polar Coordinates (r, θ), And every second 360°/n Copy that. I'll flip each one back and forth when I open the mirror n Change into 2n, It's called a dodecahedron Dₙ; It's called a circle Cₙ. The writing fades over time, moving the pointer and drawings the pattern that keeps changing.

Open the interactive demo · GitHub