html2canvas vs html-to-image vs dom-to-image-more
Capturing DOM Nodes as Images in Production
html2canvashtml-to-imagedom-to-image-moreSimilar Packages:

Capturing DOM Nodes as Images in Production

html2canvas, html-to-image, and dom-to-image-more are JavaScript libraries designed to capture screenshots of DOM elements directly in the browser. They are commonly used for generating reports, creating image previews, or saving user dashboards as files. While they share the same goal, they differ in rendering engines, CSS support, and API design. html2canvas is the original solution with a custom rendering engine. html-to-image and dom-to-image-more use a DOM cloning approach that often yields better fidelity with modern CSS.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
html2canvas26,377,56831,9233.38 MB1,054-MIT
html-to-image11,976,1037,274315 kB1862 years agoMIT
dom-to-image-more306,836682999 kB1a month agoMIT

Capturing DOM Nodes as Images in Production

All three libraries solve the same core problem β€” turning live HTML elements into raster images β€” but they work differently under the hood. This matters when you deal with complex layouts, external images, or modern CSS features. Let's compare how they handle rendering, API design, and real-world constraints.

🎨 Rendering Engine: Custom Parser vs DOM Clone

html2canvas uses a custom engine to parse CSS and draw directly to an HTML5 Canvas.

  • It does not rely on the browser's native rendering for the capture process.
  • This gives it control but means it might lag behind new CSS standards.
// html2canvas: Custom rendering engine
import html2canvas from 'html2canvas';

const canvas = await html2canvas(document.querySelector('#dashboard'), {
  backgroundColor: '#ffffff'
});
// Returns a HTMLCanvasElement

html-to-image clones the DOM node, applies styles, and serializes it to an SVG.

  • It relies on the browser to render the clone accurately.
  • Usually handles modern CSS (like flexbox and grid) better than html2canvas.
// html-to-image: DOM clone to SVG
import { toPng } from 'html-to-image';

const dataUrl = await toPng(document.querySelector('#dashboard'), {
  backgroundColor: '#ffffff'
});
// Returns a Promise<string> (data URL)

dom-to-image-more follows the same cloning approach as html-to-image.

  • It is a fork of the original dom-to-image library.
  • Inherits the SVG serialization method but includes community fixes.
// dom-to-image-more: DOM clone to SVG
import domtoimage from 'dom-to-image-more';

const dataUrl = await domtoimage.toPng(document.querySelector('#dashboard'), {
  bgcolor: '#ffffff'
});
// Returns a Promise<string> (data URL)

πŸ› οΈ API Design: Function vs Namespace

The way you call these libraries affects how easily you can integrate them into your codebase.

  • html2canvas exposes a single default function.
  • html-to-image exports named methods for different formats.
  • dom-to-image-more uses a namespace object with methods attached.
// html2canvas: Single default export
const canvas = await html2canvas(element);

// html-to-image: Named exports for formats
import { toPng, toJpeg, toBlob } from 'html-to-image';
const png = await toPng(element);
const jpeg = await toJpeg(element);

// dom-to-image-more: Namespace object
import domtoimage from 'dom-to-image-more';
const png = await domtoimage.toPng(element);
const jpeg = await domtoimage.toJpeg(element);

🧩 CSS Support & Fidelity

Visual accuracy is the most common pain point when generating images from DOM nodes.

html2canvas maintains its own CSS parser.

  • It supports most standard properties but can miss newer ones.
  • Complex shadows or specific blend modes might not render exactly as seen on screen.
// html2canvas: May require manual fixes for missing CSS
const canvas = await html2canvas(element, {
  ignoreElements: (node) => node.classList.contains('no-capture')
});

html-to-image and dom-to-image-more rely on browser rendering.

  • They clone the node and let the browser paint it before capturing.
  • This usually results in higher fidelity for modern layouts like CSS Grid.
// html-to-image: Better modern CSS support
const dataUrl = await toPng(element, {
  filter: (node) => !node.classList.contains('no-capture')
});

// dom-to-image-more: Similar filtering capability
const dataUrl = await domtoimage.toPng(element, {
  filter: (node) => !node.classList.contains('no-capture')
});

🌐 Handling External Resources and CORS

All three libraries face the same security restriction β€” the Canvas Taint Rule.

  • If an image lacks CORS headers, the canvas becomes tainted.
  • You cannot export a tainted canvas to an image or data URL.
// All packages: Must ensure images have CORS headers
// <img src="https://example.com/logo.png" crossOrigin="anonymous" />

// html2canvas example
await html2canvas(element, { useCORS: true });

// html-to-image example
await toPng(element, { cacheBust: true });

// dom-to-image-more example
await domtoimage.toPng(element, { cacheBust: true });

⚑ Performance on Large DOMs

Rendering complex trees can block the main thread and freeze the UI.

  • html2canvas can be slow on large pages because it parses every style rule.
  • html-to-image and dom-to-image-more may use less memory but still require deep cloning.
  • For very large DOMs, consider rendering only visible sections or using server-side solutions.
// Optimization pattern for all libraries
// Only capture the specific container, not the whole body
const element = document.querySelector('#report-section');

// Use scale option to reduce processing load
await html2canvas(element, { scale: 0.5 });
await toPng(element, { scale: 0.5 });
await domtoimage.toPng(element, { scale: 0.5 });

πŸ“Š Summary: Key Differences

Featurehtml2canvashtml-to-imagedom-to-image-more
RenderingπŸ”§ Custom CSS parserπŸ–ΌοΈ DOM clone to SVGπŸ–ΌοΈ DOM clone to SVG
API StyleπŸ“¦ Default functionπŸ“¦ Named exportsπŸ“¦ Namespace object
Return TypeπŸ–ΌοΈ HTMLCanvasElementπŸ“„ Data URL (string)πŸ“„ Data URL (string)
CSS Support⚠️ Good, misses some new CSSβœ… Better modern CSS supportβœ… Better modern CSS support
Background OptionbackgroundColorbackgroundColorbgcolor
Maintenanceβœ… Activeβœ… Activeβœ… Community Fork

πŸ’‘ The Big Picture

html2canvas is the reliable workhorse 🐴 β€” it has been around the longest and handles edge cases well. Use it for internal tools or when you need maximum stability over cutting-edge CSS support.

html-to-image is the modern choice πŸš€ β€” it offers a cleaner API and better visual fidelity for contemporary web designs. Use it for customer-facing features where looks matter most.

dom-to-image-more is the maintenance fix πŸ› οΈ β€” pick this if you are already using dom-to-image and need bug fixes without refactoring your entire integration layer.

Final Thought: All three libraries require careful handling of external images and CORS policies. For new projects, html-to-image often provides the best balance of ease-of-use and rendering quality β€” but test your specific layout before committing.

How to Choose: html2canvas vs html-to-image vs dom-to-image-more

  • html2canvas:

    Choose html2canvas if you need the most battle-tested solution with a large community and extensive documentation. It is ideal for projects that require stable behavior across older browsers and do not rely heavily on very recent CSS features like complex grids or filters.

  • html-to-image:

    Choose html-to-image if you need better support for modern CSS properties and a cleaner, promise-based API. It is suitable for applications that require high visual fidelity and need to export images as data URLs or blobs without heavy configuration.

  • dom-to-image-more:

    Choose dom-to-image-more if you are migrating from the original dom-to-image package and need bug fixes without changing your existing code structure. It is a community-maintained fork that keeps the original API alive while addressing critical issues found in the unmaintained predecessor.

README for html2canvas

html2canvas

Homepage | Downloads | Questions

Gitter CI NPM Downloads NPM Version

JavaScript HTML renderer

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.

How does it work?

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.

Browser compatibility

The library should work fine on the following browsers (with Promise polyfill):

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Safari 6+

As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported.

Usage

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);
});

Building

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

Examples

For more information and examples, please visit the homepage or try the test console.

Contributing

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.