SnapDOM vs dom-to-image
The original dom-to-image API against SnapDOM: rendering approach, plugin exports and the return types that change in a migration.
dom-to-image captures the DOM through SVG foreignObject. SnapDOM takes the same approach and adds open Shadow DOM, CSS counters and a plugin lifecycle.
SnapDOM vs dom-to-image, head to head
Each library exports the same element to PNG five times. The average includes fresh and repeated calls; SnapDOM can reuse the unchanged capture. The gap depends on your hardware, content and capture pattern.
A quick overview
dom-to-image clones a node, inlines CSS and assets, and wraps it in a <foreignObject> for the browser to render. It returns image data URLs or a PNG Blob. Its repository lists current releases and supported browsers.
SnapDOM is a from-scratch implementation of the same idea, actively developed, with a plugin system on top. Lifecycle hooks (beforeClone, afterClone, beforeRender, afterRender, beforeExport, afterExport) let you alter capture state, observe exports and add custom exporters without forking.
Feature matrix
| Feature | SnapDOM | dom-to-image |
|---|---|---|
| Release history | See repository | See repository |
Pseudo-elements (::before / ::after) | Supported | Passes tested fixture |
Web fonts (@font-face) | Embedded | Embedded |
| Shadow DOM | Open roots | No |
| CSS variables | Yes | Partial |
CSS counter() / counters() | Yes | Depends on the case |
| Line-clamp / multi-line ellipsis | Yes | Depends on the case |
| Plugin system | Yes | No |
| Output formats | SVG · PNG · JPG · WebP · Canvas · Blob | SVG · PNG · JPG · Blob |
| ES Module build | Yes | CommonJS |
| TypeScript types | Bundled | DefinitelyTyped |
| License | MIT | MIT |
Support varies by fixture and rendering mode; test your own UI. Fixtures and loaded versions are in the live benchmark.
When to use which
Choose SnapDOM when…
- You need fast captures, including repeats of the same element (live benchmark).
- Your output depends on browser-rendered CSS, fonts or open Shadow DOM; test those elements in your target browsers.
- You need PDF, editable vector, HTML or agent context, through plugins or your own.
Check before migrating…
- Capture your layouts in each browser you support.
- Test web fonts, open Shadow DOM, CSS counters and line-clamp with your content.
- CommonJS-era packaging and no bundled TypeScript types.
Same task, side by side
Capture #card as a PNG and add the image to the page:
dom-to-image
import domtoimage from 'dom-to-image'; const dataUrl = await domtoimage.toPng(document.querySelector('#card')); const img = new Image(); img.src = dataUrl; document.body.appendChild(img);
SnapDOM
import { snapdom } from '@zumer/snapdom'; const img = await snapdom.toPng(document.querySelector('#card')); document.body.appendChild(img);
SnapDOM returns the HTMLImageElement directly, so the data-URL plumbing goes away.
Migration snippet
Match both output format and return type. dom-to-image filter keeps nodes when true; SnapDOM exclude removes them when true. SnapDOM width and height size the output; they do not reflow the source:
| dom-to-image | SnapDOM |
|---|---|
| domtoimage.toPng(el) | (await snapdom.toPng(el)).src |
| domtoimage.toJpeg(el, { quality: 0.9 }) | (await snapdom.toJpg(el, { quality: 0.9 })).src |
| domtoimage.toBlob(el) | snapdom.toBlob(el, { format: 'png' }) |
| domtoimage.toSvg(el) | snapdom.toRaw(el) |
| { bgcolor: '#fff' } | { backgroundColor: '#fff' } |
| { width, height } | { width, height } (output size) |
| { filter: keep } | { exclude: node => !keep(node), excludeMode: 'remove' } |
| — (not supported) | { embedFonts: 'auto' } (default) |
| — (not supported) | { plugins: [...] } |
// Before import domtoimage from 'dom-to-image'; const dataUrl = await domtoimage.toPng(el, { bgcolor: '#fff' }); // After import { snapdom } from '@zumer/snapdom'; const img = await snapdom.toPng(el, { backgroundColor: '#fff' });
Frequently asked questions
Is dom-to-image still maintained?
Check the dom-to-image repository for its release history and open issues. Before migrating, compare your captured output and the return-type and option differences above.
How do I migrate from dom-to-image to SnapDOM?
Import @zumer/snapdom and start with snapdom.toPng(element). Read .src on the returned image if the caller expects a PNG data URL. Rename bgcolor to backgroundColor, invert inclusion predicates for exclude, and review output sizing.
Does SnapDOM support custom fonts?
Yes. In v3, embedFonts: 'auto' embeds used web fonts by default. Font files must be accessible; localFonts and excludeFonts add control.
What about Shadow DOM?
SnapDOM walks and clones open Shadow DOM trees, so Web Components capture correctly. dom-to-image does not natively support this.