Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

CanvasRenderingContext2D — drawing on a bitmap

CanvasRenderingContext2D provides a stateful 2D drawing interface for rectangles, paths, text, images, and pixels. Distinguish the context state, current path, and bitmap contents.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const ctx = canvas.getContext('2d');

Obtain CanvasRenderingContext2D through canvas.getContext('2d'); check for null. This is an HTML-standard Web API, not ECMAScript. These core operations are supported by modern browser engines; Node 24 does not establish their availability. Set canvas.width and canvas.height before drawing: assigning them resets the bitmap and state.

fillRect fills a rectangle, strokeRect outlines it, and clearRect clears an area, normally to transparent black; with alpha: false, to opaque black. They do not change the path. beginPath discards the current path; moveTo starts a subpath, lineTo adds a segment, and closePath closes the subpath. arc adds an arc with angles in radians; rect adds a rectangle. fill fills the path; stroke outlines it.

fillStyle and strokeStyle specify colors, gradients, or patterns; lineWidth specifies line thickness. fillText fills text and strokeText outlines it; font selects the typeface and size, while textBaseline selects the reference baseline. measureText returns TextMetrics, including width; metrics depend on the font.

save stores state and restore reinstates it, without undoing pixels or the path. setTransform(a, b, c, d, e, f) replaces the transformation. clip intersects the clipping region with the path; use save/restore to scope it. drawImage draws an image source, optionally cropping and scaling.

getImageData reads pixels as ImageData, and putImageData writes them; both ignore the transformation, and writing also ignores clipping. A cross-origin image without appropriate CORS can taint the bitmap: reading throws SecurityError. Provide an accessible equivalent of bitmap content.

Reference identifiers covered by this entry: CanvasRenderingContext2D.clearRect, CanvasRenderingContext2D.fillRect, CanvasRenderingContext2D.strokeRect, CanvasRenderingContext2D.beginPath, CanvasRenderingContext2D.moveTo, CanvasRenderingContext2D.lineTo, CanvasRenderingContext2D.closePath, CanvasRenderingContext2D.stroke, CanvasRenderingContext2D.arc, CanvasRenderingContext2D.fill, CanvasRenderingContext2D.fillText, CanvasRenderingContext2D.strokeText, CanvasRenderingContext2D.lineWidth, CanvasRenderingContext2D.strokeStyle, CanvasRenderingContext2D.fillStyle, CanvasRenderingContext2D.font, CanvasRenderingContext2D.textBaseline, CanvasRenderingContext2D.save, CanvasRenderingContext2D.restore, CanvasRenderingContext2D.measureText, CanvasRenderingContext2D.setTransform, CanvasRenderingContext2D.rect, CanvasRenderingContext2D.clip, CanvasRenderingContext2D.drawImage, CanvasRenderingContext2D.getImageData, CanvasRenderingContext2D.putImageData.

Examples

(() => {
  const canvas = document.createElement('canvas');
  canvas.width = 64;
  canvas.height = 48;
  const ctx = canvas.getContext('2d');
  if (!ctx) throw new Error('Canvas 2D unavailable');
  document.body.append(canvas);

  ctx.clearRect(0, 0, 64, 48);
  ctx.save();
  ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.beginPath();
  ctx.rect(0, 0, 32, 48);
  ctx.clip();
  ctx.fillStyle = '#ff0000';
  ctx.fillRect(0, 0, 64, 48);
  ctx.restore();
  ctx.beginPath();

  const pixel = ctx.getImageData(8, 8, 1, 1);
  ctx.putImageData(pixel, 48, 8);
  console.log(Array.from(ctx.getImageData(48, 8, 1, 1).data).join(','));
  console.log(Array.from(ctx.getImageData(40, 8, 1, 1).data).join(','));
})();

Result

255,0,0,255
0,0,0,0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources