html2canvas vs dom-to-image
Web Page Screenshot Libraries
html2canvasdom-to-imageSimilar Packages:
Web Page Screenshot Libraries

Web Page Screenshot Libraries are tools that allow developers to capture visual representations of web pages or specific elements within them. These libraries can generate images or PDFs of the rendered content, which can be useful for creating thumbnails, saving user-generated content, or implementing features like sharing and printing. They work by rendering the HTML and CSS of a page or element and converting it into a bitmap image. dom-to-image focuses on converting DOM nodes to images, while html2canvas captures the visual representation of a web page or element by rendering it in a canvas.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
html2canvas5,923,61531,6923.38 MB1,045-MIT
dom-to-image251,13310,745-3368 years agoMIT
Feature Comparison: html2canvas vs dom-to-image

Image Generation

  • html2canvas:

    html2canvas captures the visual representation of a web page or element by rendering it in a canvas. It captures the entire layout, including backgrounds, images, and text, but may have limitations with certain CSS properties.

  • dom-to-image:

    dom-to-image generates images from specific DOM elements, allowing for more control over what is captured. It supports various formats, including PNG and JPEG, and can handle SVGs, text, and styles.

CSS Support

  • html2canvas:

    html2canvas supports a wide range of CSS properties, including backgrounds, borders, and shadows. However, it may struggle with some advanced CSS features, such as filters, gradients, and animations.

  • dom-to-image:

    dom-to-image has good support for inline styles, background images, and SVGs. It can handle most CSS properties, but complex animations and certain pseudo-elements may not be captured accurately.

Customization

  • html2canvas:

    html2canvas offers limited customization options, primarily related to the rendering process. Developers can adjust the scale, use a proxy for cross-origin images, and set a background color, but overall customization is more limited compared to dom-to-image.

  • dom-to-image:

    dom-to-image allows for more customization, such as specifying the image format, quality, and dimensions. It also provides options for excluding certain elements or styles during the capture process.

Cross-Origin Support

  • html2canvas:

    html2canvas also supports cross-origin images, but it requires the images to have CORS enabled. If CORS is not configured, the library will not be able to access the images, leading to potential security issues.

  • dom-to-image:

    dom-to-image handles cross-origin images by using CORS (Cross-Origin Resource Sharing) headers. It requires the images to be served with the appropriate CORS headers to be captured accurately.

Ease of Use: Code Examples

  • html2canvas:

    html2canvas captures screenshots of web pages or elements. Here’s an example:

    import html2canvas from 'html2canvas';
    const element = document.getElementById('my-element');
    hl2canvas(element)
      .then((canvas) => {
        document.body.appendChild(canvas);
      })
      .catch((error) => {
        console.error('Error capturing screenshot:', error);
      });
    
  • dom-to-image:

    dom-to-image provides a simple API for converting DOM elements to images. Here’s a quick example:

    import { toPng } from 'dom-to-image';
    const node = document.getElementById('my-node');
    toPng(node)
      .then((dataUrl) => {
        const img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
      })
      .catch((error) => {
        console.error('Error generating image:', error);
      });
    
How to Choose: html2canvas vs dom-to-image
  • html2canvas:

    Choose html2canvas if you want to capture a screenshot of an entire web page or a large section, including complex layouts and backgrounds. It is more suitable for capturing the overall appearance of a page.

  • dom-to-image:

    Choose dom-to-image if you need to convert specific DOM elements into images, especially if you require support for SVGs, text, and styles. It is ideal for generating images from smaller, well-defined parts of a web page.

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.