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.
<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:
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 singlepaintevent 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 —
ElementImageobjects can be transferred to a web worker and rendered using anOffscreenCanvasto 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.
<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:
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
paintevent, which fires whendrawableelements have been updated and require redrawing to display the changes. ElementGeometryUpdateEvent-
Event object for the
elementgeometryupdateevent, which fires whendrawableelement geometry has been updated via anOffscreenCanvas. ElementImage-
Represents a snapshot of a
drawableelement's rendered state (captured usingHTMLCanvasElement.captureElementImage()), which can be rerendered later on, including via anOffscreenCanvas.
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 HTMLcontentattribute. HTMLCanvasElement.captureElementImage()-
Captures a snapshot of a
drawableelement's current rendered state, which can be rendered later on. HTMLCanvasElement.updateElementGeometry()-
Updates the geometry information of a
drawableelement (orElementImage)'s underlying DOM presence. HTMLCanvasElement.clearElementGeometry()-
Clears any previous geometry applied to a
drawableelement (orElementImage)'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 withdrawElementImage()(specifying no destination size). HTMLCanvasElement.getElementTransform()-
Returns the
DOMMatrixrepresenting the transform currently applied to adrawableelement (orElementImage)'s underlying DOM presence. HTMLCanvasElement.requestPaint()-
Schedules a single
paintevent even if no repaints occurred.
Events
paint-
Fired when
drawableelements have been updated and require redrawing to display the changes. elementgeometryupdate-
Fired when
drawableelement geometry has been updated via anOffscreenCanvas.
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 itscontentattribute set todrawable, using the HTML-in-Canvas API. Reflects the value of the HTMLdrawableattribute.
2D canvas context
CanvasRenderingContext2D.drawElementImage()-
Draws a
drawableelement's state recorded during the current rendering update (or anElementImage) to a 2D canvas context.
WebGL context
WebGLRenderingContext.texElementSubImage2D()-
Uploads a
drawableelement's state recorded during the current rendering update (or anElementImage) into a texture that can be used in a WebGL canvas context.
WebGPU context
GPUQueue.drawElementImageToTexture()-
Draws a
drawableelement's state recorded during the current rendering update (or anElementImage) 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
ElementImagecaptured during the current rendering update to an offscreen 2D canvas context.
Markup additions
<canvas>contentattribute-
Enables opting a
<canvas>element into rendering its descendants rather than treating them as fallback content. - HTML
drawableattribute, SVGdrawableattribute, MathMLdrawableattribute -
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
- Canvas API
- WebGL API
- WebGPU API
- HTML-in-Canvas on developer.chrome.com (2026)