Conversion Funnel · How it works & code
Multi-stage conversion from visit to recommendation: cumulative conversion = product of step-by-step conversion rates.
Runtime logic
Visiting `/dataviz/funnel`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'funnel' })`. mount resolves the variant to family `funnel`; `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; rng — Seeded deterministic PRNG (same seed, same result); 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.get — Reads current control values; 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.stroke() — Strokes the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.arcTo() — Adds an arc using tangent lines; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.funnel (L608-L650)
Code walkthrough
L609-609: Control definitions (colour/speed/size/count/extras): Funnel Color, Funnel Width, Stage Count, Average Conversion Rate, Layer Spacing px, Monochrome. L610-610: Preset buttons: name → parameter overrides (6). L611-619: Info-panel content: live rows(v), formula, notes, params, steps. L620-649: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `closePath`, `fill`, `stroke` …; math via `Math.round`, `Math.max`, `Math.sqrt`, `Math.floor`, `Math.random`, `Math.min`; returns readouts `n`, `st`, `cnt`, `prev`, `step`, `cum`, `lost` … to the info panel.
Principles
第 k Number of steps Nₖ = Nₖ₋₁ × pₖ(pₖ For single-step conversion rate) Cumulative Conversion = Nk Currently / N₁ = p₂ × p₃ × … × pₖ Gradient width Funnel Chart 展示用户一步步完成目标的过程:从访问网站,到注册、激活、下单、支付…每一步都会有人离开,所以越往下越窄,像个漏斗。 The two most important figures: Step Conversion Rate (What's left of this step? Cumulative conversion rate (相对于最开始还剩多少)。累计转化是所有单步转化率 Multiplication , So even every step 60%, After five steps, there's only one 13%. The small dots represent the downward movement of the user and the loss is the result of being thrown out. Pointer suspends any layer to view data.