HTML-in-Canvas
The HTML-in-Canvas API (drawElementImage) lets you capture live, rendered DOM elements directly into a canvas at GPU speed. This means you can take any HTML — dashboards, forms, landing pages, app UIs — and render them as textures in WebGL scenes with shaders, 3D transformations, and post-processing effects.
How it works
- Place HTML content inside a
<canvas layoutsubtree>element - The browser renders the HTML children as normal DOM
- Call
ctx.drawElementImage(element, x, y, w, h)to capture the rendered pixels into the canvas - Use the canvas as a Three.js texture, apply shaders, map to 3D geometry
What makes this different
Traditional approaches likehtml2canvas re-parse and re-render the DOM in JavaScript — they’re slow, lossy, and miss CSS features like backdrop-filter, complex shadows, and web fonts. The drawElementImage API uses the browser’s own compositor, so:
- Pixel-perfect — every CSS feature is supported because the browser renders it natively
- GPU-accelerated — captures at 60fps, fast enough for real-time animation
- Live content — the HTML can animate, scroll, and change between captures
- Multiple captures simultaneously — no nesting restrictions, multiple
<canvas layoutsubtree>elements can capture different content in the same composition
Feature detection
Always feature-detect before using the API. Compositions should fall back gracefully for browsers without the flag enabled.Re-capturing every frame
For animated content (scrolling, transitions, counters), calldrawElementImage inside your render loop to update the texture every frame: