Canvas
How it works
You place a <canvas> element on the page (optionally with width/height attributes; default 300ร150). In JavaScript you get a reference to the element (HTMLCanvasElement) and call getContext("2d"), receiving a CanvasRenderingContext2D object. You then issue drawing commands on that context โ e.g. fillStyle, fillRect(x, y, w, h), paths (Path2D), text, images โ which immediately modify the bitmap (immediate mode). Because the canvas stores only pixels, the application manages scene state itself and redraws every frame (e.g. in a requestAnimationFrame loop). For 3D you use the "webgl"/"webgl2" or "webgpu" context instead of "2d". OffscreenCanvas allows drawing off the main thread (e.g. in a Web Worker).
Problem solved
Early HTML had no native way to draw graphics dynamically and programmatically on the client. Canvas provides a raster surface on which a script can draw pixel by pixel (shapes, text, images, video frames) without plugins such as Flash.
Components
The HTML element embedding the bitmap on the page; exposes the getContext() method. Default dimensions 300ร150 px.
Object returned by getContext("2d"); provides the API to draw shapes, paths, text and images.
Official
Method selecting the drawing context by id: "2d", "bitmaprenderer", "webgl", "webgl2", "webgpu". Returns null for an unsupported or mismatched context.
3D rendering contexts on the same canvas surface; "webgl"/"webgl2" are based on OpenGL ES, "webgpu" is the newer GPU standard.
Official
A canvas that runs off the main thread (e.g. in a Web Worker), decoupled from the DOM.
Official
Implementation
Canvas content is not exposed to accessibility tools the way semantic HTML is โ it is just a bitmap.
Canvas pixels do not scale and can become blurry under magnification or on high-density displays.
Canvas keeps no information about drawn objects; after any change the scene must be redrawn manually.
Evolution
Canvas appears in the WebKit component on Mac OS X, powering Dashboard widgets and the Safari browser.
Mozilla's Gecko engine adds canvas support in version 1.8.
Opera adds canvas support; the element is subsequently standardized by the WHATWG as part of HTML5 / the HTML Living Standard.
Hyperparameters (configurable axes)
Bitmap width in pixels. Defaults to 300. Changing it clears the canvas.
Bitmap height in pixels. Defaults to 150. Changing it clears the canvas.
Context id passed to getContext(): "2d", "bitmaprenderer", "webgl", "webgl2", "webgpu".
Hardware requirements
The 2D context runs on any hardware (CPU) and rendering may be GPU-accelerated; the WebGL/WebGPU contexts use the GPU directly.