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

Magnetic Cursor Follow · How it works & code

Magnetic attraction button (distance decay) + Spring follow cursor with trail.

Runtime logic

Visiting `/physics/magnetic`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'magnetic' })`. mount resolves the variant to family `magnetic`; `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; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; 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.fillRect() — Fills a rectangle; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.stroke() — Strokes the current path; ctx.fill() — Fills the current path; ctx.fillText() — Draws filled text; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; 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.magnetic (L160-L203)

Code walkthrough

L162-162: Control definitions (colour/speed/size/count/extras): Button color, Button size, Button Count, Magnetic attraction radius R, Attraction strength k, Follow stiffness ω. L163-163: Preset buttons: name → parameter overrides (5). L164-172: Info-panel content: live rows(v), formula, notes, params, steps. L173-202: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `fillRect`, `setLineDash`, `beginPath`, `arc`, `stroke`, `fill` …; math via `Math.min`, `Math.ceil`, `Math.floor`, `Math.cos`, `Math.sin`, `Math.abs` …; returns readouts `near`, `d`, `off`, `lagd`, `R`, `k`, `w` … to the info panel.

Principles

d Button offset spring:ö = ω²(o* − o) − 2·0.8ω·ȯ Follow cursor:ẍ = ω²(p − x) − 2·0.7ω·ẋ “Magnetic attraction button”It's the interactive details of a lot of websites: the buttons when the pointer comes close to the pointer are“吸”The pointer, the closer you get, the closer you get, the more you get R Just get back in position. To make the movement soft, the buttons don't jump directly to the target, but use them Spring Following through, so there's a little elasticity and delay. 圆环光标也用弹簧跟随指针,滞后距离与速度和刚度 ω 有关;它身后的拖尾是最近的轨迹。点击按钮会有一次脉冲。

Open the interactive demo · GitHub