Since August 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The CanvasRenderingContext2D.getContextAttributes() method returns an object that contains attributes used by the context.
Note that context attributes may be requested when creating the context with HTMLCanvasElement.getContext(), but the attributes that are actually supported and used may differ.
getContextAttributes()
None.
A CanvasRenderingContext2DSettings object that contains the actual context parameters. It has the following members:
alpha OptionalA Boolean indicating if the canvas contains an alpha channel. If false, the backdrop is always opaque, which can speed up drawing of transparent content and images.
colorSpace OptionalIndicates the color space of the rendering context. Possible values are:
srgb: denotes the sRGB color space
display-p3: denotes the display-p3 color space
colorType OptionalIndicates the color type of the rendering context. Possible values are:
"unorm8" denotes the color channels to 8 bit unsigned values. This is the default value."float16" denotes the color channels to 16-bit floating-point values.desynchronized OptionalA Boolean indicating the user agent reduced the latency by desynchronizing the canvas paint cycle from the event loop.
willReadFrequently OptionalA Boolean indicating whether or not this canvas uses software acceleration (instead of hardware acceleration) to support frequent read-back operations via getImageData().
This example shows how you can specify context attributes when creating a canvas context, and then call getContextAttributes() to read back the actual parameters that the browser used.
First we create a context using HTMLCanvasElement.getContext(), specifying just one context attribute.
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d", { alpha: false });
If the getContextAttributes() method is supported, we use it to read back the actual attributes used by the browser (including those we explicitly specified):
if (ctx.getContextAttributes) {
const attributes = ctx.getContextAttributes();
log(JSON.stringify(attributes));
} else {
log("CanvasRenderingContext2D.getContextAttributes() is not supported");
}
Depending on the attributes supported by the browser, the log below should display a string that looks something like: {alpha: false, colorSpace: 'srgb', desynchronized: false, willReadFrequently: false}
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
getContextAttributes |
7332–60 | 79 | 117 | 6019–47 | 15 | 7332–60 | 117 | 5219–44 | 15 | 11.02.0–8.0 | 734.4.3–60 | 15 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getContextAttributes