files/en-us/web/api/htmlcanvaselement/getcontext/index.md
{{APIRef("Canvas API")}}
The HTMLCanvasElement.getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported, or the canvas has already been set to a different context mode.
Later calls to this method on the same canvas element, with the same contextType argument, will always return the same drawing context instance as was returned the first time the method was invoked. It is not possible to get a different drawing context object on a given canvas element.
getContext(contextType)
getContext(contextType, contextAttributes)
contextType
: A string containing the context identifier defining the drawing context associated to the canvas. Possible values are:
"2d"
"webgl" (or "experimental-webgl")
"webgl2"
"webgpu"
"bitmaprenderer"
[!NOTE] The identifier
"experimental-webgl"is used in new implementations of WebGL. These implementations have either not reached test suite conformance, or the graphics drivers on the platform are not yet stable. The Khronos Group certifies WebGL implementations under certain conformance rules.
contextAttributes {{optional_inline}}
: You can use several context attributes when creating your rendering context, for example:
const gl = canvas.getContext("webgl", {
antialias: false,
depth: false,
});
2d context attributes:
alpha
false, the browser now knows that the backdrop is always opaque, which can speed up drawing of transparent content and images.colorSpace {{optional_inline}}
"srgb" selects the sRGB color space. This is the default value."display-p3" selects the display-p3 color space.colorType {{optional_inline}}
"unorm8" sets the color channels to 8 bit unsigned values. This is the default value."float16" sets the color channels to 16-bit floating-point values.desynchronized
willReadFrequently
WebGL context attributes:
alpha
depth
stencil
desynchronized
antialias
failIfMajorPerformanceCaveat
powerPreference
"default"
"high-performance"
"low-power"
premultipliedAlpha
preserveDrawingBuffer
xrCompatible
[!NOTE] The WebGPU specification does not define any specific context attributes for
getContext(). Instead, it provides configuration options via the {{domxref("GPUCanvasContext.configure()")}} method.
A rendering context which is either a
"2d","webgl" and "experimental-webgl","webgl2","webgpu","bitmaprenderer".If the context identifier is not supported, or the canvas has already been set to a different context mode, null is returned.
InvalidStateError {{domxref("DOMException")}}
Given this {{HTMLElement("canvas")}} element:
<canvas id="canvas" width="300" height="300"></canvas>
You can get a 2d context of the canvas with the following code:
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
console.log(ctx); // CanvasRenderingContext2D { /* … */ }
Now you have the 2D rendering context for a canvas and you can draw within it.
{{Specifications}}
{{Compat}}
HTMLCanvasElement.getContext() method