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

Cloth · How it works & code

Cloth using Verlet integration and distance constraints.

Runtime logic

Visiting `/canvas/cloth`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'cloth' })`. mount resolves the variant to family `cloth`; `withControls` builds the control panel (3 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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. 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; 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.onDown — Pointer down; S.onUp — Pointer up; 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.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.arc() — Adds a circular arc (center, radius, start/end angle)

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.cloth (L933-L989)

Code walkthrough

L935-935: Control definitions (colour/speed/size/count/extras): cloth color, cloth size, grid density, constraint iterations (stiffness), gravity g, wind force. L936-936: Preset buttons: name → parameter overrides (3). L937-945: Info-panel content: live rows(v), formula, notes, params, steps. L946-988: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `closePath`, `fill`, `arc`; math via `Math.max`, `Math.round`, `Math.min`, `Math.hypot`, `Math.sign`, `Math.abs`; returns readouts `np`, `nc`, `it`, `st`, `g`, `wind`, `grab` to the info panel.

Principles

Verlet:xₙ₊₁ = xₙ + (xₙ − xₙ₋₁)·0.99 + a·Δt² Binding:|pᵢ − pⱼ| → L₀, Move half the error Stretch = Welfare|pᵢ − pⱼ| − L₀) / L₀ It's seen as a piece of cloth by the mass and“Cord”Make up a network. For each prime point Verlet Integral Update: Speed implied“This frame is different from the previous frame”Lee, very stable. After each step, fix every rope and restore it to its original length L₀ ( Distance limit ). The more amendments are made, the harder the cloth, the harder it is to be stretched. The top row is nailed; holding the drag can hold the nearest point and the pointer is pushed away from the nearby cloth when moving.

Open the interactive demo · GitHub