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

Drop-shadow Glow · How it works & code

Unlike box-shadow, drop-shadow generates shadows along the actual outline of the graphic, including transparent areas, so stars...

Runtime logic

Visiting `/filters/drop-shadow`, DemoHost lazy-loads `demos/kits/kit-filters.js` (registry `mod: 'kit-filters'`) and calls `mount(host, { variant: 'drop-shadow' })`. mount resolves the variant to family `drop-shadow`; `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.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.restore() — Restores the saved drawing state; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.stroke() — Strokes the current path; ctx.rect() — Adds a rectangle to the path; ctx.clip() — Turns the current path into the clipping region; ctx.translate() — Translates the coordinate system; ctx.lineTo() — Adds a straight line to a point

Source code

frontend/src/demos/kits/kit-filters.js: FAM.drop-shadow (L259-L272)

Code walkthrough

L259-272: Calls the factory `fFam()` to produce the full definition (spec + doc + build) from the options below. L259-259: Title shown in the info and control panels: “Drop-shadow Glow”. L259-259: Range/default of the count slider. L259-259: Extra demo-specific slider/toggle: Shadow blur. L259-259: Range/default of the size slider. L259-259: Default main colour. L259-259: Hide sample photo. L260-260: Help text of the count parameter. L260-260: Help text of the extra parameter. L260-260: Help text of the size parameter. L261-261: Preset buttons: name → parameter overrides (5). L262-262: Body of the “Notes” section. L263-263: Main formula of the “Formula” section. L263-263: Second formula line. L264-272: Per-frame draw function: draws with Canvas 2D `fillRect`, `beginPath`, `arc`, `fill`, `save`, `restore`; math via `Math.sin`, `Math.hypot`, `Math.round`; returns readouts `mode`, `cell` to the info panel.

Principles

Shadow Offset = (chart center - light source) · 0.12 (maximum 40px) Filter: drop-shadow (dx dy blur colour) Projection drop- 44, Love also has the shadow of a shape. The direction of the shadow is determined by the light source: the light source here is your pointer, and the shadow always falls on the side of the graphic back to pointer, and the longer the shadow is.

Open the interactive demo · GitHub