A pixel grid
Once you draw, the page stores colored pixels. Each shape is not its own HTML element.

The HTML5 <canvas> element is a bitmap drawing surface. JavaScript paints pixels with the Canvas 2D API (or WebGL), so you can draw shapes, charts, animations, and games in the browser.
Once you draw, the page stores colored pixels. Each shape is not its own HTML element.
An empty <canvas> shows nothing. You call getContext('2d'), then methods such as fillRect.
To animate, clear the surface and paint the next frame. requestAnimationFrame is the usual loop.
Pipeline:
<canvas> → getContext('2d') → draw methods → pixels on screen
Raster: pixel grid (not DOM shapes)
Animate: clear + redraw (often with requestAnimationFrame)
Size: set width/height attributes (drawing resolution) Unlike SVG, canvas does not keep each shape as a DOM node. Once you draw, you have colored pixels. To move something, you usually clear and redraw.
Set the size in HTML. Put width and height on the element itself. Sizing the canvas with CSS alone stretches the bitmap and can blur the drawing.
Games, dashboards, and creative tools often need to redraw many pixels every frame without creating thousands of DOM elements.
Paint a bitmap each frame. The page does not rebuild a tree of elements.
Read and write pixels with getImageData when you build filters or photo tools.
Move many objects without giving each one its own element.
Draw custom plots, brushes, and dashboards that markup alone cannot describe.
Every sketch follows the same short loop.
<canvas width="400" height="200"> is a 400 by 200 drawing.canvas.getContext('2d') returns the object that exposes the drawing methods.fillRect, stroke, and fillText to paint pixels.requestAnimationFrame.Always set width and height attributes. CSS size alone stretches the bitmap and can blur the image.
<canvas id="myCanvas" width="400" height="200"></canvas> const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 100, 100); getElementById returns the element from the page.
getContext('2d') is the object that exposes the drawing methods.
fillStyle sets the color. fillRect(x, y, w, h) fills a rectangle.
Next, practice strokes with Canvas Draw Lines.
Both Canvas and SVG draw graphics in the browser. Pick the one that matches the job.
| Topic | SVG | Canvas | PNG / JPEG |
|---|---|---|---|
| What it is | Vector markup | Raster pixels drawn with JavaScript | Raster image files |
| Page structure | Each shape is in the DOM | No element per shape | One bitmap |
| Scaling | Stays sharp | Can blur if stretched | Pixelates when enlarged |
| Best for | Icons and diagrams | Games and animation | Photographs |
You redraw many objects every frame, edit pixels or photos, or build a 2D game or particle system.
Use SVG for crisp icons, diagrams, CSS styling, and clickable shapes. Use PNG or JPEG for photographs.
Many sites use both: SVG for UI icons, Canvas for interactive plots or games. See the SVG introduction for the vector side.
These are the drawing methods you will meet first in this tutorial.
These are the places a scripted bitmap shows up in real products.
Many sprites are redrawn each frame.
Custom plots stay light because they do not build a heavy DOM tree.
Brushes, filters, and pixel edits live on the bitmap.
Particles and transitions run inside requestAnimationFrame.
Procedural shapes and patterns are painted in code.
Physics and interactive visualizations update by redrawing the frame.
getContext('2d'), then draw methods.fillRect, then lines, paths, and arcs.Add a <canvas width="400" height="200"></canvas> element, then call canvas.getContext('2d') in JavaScript to draw shapes, text, and animations pixel by pixel.
Browse lines, rectangles, arcs, curves, and paths — then open each guide.
11 people found this page helpful