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

2D Metaballs · How it works & code

The hidden equivalent of a 2D melting ball.

Runtime logic

Visiting `/canvas/metaballs2d`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'metaballs2d' })`. mount resolves the variant to family `metaballs2d`; `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); hexRgb — Colour string → [r, g, b]; 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.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.metaballs2d (L1230-L1267)

Code walkthrough

L1232-1232: Control definitions (colour/speed/size/count/extras): Merging Ball Color, ball radius, Number of Balls, Threshold T, Edge Softness, Different Colors per Ball. L1233-1233: Preset buttons: name → parameter overrides (6). L1234-1242: Info-panel content: live rows(v), formula, notes, params, steps. L1243-1266: 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.cos`, `Math.sin`; returns readouts `n`, `r`, `thr`, `cov`, `fc`, `dmin` to the info panel.

Principles

场 f(p) = Σᵢ rᵢ² / |p − cᵢ|² f(p) ≥T area is the ball Single ball boundary:|p − c| = r / √T; two balls close to the time field, and the border is naturally integrated Each ball is not a drawing circle, it's an outward decay 场 :离球心越近数值越大。把所有球的场加起来,凡是总和超过阈值 T The pixels are painted. 两个球离得远时各是各的圆;一靠近,中间的场叠加超过阈值,就像水银一样“粘”Together——This is it Merging Ball (metaball)。 The first ball follows the pointer, the other ball moves along Lisa's trajectory.

Open the interactive demo · GitHub