HTML in Canvas API
An emerging web standard solving a long-standing developer headache: drawing live, interactive HTML elements natively into graphics like 2D Canvas or WebGL.
The Problem It Solves
Imagine you are building a browser-based 3D game using WebGL or a modern interactive canvas app. You want to display an in-game computer screen, or maybe just a settings menu. You quickly hit a wall:
HTML handles text, forms, scrolling, and accessibility perfectly. But you cannot directly draw an HTML <div> onto a <canvas>.
Instead, developers historically had to do one of two things:
- The CSS Overlay Hack: Floating HTML elements on top of the canvas using CSS
position: absolute;. (This doesn't work if the element needs to be inside a 3D world). - Reinventing the Wheel: Writing thousands of lines of complex JavaScript graphics code to manually draw text, fake input cursors, and mimic scrollbars entirely inside the canvas.
The Solution: HTML-in-Canvas
The HTML in Canvas API is an experimental proposal (incubated by the WICG) created exactly for this. It natively acts as a bridge between the DOM and your Graphics.
It allows the browser to take a live, high-speed snapshot of an HTML element and hand it directly to the canvas as an image or texture.
If that HTML updates (e.g., a video frame plays, text is selected, an input is typed into), the canvas gets notified immediately to redraw it!
The Proposed Primitives
The emerging proposal introduces three main mechanisms to function securely and efficiently:
1. The layoutsubtree Attribute
To capture an HTML element, the proposal requires adding a layoutsubtree attribute to the canvas. This acts as a security and performance requirement, creating a stacking context and isolating the layout of the canvas children so they can be captured without disrupting the entire page.
2. The Paint Event
Instead of manually requesting a redraw 60 times a second using a loop, the API introduces a paint event. This event fires only when the target HTML visually changes, saving immense processing power.
3. The Rasterization Methods
When the paint event happens, you use a special method to draw it onto your canvas. The proposed methods mirror existing canvas APIs:
- 2D Canvas: Uses
ctx.drawElementImage(element, x, y). - WebGL (3D Canvas): Uses
gl.texElementImage2D(...)to turn the element into a 3D texture. - WebGPU: Uses
copyElementImageToTexture().
Code Example (Proposed Syntax)
This snippet demonstrates what the proposed syntax looks like based on the living explainer.
If this API makes its way out of incubation and into modern browsers, rendering complex components into games, VR headsets, 3D scenes, or static image exports will become a native and blazing-fast part of the web. For reliable updates, follow the official WICG html-in-canvas living explainer.
chrome://flags/#canvas-draw-element.