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

Waves · How it works & code

Water surface from multiple sinusoidal waves

Runtime logic

Visiting `/canvas/wave`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'wave' })`. mount resolves the variant to family `wave`; `withControls` builds the control panel (8 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; 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.createLinearGradient() — Creates a linear gradient; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle)

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.wave (L407-L447)

Code walkthrough

L409-409: Control definitions (colour/speed/size/count/extras): Water surface color, Amplitude A, Wave layers, Wave Number k, Pointer press. L410-410: Preset buttons: name → parameter overrides (8). L411-419: Info-panel content: live rows(v), formula, notes, params, steps. L420-446: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `createLinearGradient`, `fillRect`, `beginPath`, `moveTo`, `lineTo`, `closePath` …; math via `Math.round`, `Math.min`, `Math.max`, `Math.sin`, `Math.exp`, `Math.abs`; returns readouts `n`, `A`, `k`, `lam`, `w`, `v`, `yp` to the info panel.

Principles

yᵢ(x,t) = y₀ + A·sin(kᵢx − ωᵢt + φᵢ) + 0.35A·sin(1.7kᵢx + 0.6ωᵢt) λ = 2π / k,v = ω / k Pressure: contendsy = p · ( Financing pointer y-y) · Content of Both / 80)²) Each layer of water is superseded by two chord waves: one main wave to the right, one short wave to the left, and folding together feels irregular. The smaller the layer, the darker the color, the slower it's coming Parallax Depth sense. Wavelength λ The distance between two waves, Wave speed = ω/k。The pointer will Establishment the front floor“按”Pointer position, use the Goss function to make impact only nearby.

Open the interactive demo · GitHub