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

Drag to Reorder · How it works & code

Drag-and-sort board with critical damping spring animation for card return.

Runtime logic

Visiting `/physics/drag`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'drag' })`. mount resolves the variant to family `drag`; `withControls` builds the control panel (5 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; hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; clamp — Clamps a value to a range; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.get — Reads current control values; S.onDown — Pointer down; S.onUp — Pointer up; S.isAutoDemo — Whether auto demo is on; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.fillText() — Draws filled text; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.stroke() — Strokes the current path; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path

Source code

frontend/src/demos/kits/kit-physics.js: FAM.drag (L102-L158)

Code walkthrough

L104-104: Control definitions (colour/speed/size/count/extras): Card Highlight Color, Card Height, Number of Cards, Spring Angular Frequency ω, Damping Ratio ζ, Auto Demonstration Drag. L105-105: Preset buttons: name → parameter overrides (5). L106-114: Info-panel content: live rows(v), formula, notes, params, steps. L115-157: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `fill`, `fillRect`, `fillText`, `setLineDash`, `stroke`; math via `Math.floor`, `Math.round`, `Math.min`, `Math.sin`, `Math.max`; returns readouts `gap`, `cw`, `h`, `top`, `colX`, `H`, `n` … to the info panel.

Principles

Spring–Stop:ẍ = ω²(x Target − x) − 2ζω·ẋ ζ = 1 Critical resistance: the fastest available and the fastest available;ζ Insert Serial number i = round((y pointer - y column top) / (h + spacing)) Drag Sorter“Slippery”Come from Spring Animation :每张卡片都有一个目标位置,卡片像挂在弹簧上一样被拉过去,同时受阻尼减速。 Angular Frequency ω Make the decision slow, Nieby ζ Whether or not to return:ζ = 1 Yes Critical obstruction , It's the fastest and not too much UI (a) Animation is most commonly used;ζ The smaller the bounce, the more obvious. When you drag, insert a serial number according to the pointer ' s location, and the other cards give way; the card drops to a new location.

Open the interactive demo · GitHub