HTML-in-Canvas API

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

The HTML-in-Canvas API allows you to draw DOM elements directly into a 2D or 3D canvas context. This exploits the high-performance, graphically rich nature of canvas, while the content's DOM representation is retained in the background, meaning that it remains interactive, accessible, and integrated with key browser features such as autocomplete, text selection, find-in-page, and keyboard navigation.

Concepts and usage

Drawing UI features inside a <canvas> element is a common path for developers that want to create graphically rich websites that offer different experiences than might be possible using DOM elements. However, this comes with its own set of problems. Traditionally, anything drawn into a canvas context is just a set of pixels, and therefore isn't accessible, and doesn't benefit from the same level of interactivity or browser integration as the DOM.

HTML-in-Canvas provides a solution, allowing DOM subtrees to be drawn into a canvas context. The content's state is retained in the background, meaning that it remains accessible, while its visual rendering can be manipulated using your chosen canvas API (2D Canvas API, WebGL, or WebGPU) in whatever way you like.

To prepare DOM content to be rendered on a canvas, you first need to place it between your <canvas> tags. You then opt your <canvas> into rendering its descendants rather than treating them as fallback content by setting its content attribute to drawable. Finally, each DOM subtree that you want to render on the canvas independently is given a drawable attribute.

html
<canvas width="640" height="480" content="drawable">
  <div id="subtree1" drawable>...</div>
  <div id="subtree2" drawable>...</div>
</canvas>

Each canvas context type (except bitmaprenderer) comes with a feature to draw drawable DOM trees onto the context. For example, the CanvasRenderingContext2D.drawElementImage() method takes an element reference and various other arguments to declare where and how it should be drawn. The simplest version takes x and y coordinates that define where the top-left corner of the element should be placed:

js
const canvas = document.querySelector("canvas");
const div1 = document.querySelector("#subtree1");
const ctx = canvas.getContext("2d");
// ...
canvas.addEventListener("paint", () => {
  ctx.drawElementImage(div1, 100, 100);
});

The element draw call needs to be invoked inside a paint event handler to guarantee that the element has been painted, and that the latest paint will be drawn.

In 3D contexts, the elements are uploaded/drawn into textures to be used inside fragment shaders: WebGLRenderingContext.texElementSubImage2D() in the case of WebGL, and GPUQueue.drawElementImageToTexture() in the case of WebGPU.

Other features include:

  • The HTMLCanvasElement.getElementImageDefaultSize() method, which returns the element's default rendered size. This is useful if you want to re-render the element at a size relative to its default size, or preallocate texture storage, for example.
  • The HTMLCanvasElement.requestPaint() method, which schedules a single paint event even if no repaints occurred, useful for example if you want to move an element around the canvas when its state hasn't changed.
  • The HTMLCanvasElement.updateElementGeometry() method, which can be used for example to update the size and position of a rendered element's underlying DOM presence so it remains interactive in cases where the geometry is not updated automatically.
  • Offscreencanvas rendering — ElementImage objects can be transferred to a web worker and rendered using an OffscreenCanvas to improve performance on render-heavy apps. See our Offscreencanvas reference pages for more information and examples.

For guides, see the HTML-in-Canvas series on developer.chrome.com.

Accessibility

drawable DOM elements are exposed to the accessibility tree in the same way as regular DOM elements; this includes hiding them using features such as aria-hidden, inert, etc.

There is one exception: if a drawable element's geometry is not updated (either automatically via drawElementImage() or explicitly with updateElementGeometry()), the drawable element and its subtree are reported to the accessibility tree without geometry information. This allows assistive technologies to filter out content without geometry information. It is important that the accessibility implications of unused canvas descendants, such as hidden views or no-longer-drawn content, are considered. In many cases, these should be removed from the DOM or hidden.

Note: When elements are rendered to a canvas using drawElementImage(), texElementSubImage2D(), or drawElementImageToTexture(), accessibility indicators such as focus rings or carets are rendered as expected.

Consider the following example, where a <figure>, <img>, and <caption> are included inside a <canvas content="drawable">. The <img> and <figcaption> elements include the drawable attribute.

html
<canvas id="canvas" content="drawable">
  <figure id="figure">
    <img drawable id="image" src="..." alt="image alt text" />
    <figcaption drawable id="caption">A caption</figcaption>
  </figure>
</canvas>

The semantic structure of the <canvas> content is preserved, while the <img> and <figcaption> can be rendered separately:

js
const canvas = document.querySelector("canvas");
const image = document.querySelector("img");
const caption = document.querySelector("figcaption");
const ctx = canvas.getContext("2d");

canvas.addEventListener("paint", () => {
  ctx.reset();
  // By default, drawElementImage() automatically updates element geometry,
  // so that the image and caption have geometry information for accessibility.
  ctx.drawElementImage(image, 0, 0);
  ctx.drawElementImage(caption, 0, 200);
});

Interfaces

CanvasPaintEvent

Event object for the paint event, which fires when drawable elements have been updated and require redrawing to display the changes.

ElementGeometryUpdateEvent

Event object for the elementgeometryupdate event, which fires when drawable element geometry has been updated via an OffscreenCanvas.

ElementImage

Represents a snapshot of a drawable element's rendered state (captured using HTMLCanvasElement.captureElementImage()), which can be rerendered later on, including via an OffscreenCanvas.

Extensions to other interfaces

HTMLCanvasElement

HTMLCanvasElement.content

Indicates whether the <canvas> element is opted into rendering its descendants rather than treating them as fallback content. Reflects the value of the HTML content attribute.

HTMLCanvasElement.captureElementImage()

Captures a snapshot of a drawable element's current rendered state, which can be rendered later on.

HTMLCanvasElement.updateElementGeometry()

Updates the geometry information of a drawable element (or ElementImage)'s underlying DOM presence.

HTMLCanvasElement.clearElementGeometry()

Clears any previous geometry applied to a drawable element (or ElementImage)'s underlying DOM presence, removing its interactivity and marking it as offscreen in the accessibility tree.

HTMLCanvasElement.getElementImageDefaultSize()

Returns an object containing the default size, in canvas units, of an element (or ElementImage) when drawn with drawElementImage() (specifying no destination size).

HTMLCanvasElement.getElementTransform()

Returns the DOMMatrix representing the transform currently applied to a drawable element (or ElementImage)'s underlying DOM presence.

HTMLCanvasElement.requestPaint()

Schedules a single paint event even if no repaints occurred.

Events

paint

Fired when drawable elements have been updated and require redrawing to display the changes.

elementgeometryupdate

Fired when drawable element geometry has been updated via an OffscreenCanvas.

HTMLElement, SVGElement, MathMLElement

HTMLElement.drawable, SVGElement.drawable, MathMLElement.drawable

Marks an element as a paint root, meaning that it can be rendered independently on a parent <canvas> element with its content attribute set to drawable, using the HTML-in-Canvas API. Reflects the value of the HTML drawable attribute.

2D canvas context

CanvasRenderingContext2D.drawElementImage()

Draws a drawable element's state recorded during the current rendering update (or an ElementImage) to a 2D canvas context.

WebGL context

WebGLRenderingContext.texElementSubImage2D()

Uploads a drawable element's state recorded during the current rendering update (or an ElementImage) into a texture that can be used in a WebGL canvas context.

WebGPU context

GPUQueue.drawElementImageToTexture()

Draws a drawable element's state recorded during the current rendering update (or an ElementImage) into a texture that can be used in a WebGPU canvas context.

OffscreenCanvas

OffscreenCanvas.updateElementGeometry()

Updates the geometry applied to an ElementImage's underlying DOM presence.

OffscreenCanvas.clearElementGeometry()

Clears any previous geometry applied to an ElementImage's underlying DOM presence.

OffscreenCanvas.getElementImageDefaultSize()

Returns an object containing the default size, in canvas units, of an ElementImage.

OffscreenCanvasRenderingContext2D.drawElementImage()

Draws an ElementImage captured during the current rendering update to an offscreen 2D canvas context.

Markup additions

<canvas> content attribute

Enables opting a <canvas> element into rendering its descendants rather than treating them as fallback content.

HTML drawable attribute, SVG drawable attribute, MathML drawable attribute

Marks an element as a paint root, meaning that it (along with its descendants) can be rendered independently on a parent <canvas content="drawable"> element.

Examples

Specifications

This feature does not appear to be defined in any specification.

Browser compatibility

See also