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

Masks · How it works & code

SVG mask/clipPath。

Runtime logic

Visiting `/svg/mask`, DemoHost lazy-loads `demos/kits/kit-svg.js` (registry `mod: 'kit-svg'`) and calls `mount(host, { variant: 'mask' })`. mount resolves the variant to family `mask`; `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; 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.clear — Clears with the background colour (translucent = trails); 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.clearRect() — Clears a rectangle; ctx.fillRect() — Fills a rectangle; ctx.createRadialGradient() — Creates a radial gradient; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.drawImage() — Draws an image or offscreen canvas; ctx.fillText() — Draws filled text; ctx.arcTo() — Adds an arc using tangent lines; ctx.stroke() — Strokes the current path; ctx.save() — Saves the drawing state (transform, styles, clip)

Source code

frontend/src/demos/kits/kit-svg.js: FAM.mask (L417-L435)

Code walkthrough

L417-435: Calls the factory `sFam()` to produce the full definition (spec + doc + build) from the options below. L417-417: Title shown in the info and control panels: “Masks”. L417-417: Range/default of the count slider. L417-417: Extra demo-specific slider/toggle: Edge Blur. L417-417: Range/default of the size slider. L417-417: Default main colour. L418-418: Help text of the count parameter. L418-418: Help text of the extra parameter. L418-418: Help text of the size parameter. L419-419: Preset buttons: name → parameter overrides (5). L420-420: Body of the “Notes” section. L421-421: Main formula of the “Formula” section. L421-421: Second formula line. L422-435: Per-frame draw function: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `closePath`, `fill`, `clearRect` …; math via `Math.max`, `Math.ceil`, `Math.sin`, `Math.cos`, `Math.min`, `Math.hypot` …; returns readouts `mode`, `cell` to the info panel.

Principles

Show = content · Mask Brightness (0 ~ 1) Plumbing: Brightness = 1 - slow motion ((d − (r − f)) / f) Mask: 里白色区域让下面的内容显示、黑色区域把它挡住,灰色则半透明。这里在一层深色幕布上「挖」出几个圆孔(mask 中的白色圆,边缘用径向渐变羽化),圆孔沿轨道移动,指针处也有一个孔,透过孔能看到下面流动的彩色条纹。clipPath Instead, it can only be cut hard, without a semi-transparent transition. SVG It's "The Vector Chart in Text":&lt;path d="M… L… C… A…"&gt; 里 M Means moving to a point, L Draw straight lines, C Draw three Bézier curves, A Draw round a Elimination Translate. Here Canvas 的 Path2D Parsing the same path, then pressing time t 改变描边、形状、颜色等属性,效果与网页里的 SVG Animated same.

Open the interactive demo · GitHub