Render HTML to Canvas

There are two ways to render HTML into a <canvas>: a DOM-screenshotting library like html2canvas, which re-implements CSS rendering in JavaScript, or the native drawElementImage() API from the WICG HTML-in-Canvas spec, which has the browser paint real, live DOM elements directly into the canvas.

html2canvas vs drawElementImage() at a glance

html2canvas (library) drawElementImage() (native)
Fidelity Approximate. CSS is re-implemented in JS, so ligatures, subpixel text, some filters, and newer CSS features render differently or not at all The browser's own renderer paints the element, so output matches what the page shows, fonts and layout included
Performance Clones the DOM and rasterizes in JavaScript, typically tens to hundreds of milliseconds per capture on real pages Uses the browser's paint pipeline; fast enough to redraw every frame for animation and video capture
Live / interactive content One-shot static snapshot; re-capture for every change Continuous. The paint event fires when a child's rendering changes, and hit testing plus the accessibility tree keep working via layoutsubtree / content="drawable"
Browser support All modern browsers On by default in Chrome 157+; behind the canvas-draw-element flag in Chromium 147–156 and Brave; no Firefox or Safari implementation yet
Bundle size ~45 kB min+gzip shipped to every visitor 0 kB (built into the browser)

The html2canvas approach and its limits

html2canvas has been the standard answer for over a decade. It works in every browser, needs no flags, and captures almost any element on the page with one call:

import html2canvas from 'html2canvas';

const target = document.querySelector('#card');
const canvas = await html2canvas(target);
document.body.appendChild(canvas);
// or export it:
const png = canvas.toDataURL('image/png');

It walks the DOM, reads every computed style, and redraws the tree onto a canvas with 2D drawing commands. In effect it's a CSS renderer written in JavaScript, and that is where its limits come from:

The library exists because the platform had no API for this. The HTML-in-Canvas proposal adds one.

The native way: layoutsubtree + drawElementImage()

The WICG HTML-in-Canvas spec adds a small API surface to <canvas>: a layoutsubtree attribute that opts the canvas's children into layout (Chrome 157+ renamed it content="drawable" and also wants drawable on each drawn child, so set both until Brave catches up), a drawElementImage() context method that paints a child into the canvas, and a paint event that tells you when to redraw. A complete working example:

<!-- layoutsubtree: Chromium ≤156 (Brave Stable);
     content="drawable" + drawable: Chrome 157+. Set both. -->
<canvas id="c" layoutsubtree content="drawable">
  <div id="content" drawable>Hello</div>
</canvas>
<script>
  const ctx = c.getContext('2d');

  // The paint event fires whenever a child element's rendering
  // changes — redraw there, once per frame at most.
  c.onpaint = () => {
    ctx.reset();
    ctx.drawElementImage(content, 0, 0);
    // Chrome 155+ keeps hit testing and accessibility in line with
    // the drawn position. (Older builds returned a DOMMatrix that
    // you applied to content.style.transform.)
  };

  // Size the canvas grid to device pixels to prevent blurriness.
  new ResizeObserver(([e]) => {
    c.width = e.devicePixelContentBoxSize[0].inlineSize;
    c.height = e.devicePixelContentBoxSize[0].blockSize;
  }).observe(c, { box: 'device-pixel-content-box' });
</script>

Key behaviors to know:

Full signatures and IDL are in the API reference. The API is on by default in Chrome 157 and later. Older Chromium builds and Brave need chrome://flags/#canvas-draw-element; the browser support page has the steps.

Migrating from html2canvas

// One-shot export, html2canvas style: force a paint,
// draw, then read the pixels out.
c.addEventListener('paint', () => {
  ctx.reset();
  ctx.drawElementImage(content, 0, 0);
  c.toBlob((blob) => downloadOrUpload(blob), 'image/png');
}, { once: true });
c.requestPaint();

See it running

Three demos cover the common html2canvas use cases:

Next: browse the gallery of HTML canvas examples, read the spec overview for the full design, or check browser support to try the demos in your own browser.