Taking a screenshot of a DOM element in the browser sounds simple. In practice, it involves re-implementing a significant subset of CSS rendering in JavaScript — and the three main libraries that do this have meaningfully different trade-offs.
This post compares html2canvas, dom-to-image (and its fork dom-to-image-more), and html-to-image — based on feature support, accuracy, performance, and bundle size.
The Core Challenge
Browsers don't expose a native "capture this element as an image" API for arbitrary DOM content. The html2canvas approach involves:
Walking the DOM tree
Reading computed styles for each element
Re-drawing each element onto an HTML5 Canvas
Exporting the canvas as a PNG or JPEG
The alternative approach (used by dom-to-image and html-to-image) uses a different trick:
Clone the DOM subtree
Inline all styles and resources
Serialize the clone to an SVG using a <foreignObject> element
Render the SVG to a Canvas using an Image element
Each approach has different failure…