Big Data: Canvas, Hit-Testing and Performance
Measure where SVG breaks, draw big data on canvas with D3, click marks you cannot select, show less to show more, and use workers and Konva.
Every chart so far had a few hundred marks. A year of trips has hundreds of thousands, and the page freezes. This phase starts by measuring: how many elements the browser can keep, and where SVG stops being the right tool.
Then you move the same D3 scales and shapes onto a canvas, and fix what canvas takes away: there is nothing to click, so you find the mark under the pointer yourself, with isPointInPath, a hidden colour canvas, a quadtree and a Delaunay diagram. You learn to show less to show more (sampling, binning, level of detail), to keep slow work off the main thread with workers and OffscreenCanvas, and when a scene graph such as Konva is worth its cost.
At the end the dashboard gets a station map that stays fast with a year of rides under it, lets Priya click a station by mouse, finger or keyboard, and shows how long each layer took to draw.
Phase 12 · the story
Priya, operations lead
“With the whole year on the map the page freezes, and I can't click a station to see what it did.”
Arjun's trips canvas from last time was a hit: ten thousand June trips as one picture. Priya wants the same for the whole year, and she wants to click a station and read its numbers, because that is how she plans where the vans go.
The first try is a disaster. Drawing every ride as a page element makes the browser stall for seconds, and a canvas, which draws fast, has no stations to click at all: it is one flat picture. Priya waits, clicks, and nothing happens.
Arjun measures instead of guessing. He finds where the page slows down, draws a count of rides instead of every ride, and builds the one thing canvas lacks: a way to ask which station is under the pointer. By the end of this phase the map is fast, honest about what it aggregated, and clickable.
In this phase
You will learn
- Measure SVG against canvas on your own device and choose with numbers, not habit
- Draw with D3 scales and shapes on a canvas, and find the mark under a click with a quadtree, colour picking or
isPointInPath - Show a year of data by sampling or counting, and say what you left out
- Keep heavy work off the main thread with a worker, and decide when Konva is worth it
You write code in
In your Vite project lumen-dashboard/ (run npm run dev), and in the editor on this page
Files you touch
- src/utils/hit-test.js
- src/charts/station-map.js
- src/charts/trips-canvas.js
- src/main.js
Practise this phase
Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.