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

Reaction–Diffusion · How it works & code

Gray-Scott Reaction-Diffusion Model

Runtime logic

Visiting `/canvas/reactdiff`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'reactdiff' })`. mount resolves the variant to family `reactdiff`; `withControls` builds the control panel (6 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); rng — Seeded deterministic PRNG (same seed, same result); 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.stroke() — Strokes the current path

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.reactdiff (L1141-L1189)

Code walkthrough

L1143-1143: Control definitions (colour/speed/size/count/extras): Pattern Color, Brush Radius, Feed Rate F, Consumption Rate k. L1144-1144: Preset buttons: name → parameter overrides (6). L1145-1153: Info-panel content: live rows(v), formula, notes, params, steps. L1154-1188: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `drawImage`, `beginPath`, `arc`, `stroke`; math via `Math.min`, `Math.max`, `Math.round`, `Math.floor`, `Math.ceil`; returns readouts `cols`, `rows`, `F`, `k`, `steps`, `vm`, `vp` to the info panel.

Principles

∂U/∂t = D_U∇²U − UV² + F(1 − U) ∂V/∂t = D_V∇²V + UV² − (F + k)V D_U = 1,D_V = 0.5,∇² 用 3×3 Volume core Imagine two chemicals: U Yes“Food”, V Yes“Food-eating creatures”. V Come across U I'll turn it into more V (UV² Item) at the same time V It'll be speed k Death U Rate F Added. Both are spreading, but U It spreads V Almost double. This“Slow down, slow down”The competition is spontaneous Turing : Zebra stripes, leopard spots, corals, mazes…… Change F 和 k You can switch the pattern type. The pointers will be dropped by the pass V。

Open the interactive demo · GitHub