Canvas 2D in Depth
Draw thousands of marks as pixels: the canvas and its context, paths, text, transforms, sharp high-DPI drawing, images, pixels, animation and accessibility.
SVG gives every mark its own page element, and that is wonderful until you have ten thousand of them. Canvas is the other way to draw: one element, one grid of pixels, and a set of drawing commands. Nothing is remembered, so there is nothing to slow down.
This phase teaches the canvas properly. You meet the context and the two sizes every canvas has, then paths, styles and text, then transforms and the state stack, which cause most canvas surprises. You make drawings sharp on high-resolution screens, work with images and single pixels, animate, and make a canvas readable for people who cannot see it.
At the end you add a canvas to the Lumen Bikes dashboard that draws all 10,000 June trips as dots, sharp on every screen, with a summary and a table beside it.
Phase 11 · the story
Arjun, engineer
“I want to draw every trip, all 10,000 of them, and the SVG page is starting to crawl.”
Priya asked a simple question on Thursday: "Where do our trips start?" Arjun tried the obvious thing. One SVG circle per trip, 10,000 circles, each one a page element the browser has to keep, style and lay out. The page took seconds to appear and the scroll stuttered.
He knows the other tool. A canvas is one element: you tell it to paint, it paints pixels, and it forgets. Ten thousand dots are ten thousand paint commands, not ten thousand things to look after. The catch is that everything he took for granted in SVG is now his job: sizes, sharpness on phones, what a screen reader hears, what happens when you click.
In this phase you learn the canvas properly, and then you give the dashboard its first canvas: every June trip as a dot, sharp on every screen, with a summary and a table for the people who cannot see the picture.
In this phase
You will learn
- Set up a canvas with the right size, and draw paths, shapes and text on it
- Use transforms and the state stack without leaking one drawing's settings into the next
- Make a canvas sharp on high-resolution screens and animate it with a redraw loop
- Draw 10,000 dots on a canvas and describe the picture in words and in a table
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/canvas.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.