Pixelate Cursor · How it works & code
Pixelation via downscaling and grayscale quantization
Runtime logic
Visiting `/canvas/pixelate`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'pixelate' })`. mount resolves the variant to family `pixelate`; `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; makeLayer — Offscreen layer (trails / pixel fields); hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; 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.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.createLinearGradient() — Creates a linear gradient; 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.translate() — Translates the coordinate system; ctx.rotate() — Rotates the coordinate system; ctx.restore() — Restores the saved drawing state; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path
Source code
frontend/src/demos/kits/kit-canvas.js: FAM.pixelate (L1269-L1303)
Code walkthrough
L1271-1271: Control definitions (colour/speed/size/count/extras): Main color, Pixel block size b, Grading count, Lens radius, Black and white. L1272-1272: Preset buttons: name → parameter overrides (6). L1273-1281: Info-panel content: live rows(v), formula, notes, params, steps. L1282-1302: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `createLinearGradient`, `fillRect`, `beginPath`, `arc`, `fill`, `save` …; math via `Math.min`, `Math.cos`, `Math.sin`, `Math.round`, `Math.ceil`; returns readouts `b`, `lw`, `lh`, `np`, `ratio`, `L`, `lp` to the info panel.
Principles
Sample: W / b⌉,H′ = ⌈H / b⌉ Zoom in: Recent Neighboring Value (one colour per block) Quantification: c = round(c) · (L−1) / 255) · 255 / (L−1) The essence of pixels is Reduce Resolution : Let's narrow it down b Multiply b×b One pixel on average)“Recent Neighborhood”The way to zoom back in, there's only one color for each piece. Plus Numeric : Take every channel 0~255 Quantified to L It's like the old game. 指针处的圆形透镜显示原始画面,方便对比。数据量只剩原来的 1/b²。