Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Best DOM to Image Alternatives for Screenshots

· 5 min read · Home Tech

In this article
  1. What to Look For
  2. html-to-image
  3. html2canvas
  4. dom-to-image
  5. modern-screenshot
  6. dom-to-image-more
  7. Puppeteer (for server-side)
  8. How to Choose
  9. Frequently Asked Questions
  10. Bottom Line

The top pick for capturing a DOM element as an image is html-to-image. It is actively maintained, supports modern SVG and CSS features, and offers a simple promise-based API, making it the most reliable choice for most projects. Alternatives like html2canvas remain popular for legacy browser support, while dom-to-image is a lightweight option if you only need basic functionality.

What to Look For

When evaluating DOM-to-image libraries, focus on these practical criteria:

  • Rendering fidelity: How accurately does it reproduce CSS (flexbox, grid, gradients, shadows, filters, pseudo-elements)? Some libraries struggle with modern layout or require manual hacks.
  • SVG support: Does it handle inline SVG, external images, and foreignObject correctly? This is a common pain point.
  • Performance: How fast is it on large or complex DOM trees? Does it cause layout thrashing or memory spikes?
  • Output formats: PNG, JPEG, and WebP are standard. Some tools also offer PDF or canvas output.
  • API and flexibility: Does it support custom filters, pixel ratio, and background color? Is it promise-based or callback-based?
  • Maintenance and community: Is the project actively updated? Check open issues for known bugs (e.g., CORS, CSS variables).
  • Bundle size: If you care about load time, check the minified size. Some libraries are heavier than others.

html-to-image

Strengths: Modern and actively maintained. Excellent support for CSS features like flexbox, grid, gradients, and box-shadow. Handles SVG and foreignObject well. Offers toPng, toJpeg, toBlob, and toCanvas methods. Allows custom pixel ratio and style overrides. Small footprint (~10 KB gzipped).

Trade-offs: Requires a browser that supports foreignObject (all modern browsers do). Can struggle with cross-origin images unless you set CORS headers. Older IE is not supported.

Who it suits: Most developers building for modern browsers. Ideal for React, Vue, or vanilla JS projects that need reliable, high-fidelity screenshots.

html2canvas

Strengths: The most established library, with a large community and extensive documentation. Works in older browsers (IE9+). Supports many CSS properties, including pseudo-elements and some modern layout features. Offers options like scale, backgroundColor, and logging.

Trade-offs: Heavier (~30 KB gzipped). Rendering can be inaccurate for complex CSS like flexbox gaps, CSS variables, or mix-blend-mode. Performance degrades on large DOMs. Known issues with cross-origin images and some SVG edge cases.

Who it suits: Projects that need legacy browser support or rely on a battle-tested library with a huge community. Also good if you are already using it and don't want to migrate.

dom-to-image

Strengths: Extremely lightweight (~5 KB gzipped). Simple API: toPng, toJpeg, toBlob. Works well for basic captures with simple CSS. No dependencies.

Trade-offs: Not actively maintained (last release years ago). Poor support for modern CSS like flexbox, grid, and CSS variables. Fails on many SVG and image elements. Lacks options like pixel ratio or style overrides.

Who it suits: Small projects that need a quick, minimal screenshot tool for simple, static layouts. Not recommended for production apps with complex UI.

modern-screenshot

Strengths: A newer library that focuses on accuracy and performance. Uses foreignObject for rendering, similar to html-to-image, but with additional features like removeCloneNode and onCloneNode for fine-grained control. Supports WebP output. Good TypeScript definitions.

Trade-offs: Smaller community and less battle-testing. May have undocumented edge cases. Requires modern browsers.

Who it suits: Developers who want a fresh, well-designed API and are willing to accept a smaller ecosystem for better performance.

dom-to-image-more

Strengths: A fork of dom-to-image that fixes many bugs and adds support for modern CSS, SVG, and web fonts. Actively maintained. Offers similar lightweight API with improvements.

Trade-offs: Still not as feature-rich as html-to-image. May have occasional regressions. Smaller community than html2canvas.

Who it suits: Teams that already use dom-to-image and need a drop-in replacement with better fidelity, without migrating to a new API.

Puppeteer (for server-side)

Strengths: Not a library but a headless Chrome automation tool. Can take screenshots of entire pages or elements with pixel-perfect accuracy, including WebGL and complex animations. Offers full control via CDP.

Trade-offs: Requires a Node.js server and significant memory/CPU. Overkill for client-side captures. Slower due to browser startup.

Who it suits: Server-side generation of screenshots (e.g., for social cards, PDFs) where you need exact rendering and can afford the overhead.

How to Choose

Start with your budget (time and money). If you need a free, open-source solution, html-to-image is the best default. If you have legacy browser requirements, html2canvas is the safe bet. For server-side generation, Puppeteer is worth the setup cost.

Consider your use case: For client-side screenshots of a widget or chart, html-to-image is fast and accurate. For full-page captures with complex CSS, you may need to test multiple libraries. For high-volume server-side rendering, Puppeteer is the only reliable choice.

Common trade-offs: accuracy vs. size (html2canvas is larger but more compatible), speed vs. features (dom-to-image is fast but limited), and maintenance vs. community (html-to-image is modern but smaller community than html2canvas).

Always test with your actual DOM, including images, fonts, and CSS variables. Use the library’s demo page or a local test to compare output.

Frequently Asked Questions

Why do my images appear blank or broken in the screenshot?

This is usually a CORS issue. Ensure the images are served with Access-Control-Allow-Origin headers, or use crossOrigin="anonymous" on the img elements. Some libraries also require you to pass a useCORS option.

Can I capture a specific element, not the whole page?

Yes, all the libraries mentioned accept a DOM node as the first argument. Pass the element you want to capture, and it will render only that node.

How do I handle WebP or JPEG output?

Most libraries offer methods for different formats. For example, html-to-image has toJpeg and toWebp. If not, you can convert the canvas to the desired format using canvas.toDataURL().

Does it work with React or Vue?

Yes, these libraries are framework-agnostic. You can call them in event handlers or effects. Be aware that some libraries may have issues with virtual DOM updates, so capture after the DOM is stable.

Why is the screenshot blurry on retina displays?

Set the pixelRatio option to window.devicePixelRatio or a higher value (e.g., 2) to capture at higher resolution. Most libraries support this via options.

Can I capture a page that uses CSS animations?

Yes, but the screenshot will capture the current state. If you need to pause animations, you can temporarily set animation-play-state: paused on the element before capturing.

Bottom Line

For most projects, html-to-image is the best choice: it is modern, accurate, and actively maintained. If you need legacy browser support, fall back to html2canvas. For server-side rendering, use Puppeteer. Avoid dom-to-image unless you have a very simple use case and want minimal dependencies. Always test with your actual content to ensure fidelity.