CKTIS / JavaScript
HTMLCanvasElement: drawing context and dimensions
HTMLCanvasElement provides a browser drawing surface. Learn how to use getContext(), width, and height. With a 2D context, assigning either dimension clears the drawing and resets context state, even when the value does not change.
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
canvas.getContext(contextType, contextAttributes)
canvas.width = width;
canvas.height = height;HTMLCanvasElement represents a browser canvas element. The element itself holds the drawing surface; a context provides drawing operations. This is a Web API, not part of ECMAScript. Having Node 24 in a repository does not establish support for this API.
Call HTMLCanvasElement.getContext on an instance as canvas.getContext("2d") to obtain a CanvasRenderingContext2D. An optional second argument specifies context settings. Check the result: the method returns null if the type is unsupported, the element already has a different context mode, or context creation fails. For example, even for a supported WebGL context type, a call with failIfMajorPerformanceCaveat enabled can return null, and a retry on the same canvas with that option disabled can succeed. Once a context is successfully created, subsequent calls on the same canvas with the same context type return that same object. After control is transferred through transferControlToOffscreen(), the method throws InvalidStateError.
HTMLCanvasElement.width and HTMLCanvasElement.height reflect the dimension attributes and determine the internal drawing surface size. Their defaults are 300 and 150, respectively. The display size set through CSS is separate: it scales the image, potentially making it blurry or distorted.
With a 2D context, assigning either width or height clears the surface, discards the current path, and resets context settings such as fillStyle to their defaults, even when assigning the current value. Set dimensions before configuring the 2D context and drawing; restore state and redraw after resizing. Resizing a WebGL drawing buffer does not reset WebGL context state. After transferring control to an OffscreenCanvas through transferControlToOffscreen(), assigning either width or height on the original element throws InvalidStateError. The core API is widely available in browsers, but support for individual context types and options varies.
Examples
(() => {
const canvas = document.createElement("canvas");
console.log(canvas.width, canvas.height);
canvas.width = 4;
canvas.height = 2;
const ctx = canvas.getContext("2d");
if (ctx === null) {
throw new Error("2D context unavailable");
}
console.log(canvas.getContext("2d") === ctx);
ctx.fillStyle = "#ff0000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
canvas.width = canvas.width;
console.log(canvas.width, canvas.height, ctx.fillStyle);
})();Result
300 150 true 4 2 #000000
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.