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

Noise Field · How it works & code

Fractal Brownian Motion (FBM) Noise Field

Runtime logic

Visiting `/canvas/noise`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'noise' })`. mount resolves the variant to family `noise`; `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; makeLayer — Offscreen layer (trails / pixel fields); makeNoise — Smooth value noise + fBm octaves; hexRgb — Colour string → [r, g, b]; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.drawImage() — Draws an image or offscreen canvas; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.noise (L451-L490)

Code walkthrough

L452-452: Control definitions (colour/speed/size/count/extras): Color, Scale (Feature Size), Octaves Count, Domain Distortion Strength, Mode 1 smooth 2 marbles 3 flow. L453-453: Preset buttons: name → parameter overrides (8). L454-462: Info-panel content: live rows(v), formula, notes, params, steps. L463-489: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `drawImage`, `beginPath`, `arc`, `moveTo`, `lineTo`, `stroke`; math via `Math.min`, `Math.max`, `Math.round`, `Math.sin`, `Math.abs`; returns readouts `oct`, `f`, `amp`, `mode`, `warp`, `vp` to the info panel.

Principles

fbm(p) = Σᵢ 0.5ⁱ · noise(2ⁱ · f · p) / Σ 0.5ⁱ Domain distortion: p '=p + w · (fbm(p+a), fbm(p+b)) Marble = 1⁄2 + 1⁄2 sin (10) · fbm) |2 · fbm − 1| Value Noise Places a random number at the integer point, with a smooth curve inserted in the middle to get a soft up and down. Put the same noise on the frequency ×2、Amplitude ×0.5 叠加多层(倍频),大轮廓上就有了小细节,这就是 FBM , It's often used to make clouds, topography, flames. Domain Warping The coordinates are pushed and sampled with noise, and the shape becomes a flow vortex. The third dimension takes time, so the pattern evolves slowly.

Open the interactive demo · GitHub