SnapDOM Documentation
Capture a DOM element as an image. Plugins add PDF, editable vector and recordings. This reference covers SnapDOM 3.
Installation
npm i @zumer/snapdom@latest
Or use yarn add @zumer/snapdom@latest.
Quick start
Capture an element as a PNG image:
import { snapdom } from '@zumer/snapdom';
const img = await snapdom.toPng(document.querySelector('#card'));
document.body.appendChild(img);
Use snapdom(el) when you need several exports from the same captured state:
const result = await snapdom(document.querySelector('#card'));
await result.toPng(); // → HTMLImageElement
await result.toSvg(); // → SVG as Image
await result.download({ format: 'jpg', filename: 'card.jpg' });
Documentation sections
Browser and module builds
CDN
Choose the script tag for a global snapdom, or the module import:
<!-- Minified build --> <script src="https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.js"></script>
<!-- Minified ES Module build -->
<script type="module">
import { snapdom } from "https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs";
</script>
Build outputs
| Variant | File | Use case |
|---|---|---|
| ESM (tree-shakeable) | dist/snapdom.mjs | Bundlers (Vite, webpack), import |
| IIFE (global) | dist/snapdom.js | Browser script tag; exposes window.snapdom. No CommonJS build. |
Plugin registry import. The subpath shares the core runtime and registry:
import { registerPlugins } from '@zumer/snapdom/plugins';
Capture flow
A capture freezes the subtree; exports read that state. With the default SVG engine:
| Stage | What happens |
|---|---|
| Clone | Deep clone with styles, open Shadow DOM and same-origin iframes. Leave nodes out with exclude. |
| Styles & Pseudo | Inline ::before/::after as elements, resolve counter()/counters(). |
| Images & Backgrounds | Fetch and inline external images/backgrounds as data URLs. |
| Fonts | Embed the webfonts the capture uses by default. Render icon-font glyphs through the icon path. |
| SVG | Wrap clone in <foreignObject>, serialize to data:image/svg+xml. |
| Export | Return the SVG, rasterize to PNG/JPG/WebP/canvas, create a blob, or run a plugin export. |
Plugins can use the clone without rendering by declaring needs: 'clone'. Image exports require 'render', the default. Hooks and custom outputs: see the plugins guide.
Choose a recipe for your output
For a PNG preview, a downloaded file or a Blob to upload, use the HTML to PNG recipe. For a shareable report with several charts, use the dashboard export recipe.
For framework state updates, the Angular guide waits for the next render, the Svelte guide waits for tick(), and the Lit guide selects a card inside its shadow root after updateComplete.