Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Browser and Web APIs

The Canvas 2D drawing context

Reference for the Canvas 2D context: getContext('2d'), rectangles, paths, arcs, text, gradients, transforms, save/restore, pixel data and drawing basics.

The <canvas> element is a blank bitmap that you paint with JavaScript. You get a drawing object from it with canvas.getContext("2d"), a CanvasRenderingContext2D, and call its methods to draw rectangles, paths, text and images. Canvas is immediate mode: it keeps only the pixels, not a list of shapes, so to move something you clear the canvas and draw again.

The canvas has two sizes: its bitmap size set by the width and height attributes (default 300 by 150), and its display size set by CSS. If they differ, the bitmap is stretched and looks blurry.

JavaScript
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "tomato";
ctx.fillRect(10, 10, 100, 60);

Try it

Shapes, paths, text and a gradient
Transforms, save/restore and pixel data

Syntax

JavaScript
const ctx = canvas.getContext("2d", options)

Common members of the context:

Member Description
fillStyle, strokeStyle Color, gradient or pattern for filling and outlining.
lineWidth, lineCap, lineJoin Stroke appearance.
fillRect(x, y, w, h), strokeRect(), clearRect() Draw or erase rectangles directly.
beginPath(), moveTo(), lineTo(), arc(), rect(), closePath() Build a path. Then call fill() or stroke().
fillText(text, x, y), strokeText(), measureText() Draw text; font, textAlign and textBaseline control it.
drawImage(image, x, y) Draw an image, another canvas or a video frame.
createLinearGradient(), createRadialGradient() Create gradients with addColorStop(offset, color).
translate(), rotate(), scale(), setTransform() Change the coordinate system. Angles are in radians.
save(), restore() Push and pop the current drawing state (styles and transform).
getImageData(), putImageData() Read and write raw RGBA pixels.
globalAlpha, globalCompositeOperation Transparency and blending.

Return value: getContext("2d") returns the context, or null if the canvas already has a different kind of context. Exceptions: getImageData() throws SecurityError if the canvas is tainted by a cross-origin image drawn without CORS permission.

Usage notes

  • Set size with attributes, not CSS, for crisp drawing: <canvas width="400" height="300">.
  • High-DPI screens: multiply the bitmap size by devicePixelRatio, set the CSS size to the original, and call ctx.scale(ratio, ratio).
  • Always beginPath() before a new shape; otherwise old segments are filled or stroked again.
  • Half pixels: a 1px line at an integer coordinate is blurry because it straddles two pixels; draw at x + 0.5.
  • Animation: clear with clearRect(0, 0, canvas.width, canvas.height) and redraw inside requestAnimationFrame().
  • Accessibility: canvas content is invisible to screen readers. Add fallback content inside the element and ARIA labels, or use SVG when you need accessible shapes.
  • Large canvases cost memory; OffscreenCanvas can draw in a worker.

Browser support

The Canvas 2D context is supported in every browser. Newer additions such as roundRect(), reset() and letterSpacing are more recent, so check a compatibility table.