CanvasRenderingContext2D: getContextAttributes() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2023.

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.

Syntax

js
getContextAttributes()

Parameters

None.

Return value

A CanvasRenderingContext2DSettings object that contains the actual context parameters. It has the following members:

alpha Optional

A 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 Optional

Indicates the color space of the rendering context. Possible values are:

colorType Optional

Indicates 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 Optional

A Boolean indicating the user agent reduced the latency by desynchronizing the canvas paint cycle from the event loop.

willReadFrequently Optional

A Boolean indicating whether or not this canvas uses software acceleration (instead of hardware acceleration) to support frequent read-back operations via getImageData().

Examples

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.

js
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):

js
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}

Specifications

Specification
HTML
# 2dcontext:dom-context-2d-canvas-getcontextattributes-2

Browser compatibility

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
Chrome – No support
Chrome 32 – 59 (Release date: 2014-01-14)
footnote Removed in 60 and later
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 117 (Release date: 2023-08-29)
footnote Full support
Opera – No support
Opera 19 – 46 (Release date: 2014-01-28)
footnote Removed in 47 and later
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – No support
Chrome Android 32 – 59 (Release date: 2014-01-15)
footnote Removed in 60 and later
Chrome Android – Full support
Chrome Android 73 (Release date: 2019-03-12)
footnote Full support
Firefox for Android – Full support
Firefox for Android 117 (Release date: 2023-08-29)
footnote Full support
Opera Android – No support
Opera Android 19 – 43 (Release date: 2014-01-28)
footnote Removed in 44 and later
Opera Android – Full support
Opera Android 52 (Release date: 2019-05-17)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – No support
Samsung Internet 2 – 7.4 (Release date: 2014-10-17)
footnote Removed in 8 and later
Samsung Internet – Full support
Samsung Internet 11 (Release date: 2019-12-05)
footnote Full support
WebView Android – No support
WebView Android 4.4.3 – 59 (Release date: 2014-06-02)
footnote Removed in 60 and later
WebView Android – Full support
WebView Android 73 (Release date: 2019-03-12)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also