Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

HTML-in-Canvas Drawable Content, Paint Events, WebGL Textures, and Accessibility

How to draw live DOM into 2D, WebGL, and WebGPU canvas with drawable attributes, paint events, texture upload, and geometry sync.

This page summarises Chrome’s HTML-in-Canvas documentation and related WHATWG canvas best practices, together with guidance on shaders, export, and 3D integration. The HTML-in-Canvas API lets you paint real DOM subtrees into <canvas> while keeping interactivity, accessibility, and browser features that plain canvas pixels do not expose.

HTML-in-Canvas: what it is and when you need it

Section titled “HTML-in-Canvas: what it is and when you need it”

The HTML-in-Canvas API bridges interactive DOM and high-performance canvas rendering. You can draw live subtrees into 2D, WebGL, or WebGPU contexts while the browser retains interactivity, accessibility trees, and integrations such as find-in-page and translation.

The HTML-in-Canvas API bridges the gap between the interactive and accessible world of the DOM and the high-performance, pixel-level control of <canvas>. The API lets you draw live DOM subtrees directly into 2D, WebGL, or WebGPU canvas contexts—while the browser retains interactivity, accessibility trees, and browser-level integrations.

— Chrome, HTML-in-Canvas overview

Regular <canvas> bitmaps are invisible to screen readers, in-page search, and most assistive tools unless you duplicate semantics elsewhere. With content="drawable" on the canvas and drawable on target elements, layout stays in the DOM and pixels are composited into the canvas context—see Expose canvas content to browser features.

Typical goals include GPU shader effects on real UI (Apply WebGL shaders to HTML content), HUDs in 3D scenes (Enable interactive HTML content in 3D scenes), and exporting DOM visuals via toDataURL() or captureStream() (Export HTML content from canvas).

canvas text controls: why not rebuild <input> on raw canvas?

Section titled “canvas text controls: why not rebuild <input> on raw canvas?”

If you only need forms or editable text inside a graphical surface, prefer native elements—or HTML-in-Canvas—instead of hand-drawn canvas text editors.

Authors should avoid implementing text editing controls using the canvas element. Doing so has a large number of disadvantages… This is a huge amount of work, and authors are most strongly encouraged to avoid doing any of it by instead using the input element, the textarea element, or the contenteditable attribute.

— WHATWG, The canvas element — Best practices (Web Developers Edition)

The same guidance appears in the full HTML specification. HTML-in-Canvas keeps real <input>, <button>, and text in the DOM while you composite them into the canvas.

content="drawable" and the drawable subtree: how to mark HTML for canvas

Section titled “content="drawable" and the drawable subtree: how to mark HTML for canvas”

To render HTML inside a canvas, configure two attributes on the canvas and its descendants, as described in HTML-in-Canvas: Technical concepts.

Piece Attribute Role
<canvas> content="drawable" Opts the canvas into laying out descendant DOM nodes instead of treating them only as hidden fallback content
Target elements drawable Marks an independent paint root eligible for canvas rendering

Each drawable element is its own paint root with a drawable subtree: the element plus descendants, excluding nested descendants that also have drawable. That lets a drawable card and a nested drawable button upload or draw on separate schedules—useful for layered 3D UI.

Browser support is not universal. Detect the API before relying on it (Chrome documents support from Chrome 157 in several guides):

if ('content' in HTMLCanvasElement.prototype) {
// HTML-in-Canvas available
} else {
// Fallback — see below
}

paint event and requestPaint(): when the canvas redraws HTML

Section titled “paint event and requestPaint(): when the canvas redraws HTML”

The paint event fires on the <canvas> when layout, text, or styles change on any drawable descendant. Handle drawing in canvas.onpaint or addEventListener('paint', …).

The HTML-in-Canvas API introduces the paint event. The browser automatically dispatches a paint event on the <canvas> whenever layout, text, or CSS styles change on any of its drawable descendants.

— Chrome, HTML-in-Canvas: Technical concepts

CanvasPaintEvent exposes changedElements: drawable nodes that visually changed since the last paint—useful in WebGL loops to re-upload only dirty textures (Apply WebGL shaders to HTML content).

Frame lifecycle: drawing inside paint appears in the current frame; DOM style changes inside paint appear on the next frame. For changes that must affect the current frame, update styles in requestAnimationFrame before paint (Technical concepts — Frame lifecycle).

Call canvas.requestPaint() to schedule a paint even when DOM styles did not change—typical for animation driven outside DOM updates (Technical concepts — Trigger a paint event).

Nested <canvas content="drawable"> elements receive paint events in reverse DOM order (child canvas first, parent second) so compositing sees fresh child pixels (Technical concepts — Nested canvases).

2D canvas: drawElementImage() and automatic geometry

Section titled “2D canvas: drawElementImage() and automatic geometry”

In a 2D context, render drawable nodes with ctx.drawElementImage(element, dx, dy) and variants for scale and crop—see Render HTML into 2D canvas.

In 2D canvas contexts, the browser automatically synchronizes the element’s layout coordinates, accessibility bounds, and pointer hit-testing with the drawn canvas coordinates.

— Chrome, Render HTML into 2D canvas

Pass { preserveElementGeometry: true } to drawElementImage() when you want manual control; then call canvas.updateElementGeometry() with a DOMMatrix in CSS pixels (Render HTML into 2D canvas — Manually update element geometry).

Export workflows combine onpaint, drawElementImage, and canvas.toDataURL() (Export HTML content from canvas).

WebGL and WebGPU: textures, upload, and updateElementGeometry()

Section titled “WebGL and WebGPU: textures, upload, and updateElementGeometry()”

In 3D contexts the browser cannot infer where a texture-mapped quad sits on screen. You upload DOM snapshots to GPU memory and synchronise geometry so clicks, text selection, and accessibility bounds match the shader-transformed mesh.

WebGL: texImage2D and texElementSubImage2D

Section titled “WebGL: texImage2D and texElementSubImage2D”

Workflow from HTML in WebGL canvas and Apply WebGL shaders to HTML content:

  1. Pre-allocate with gl.texImage2D(..., null) sized from canvas.getElementImageDefaultSize(element) or captureElementImage() (round with Math.ceil()).
  2. On each paint, upload with gl.texElementSubImage2D()—only after the element has a paint record; if captureElementImage() throws, call canvas.requestPaint() and retry next frame.
  3. Reallocate level 0 only when size changes; prefer mutable texImage2D over immutable texStorage2D when sizes can change.

In WebGPU, copy snapshots with device.queue.drawElementImageToTexture() into a texture created with COPY_DST and RENDER_ATTACHMENT usage—details and dictionary fields are in HTML in WebGPU canvas.

updateElementGeometry(): hit testing and accessibility in 3D

Section titled “updateElementGeometry(): hit testing and accessibility in 3D”

Pass a canvasTransform (DOMMatrix mapping the element’s border box in CSS pixels to the canvas) on every frame where the 3D transform changes:

Then pass the transform to canvas.updateElementGeometry() as canvasTransform. This moves the HTML element to sit directly on top of its rendered pixels. This ensures that when a user clicks a button or selects text, they are actually hitting the real HTML element. The browser also uses the transform for the element’s accessibility bounds, for example, for screen reader focus rings.

— Chrome, Apply WebGL shaders to HTML content

Option Behavior
Default (preserveHitTestOrder: false) Each geometry update moves the element to the top of the hit-test stack
preserveHitTestOrder: true Keeps stack order—use for backgrounds updated after foreground UI
canvasClip Limits hit-testable region in transform space
canvas.clearElementGeometry(element) Removes element from hit testing; marks offscreen for accessibility

For animated WebGL scenes, call canvas.requestPaint() in your requestAnimationFrame loop and re-upload only when event.changedElements is non-empty (Apply WebGL shaders to HTML content).

ResizeObserver and pixel grids: sharp HTML inside canvas

Section titled “ResizeObserver and pixel grids: sharp HTML inside canvas”

Drawable HTML lays out in CSS pixels; the canvas grid is canvas.width × canvas.height. Misaligned grids cause double resampling and blur (Technical concepts — Sizing and units).

Size the grid to match device pixels with ResizeObserver and, when available, devicePixelContentBoxSize:

const observer = new ResizeObserver(([entry]) => {
const dpc = entry.devicePixelContentBoxSize;
canvas.width = dpc
? dpc[0].inlineSize
: Math.round(entry.contentRect.width * window.devicePixelRatio);
canvas.height = dpc
? dpc[0].blockSize
: Math.round(entry.contentRect.height * window.devicePixelRatio);
});

Convert CSS measurements to canvas grid coordinates by tracking scaleX = canvas.width / cssContentWidth and scaleY = canvas.height / cssContentHeight in the same observer (Technical concepts — Convert CSS pixels to canvas grid coordinates).

Accessibility: geometry must exist before assistive tools can place content

Section titled “Accessibility: geometry must exist before assistive tools can place content”

Drawable DOM inside content="drawable" generally appears in the accessibility tree like normal HTML unless hidden with aria-hidden or inert. An exception applies when rendered geometry is missing: the subtree may be exposed without geometry until you draw or call updateElementGeometry().

A drawable element inside a <canvas content=“drawable”> lacks rendered geometry if: It has not yet been rendered with drawElementImage()… Its geometry has not been explicitly updated with updateElementGeometry().

— Chrome, HTML-in-Canvas: Technical concepts

For interactive canvas UIs, include focusable fallback or real focus targets; for focus rings on custom canvas UI, WHATWG recommends drawFocusIfNeeded() when drawing focusable canvas content (canvas best practices).

Three.js, PlayCanvas, and fallbacks when HTML-in-Canvas is missing

Section titled “Three.js, PlayCanvas, and fallbacks when HTML-in-Canvas is missing”

Libraries can wrap the same primitives: THREE.HTMLTexture plus InteractionManager (HTML-in-Canvas overview — Three.js), PlayCanvas HTML textures, Babylon.js HtmlTexture, and PixiJS HTMLSource—each documented on the overview page. These are community integrations, not browser standards.

When the API is absent:

Prefer native HTML-in-Canvas when supported for performance and fidelity (fallback strategies).

  • Omitting drawable on direct canvas children or on elements drawn independently.
  • Uploading WebGL textures before the first paint record exists.
  • Forgetting updateElementGeometry() in WebGL/WebGPU while the mesh moves—clicks stick to old positions (Technical concepts — Update an element’s geometry).
  • Using canvas grid units where APIs expect CSS pixels (geometry) or the reverse (drawing coordinates)—see the units table in Technical concepts.
  • Rebuilding text editors on raw canvas instead of using <input>, <textarea>, contenteditable, or drawable DOM (WHATWG best practices).
  1. In a supporting browser, open your page and confirm 'content' in HTMLCanvasElement.prototype matches your intended code path (Technical concepts — Fallback strategies).
  2. Inspect the <canvas>: it has content="drawable" and each painted subtree root has drawable, as in Prepare your canvas to render HTML.
  3. Change styles or text on a drawable node and verify the canvas receives a paint event and redraws (Understand the paint event).
  4. For WebGL or WebGPU, move the mesh and confirm buttons and text selection still hit the DOM (geometry sync via Apply WebGL shaders to HTML content).
  5. Zoom the page or resize the window and check that HTML drawn into the canvas stays sharp after ResizeObserver updates the grid (Size the canvas grid with a resize observer).

You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will correct or clarify it.