dom-to-image, html-to-image, and html2canvas are client-side JavaScript libraries that enable rendering of DOM elements as image files (PNG, JPEG, etc.) directly in the browser. These tools are commonly used for generating screenshots, exporting visual reports, creating shareable content, or capturing UI states without server involvement. All three work by traversing the DOM, interpreting styles and layout, and producing a rasterized image output using canvas or SVG-based techniques.
When you need to turn part of a web page into an image — whether for sharing, exporting, or archiving — these three libraries offer client-side solutions. But they differ significantly in architecture, capabilities, and maintenance status. Let’s break down what each does well and where they fall short.
dom-to-image works by cloning the target element, converting all styles to inline attributes, serializing it to an SVG foreignObject, and then drawing that SVG onto a canvas. This approach is simple but fragile when dealing with modern CSS.
// dom-to-image
import domtoimage from 'dom-to-image';
domtoimage.toPng(document.getElementById('capture'))
.then(dataUrl => {
const img = new Image();
img.src = dataUrl;
document.body.appendChild(img);
});
html-to-image uses the same underlying technique as dom-to-image but improves the style parsing and serialization logic. It’s essentially a drop-in replacement with better CSS handling.
// html-to-image
import { toPng } from 'html-to-image';
toPng(document.getElementById('capture'))
.then(dataUrl => {
const img = new Image();
img.src = dataUrl;
document.body.appendChild(img);
});
html2canvas takes a completely different approach: it walks the DOM tree, computes the rendered styles using getComputedStyle(), and manually redraws every element onto a canvas using the Canvas 2D API. This gives it far greater control over rendering accuracy.
// html2canvas
import html2canvas from 'html2canvas';
html2canvas(document.getElementById('capture')).then(canvas => {
document.body.appendChild(canvas);
});
If your UI uses modern layout techniques, this is where the libraries diverge sharply.
dom-to-image struggles with:
::before, ::after)It doesn’t attempt to interpret computed styles deeply — it just copies declared styles, which breaks when layout depends on parent-child relationships.
html-to-image improves support for:
transform and opacityfontEmbedCSS optionHowever, it still relies on SVG serialization, so complex interactions (like z-index stacking contexts) can fail.
// html-to-image with custom font embedding
import { toPng } from 'html-to-image';
toPng(node, {
fontEmbedCSS: '@import url("https://fonts.googleapis.com/css2?family=Roboto");'
});
html2canvas excels at:
z-index and stacking contextsignoreElements configuration)useCORS: true and proper font hosting)// html2canvas with CORS and logging
html2canvas(node, {
useCORS: true,
logging: false,
allowTaint: false
});
⚠️ Note:
html2canvascannot render iframes, plugins, or video elements — none of these libraries can, due to browser security restrictions.
dom-to-image produces decent quality for simple layouts but often fails silently on complex ones. It’s fast because it offloads rendering to the browser’s SVG engine, but that also means less control.
html-to-image offers similar performance to dom-to-image but with fewer visual glitches thanks to better preprocessing. Still limited by SVG’s inability to perfectly mimic HTML rendering.
html2canvas is slower — especially on large DOM trees — because it manually draws every pixel. But you get pixel-perfect output that matches what the user sees, including subtle effects like text antialiasing and blend modes.
For high-DPI (retina) displays, html2canvas supports scaling:
// html2canvas with scale for high DPI
html2canvas(node, {
scale: window.devicePixelRatio
});
Neither dom-to-image nor html-to-image handle device pixel ratio automatically — you’d need to scale the canvas yourself post-render.
All three libraries face the same fundamental limitation: they cannot render images or fonts from external domains unless those resources are served with CORS headers.
html2canvas provides explicit options to handle this:
useCORS: true — attempts to load cross-origin images if the server allows itallowTaint: false (default) — prevents tainted canvases that would block toDataURL()// Safe html2canvas config for mixed content
html2canvas(node, {
useCORS: true,
allowTaint: false
});
dom-to-image and html-to-image embed images as base64 data URLs during serialization. If an image is cross-origin and lacks CORS headers, it will either be omitted or cause the entire render to fail — with little feedback.
dom-to-image is effectively deprecated. Its GitHub repository shows no meaningful updates since 2018, and the npm page includes community warnings about its limitations. Do not use it in new projects.
html-to-image is actively maintained as of 2024, with regular bug fixes and improvements to CSS compatibility. It’s the spiritual successor to dom-to-image.
html2canvas remains under active development, with frequent releases addressing edge cases in modern browsers. It has the largest test suite and best documentation of the three.
dom-to-image: ~15 KB minified (no dependencies)html-to-image: ~20 KB minified (no dependencies)html2canvas: ~90 KB minified (larger due to comprehensive rendering logic)If you’re building a lightweight utility (e.g., a browser extension), html-to-image offers the best balance. For mission-critical applications where visual fidelity matters (e.g., financial dashboards, design tools), html2canvas is worth the extra weight.
If you’re currently using dom-to-image, migrating to html-to-image is nearly seamless:
// Before (dom-to-image)
import domtoimage from 'dom-to-image';
domtoimage.toPng(node);
// After (html-to-image)
import { toPng } from 'html-to-image';
toPng(node);
Switching to html2canvas requires more refactoring since it returns a HTMLCanvasElement instead of a data URL, but gives you more control:
// Convert html2canvas output to data URL if needed
html2canvas(node).then(canvas => {
const dataUrl = canvas.toDataURL('image/png');
});
| Scenario | Recommended Library |
|---|---|
| Simple static content (text, basic boxes) | html-to-image |
| Modern layouts (flexbox, grid, transforms) | html2canvas |
| High-fidelity rendering (reports, dashboards) | html2canvas |
| Lightweight bundle requirement | html-to-image |
Legacy project already using dom-to-image | Keep it (but plan migration) |
| New project with complex UI | html2canvas |
Start with html-to-image if your use case is straightforward — it’s small, fast, and good enough for most marketing pages, forms, or simple cards. Switch to html2canvas when you notice missing styles, layout shifts, or font issues. And avoid dom-to-image entirely unless you’re maintaining old code that hasn’t broken yet.
Remember: no client-side library can perfectly replicate browser rendering. Always test with real-world content, especially if your app uses dynamic theming, custom fonts, or responsive layouts.
Choose html2canvas if you require the most comprehensive rendering fidelity, including support for complex CSS properties, web fonts, pseudo-elements, and cross-origin images (with proper CORS setup). It uses a more sophisticated rendering engine that mimics browser painting behavior more closely, though at the cost of larger bundle size and potentially slower performance on large DOM trees.
Choose html-to-image if you want a modern, actively maintained fork of dom-to-image with improved support for contemporary CSS layouts (including flexbox and grid), better font handling, and ongoing bug fixes. It retains the same core API but addresses many of the original package’s shortcomings, making it a solid choice for most new browser-based image capture needs.
Choose dom-to-image if you're working on a legacy project that already uses it or if you need a lightweight solution with minimal dependencies. However, note that this package is no longer actively maintained and has known limitations with modern CSS features like flexbox, grid, and certain transforms. Avoid it for new projects unless you have specific compatibility requirements.
Homepage | Downloads | Questions
The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.
The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements.
It does not require any rendering from the server, as the whole image is created on the client's browser. However, as it is heavily dependent on the browser, this library is not suitable to be used in nodejs. It doesn't magically circumvent any browser content policy restrictions either, so rendering cross-origin content will require a proxy to get the content to the same origin.
The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made.
The library should work fine on the following browsers (with Promise polyfill):
As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported.
The html2canvas library utilizes Promises and expects them to be available in the global context. If you wish to
support older browsers that do not natively support Promises, please include a polyfill such as
es6-promise before including html2canvas.
To render an element with html2canvas, simply call:
html2canvas(element[, options]);
The function returns a Promise containing the <canvas> element. Simply add a promise fulfillment handler to the promise using then:
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
});
You can download ready builds here.
Clone git repository:
$ git clone git://github.com/niklasvh/html2canvas.git
Install dependencies:
$ npm install
Build browser bundle
$ npm run build
For more information and examples, please visit the homepage or try the test console.
If you wish to contribute to the project, please send the pull requests to the develop branch. Before submitting any changes, try and test that the changes work with all the support browsers. If some CSS property isn't supported or is incomplete, please create appropriate tests for it as well before submitting any code changes.