html2canvas and modern-screenshot render DOM elements to images directly in the browser, enabling client-side features like user-generated previews. puppeteer controls a headless Chrome instance from Node.js to capture full pages or specific elements server-side, ideal for automation and testing. screenshot-desktop captures the actual operating system screen buffer, used primarily in Electron apps or Node scripts needing monitor-level access.
Choosing the right tool for screenshots depends on where your code runs and what exactly you need to capture. html2canvas and modern-screenshot work inside the browser to turn DOM nodes into images. puppeteer runs on the server to control a real browser engine. screenshot-desktop bypasses the browser entirely to grab the operating system's screen buffer. Let's break down how they handle real-world tasks.
html2canvas runs entirely in the user's browser.
// html2canvas: Client-side execution
import html2canvas from 'html2canvas';
const element = document.getElementById('profile-card');
html2canvas(element).then((canvas) => {
document.body.appendChild(canvas);
});
modern-screenshot also runs in the browser.
html2canvas, it executes on the client without server dependencies.// modern-screenshot: Client-side execution
import { domToPng } from 'modern-screenshot';
const node = document.getElementById('app-root');
domToPng(node).then((dataUrl) => {
const link = document.createElement('a');
link.download = 'screenshot.png';
link.href = dataUrl;
link.click();
});
puppeteer runs in Node.js on the server or local machine.
// puppeteer: Server-side execution
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
screenshot-desktop runs in Node.js or Electron.
// screenshot-desktop: OS-level execution
const screenshot = require('screenshot-desktop');
screenshot().then((img) => {
// img is a Buffer containing the screen capture
require('fs').writeFileSync('screen.png', img);
});
html2canvas reimplements CSS rendering in JavaScript.
// html2canvas: Handling CORS
html2canvas(element, {
useCORS: true
}).then((canvas) => {
// Handle canvas
});
modern-screenshot aims to improve on DOM rendering limits.
// modern-screenshot: Options for fidelity
domToPng(node, {
features: {
debug: false
}
}).then((dataUrl) => {
// Handle data URL
});
puppeteer uses the actual Chrome rendering engine.
// puppeteer: Full page capture
await page.screenshot({
path: 'full.png',
fullPage: true
});
screenshot-desktop captures raw pixels from the GPU.
// screenshot-desktop: Specific screen selection
screenshot({ screen: 0 }).then((img) => {
// Capture primary monitor only
});
You want users to download an image of their customized profile card.
html2canvas or modern-screenshot// Client-side download
html2canvas(document.querySelector('#preview')).then((canvas) => {
const link = document.createElement('a');
link.download = 'my-profile.png';
link.href = canvas.toDataURL();
link.click();
});
You need to verify that your production site looks correct after deployment.
puppeteer// CI/CD Screenshot
await page.screenshot({ path: './baseline/homepage.png' });
You are building an Electron app and need to capture the user's workspace for a bug report.
screenshot-desktop// Electron main process
const img = await screenshot({ format: 'png' });
You need to generate a downloadable PDF of a long dashboard.
puppeteer// PDF Generation
await page.pdf({ path: 'report.pdf', format: 'A4' });
| Feature | html2canvas | modern-screenshot | puppeteer | screenshot-desktop |
|---|---|---|---|---|
| Environment | Browser | Browser | Node.js | Node.js / Electron |
| Target | DOM Element | DOM Element | URL / Page | Monitor Screen |
| CSS Support | Good (JS reimplementation) | Better (Modern focus) | Perfect (Chrome Engine) | None (Raw Pixels) |
| Server Required | No | No | Yes | Yes |
| Cross-Origin | Strict (Needs CORS) | Strict (Needs CORS) | None (Server fetches) | N/A |
html2canvas is the safe bet for client-side DOM capture. It has a large community and predictable behavior for standard HTML. Use it for user-facing features where server load is a concern.
modern-screenshot is worth testing if html2canvas fails on specific CSS features. It serves the same role but may handle modern styles better.
puppeteer is the professional standard for server-side automation. Use it for testing, PDFs, or when you need pixel-perfect accuracy that only a real browser engine can provide.
screenshot-desktop is a niche tool for Electron or Node utilities. Use it only when you need to capture the actual monitor output, such as for support tools or desktop automation.
Final Thought: Match the tool to the environment. If the code runs in the browser, pick html2canvas or modern-screenshot. If it runs on the server, pick puppeteer. If you need the OS screen, pick screenshot-desktop.
Choose html2canvas when you need a stable, widely-supported solution for converting HTML elements to images directly in the user's browser. It is the standard choice for features like 'save profile as image' or generating previews without server involvement, though it may struggle with complex CSS.
Choose modern-screenshot if you need a client-side DOM renderer that aims for better performance or newer CSS support than html2canvas. It fits the same use cases as html2canvas but may offer improvements in rendering speed or fidelity for modern browsers.
Choose puppeteer when you need high-fidelity screenshots from a Node.js environment, such as generating PDF reports, visual regression testing, or capturing full-page scrolls that client-side tools cannot handle reliably.
Choose screenshot-desktop only when you need to capture the user's entire monitor or specific screen regions from a Node.js or Electron application. It is not for DOM rendering but for OS-level screen capture tasks.
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.