SnapDOM

July 27, 2026 · View on GitHub

Website NPM version NPM weekly downloads GitHub contributors GitHub stars GitHub forks Sponsor tinchox5 License

English | 简体中文

SnapDOM

SnapDOM is a next-generation DOM Capture Engine — the fast, modern alternative to html2canvas, dom-to-image, and html-to-image.
It converts any DOM subtree into a self-contained representation that can be exported to SVG, PNG, JPG, WebP, Canvas, Blob, or any custom format through plugins — ultra-fast, modular, extensible, and dependency-free.

📖 Documentation, guides & live demos → snapdom.dev

Features

Full DOM capture with embedded styles, pseudo-elements and fonts; export to SVG, PNG, JPG, WebP, canvas or Blob — ultra fast, dependency-free, and 100% based on standard Web APIs.

👉 See the complete technical feature list in FEATURES.md.

Website & Live Demos

https://snapdom.dev

Quick Start

Capture any DOM element to PNG in one line:

import { snapdom } from '@zumer/snapdom';

const img = await snapdom.toPng(document.querySelector('#card'));
document.body.appendChild(img);

Reusable capture (one clone, multiple exports):

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' });

Table of Contents

Installation

NPM / Yarn (stable)

npm i @zumer/snapdom
yarn add @zumer/snapdom

NPM / Yarn (dev builds)

For early access to new features and fixes:

npm i @zumer/snapdom@dev
yarn add @zumer/snapdom@dev

⚠️ The @dev tag usually includes improvements before they reach production, but may be less stable.

CDN (stable)

<!-- Minified build -->
<script src="https://unpkg.com/@zumer/snapdom/dist/snapdom.js"></script>

<!-- Minified ES Module build -->
<script type="module">
  import { snapdom } from "https://unpkg.com/@zumer/snapdom/dist/snapdom.mjs";
</script>

CDN (dev builds)

<!-- Minified build (dev) -->
<script src="https://unpkg.com/@zumer/snapdom@dev/dist/snapdom.js"></script>

<!-- Minified ES Module build (dev) -->
<script type="module">
  import { snapdom } from "https://unpkg.com/@zumer/snapdom@dev/dist/snapdom.mjs";
</script>

Build Outputs

VariantFileUse case
ESM (tree-shakeable)dist/snapdom.mjsBundlers (Vite, webpack), import
IIFE (global)dist/snapdom.jsScript tag, legacy require

Bundler (npm):

import { snapdom } from '@zumer/snapdom';  // → dist/snapdom.mjs

Script tag (CDN):

<script src="https://unpkg.com/@zumer/snapdom/dist/snapdom.js"></script>
<script> snapdom.toPng(document.body).then(img => document.body.appendChild(img)); </script>

Subpath imports (lighter bundle if you only need one):

import { preCache } from '@zumer/snapdom/preCache';
import { plugins } from '@zumer/snapdom/plugins';

Usage

PatternWhen to use
Reusable snapdom(el)One clone → many exports (PNG + JPG + download).
Shortcuts snapdom.toPng(el)Single export, less code.

Reusable capture

Capture once, export many times (no re-clone):

const el = document.querySelector('#target');
const result = await snapdom(el);

const img = await result.toPng();
document.body.appendChild(img);
await result.download({ format: 'jpg', filename: 'my-capture.jpg' });

One-step shortcuts

Direct export when you need a single format:

const png = await snapdom.toPng(el);
const blob = await snapdom.toBlob(el);
document.body.appendChild(png);

CORS & External Resources

When capturing elements that reference external stylesheets (e.g., Google Fonts, Font Awesome, or any CDN‑hosted CSS), you must ensure that the resources are served with proper CORS headers. Otherwise, the captured image may lack the expected fonts or icons, even though they render correctly in the browser.

Why is this needed?

  • Browsers block JavaScript (including SnapDOM) from reading the binary data of cross‑origin fonts or images unless the server explicitly allows it via Access-Control-Allow-Origin.
  • SnapDOM relies on Canvas, which enforces strict CORS policies — unlike the browser's rendering engine, which is more permissive for on‑screen display.

How to fix it

Add the crossorigin="anonymous" attribute to the <link> tag when loading external stylesheets:

<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css"
  crossorigin="anonymous"
/>

Note: If you are hosting the fonts or assets on the same origin as your page (e.g., using a local server like http://localhost), you do not need to add crossorigin – the browser treats them as same‑origin and allows full access.

Documentation

The full reference lives on snapdom.dev/docs — kept there so it stays in sync and searchable:

  • API reference — the snapdom() reusable object, shortcut methods, and exporter-specific options.
  • Options — every capture option (scale, dpr, embedFonts, useProxy, exclude/filter, compress, outerTransforms, outerShadows, cache…) explained with examples.
  • Plugins — build, register and ship custom plugins and export formats. Browse community plugins on the plugins page.
  • Cache & preCache — control caching between captures and preload resources.

API at a glance

snapdom(el, options?) returns a reusable object (toPng, toSvg, toCanvas, toBlob, toJpg, toWebp, download, url). For single exports, use the shortcuts:

MethodDescription
snapdom.toSvg(el, options?)Returns an SVG HTMLImageElement
snapdom.toCanvas(el, options?)Returns a Canvas
snapdom.toBlob(el, options?)Returns an SVG or raster Blob
snapdom.toPng(el, options?)Returns a PNG image
snapdom.toJpg(el, options?)Returns a JPG image
snapdom.toWebp(el, options?)Returns a WebP image
snapdom.download(el, options?)Triggers a download

Options at a glance

All options are optional and can be passed to snapdom(el, options) or any shortcut method.

OptionTypeDefaultDescription
scalenumber1Output scale multiplier
dprnumberdevicePixelRatioPixel density of the rasterized output
width / heightnumbernullTarget output size (keeps aspect ratio if only one is set)
backgroundColorstringnull (#ffffff for JPEG/WebP)Background fill
qualitynumber0.92JPEG/WebP quality (0–1)
format'png' | 'jpeg' | 'webp' | 'svg''png'Format for download()
typestring'svg'Blob type for toBlob() ('png', 'jpeg'…)
filenamestring'snapDOM'Download filename
embedFontsbooleanfalseInline @font-face so text renders with your real fonts
iconFontsstring | RegExp | array[]Icon font families (always embedded)
localFontsarray[]Explicit fonts: { family, src, weight?, style? }
excludeFontsobjectSkip fonts by family / domain / subset
excludestring[][]CSS selectors to leave out of the capture
filter(el) => booleannullKeep-predicate (return false to drop a node)
excludeMode / filterMode'hide' | 'remove''hide'How excluded nodes are handled
clip'viewport' | {x, y, width, height}nullCapture only a region; offscreen content is pruned
compressbooleantrueDownsample inlined images to their visible resolution
useProxystring''CORS proxy prefix for cross-origin images
fallbackURLstring | fnFallback image for broken <img>
cache'soft' | 'auto' | 'full' | 'disabled''soft'Cache policy between captures
outerTransformsbooleantrueKeep root translate/rotate in the output
outerShadowsbooleanfalseExpand bounds to include root shadows/blur/outline
fastbooleantrueSkip idle delays for faster capture
reconcilebooleanfalseMeasure the clone against the live DOM and pin any diverging box to its real size. Fixes rare text re-wrap/layout drift at the cost of roughly doubling capture time — snapdom warns once (console.warn) if it detects a capture that could benefit from it
burstbooleanfalseMemoizes repeated captures of this element via a scoped MutationObserver — an unchanged repeat skips the pipeline entirely. Without it, snapdom warns once if the same element is captured 3+ times within 2s
invalidatebooleanfalseWith burst: true, forces a fresh capture for changes automatic tracking can't see (canvas draws, programmatic CSSOM edits)
pluginsarrayPer-capture plugins (override globals by name)

📖 Full API & every option, explained with examples → snapdom.dev/docs

Limitations

  • External images should be CORS-accessible (use useProxy option for handling CORS denied)
  • When WebP format is used on Safari, it will fallback to PNG rendering.
  • @font-face CSS rule is well supported, but if need to use JS FontFace(), see this workaround #43
  • Safari: captures with embedFonts or background/mask images run slower due to WebKit #219770 (font decode timing). SnapDOM does pre-captures + drawImage to prime the pipeline; configurable via safariWarmupAttempts (default 3).
  • Custom scrollbar styles (::-webkit-scrollbar): Applied only when the element has not been scrolled. When scrolled, the viewport content is captured without the scrollbar.

Performance Benchmarks

Setup. Vitest benchmarks on Chromium, repo tests. Hardware may affect results. Values are average capture time (ms) → lower is better.

Simple elements

ScenarioSnapDOM currentSnapDOM v1.9.9html2canvashtml-to-image
Small (200×100)0.5 ms0.8 ms67.7 ms3.1 ms
Modal (400×300)0.5 ms0.8 ms75.5 ms3.6 ms
Page View (1200×800)0.5 ms0.8 ms114.2 ms3.3 ms
Large Scroll (2000×1500)0.5 ms0.8 ms186.3 ms3.2 ms
Very Large (4000×2000)0.5 ms0.9 ms425.9 ms3.3 ms

Complex elements

ScenarioSnapDOM currentSnapDOM v1.9.9html2canvashtml-to-image
Small (200×100)1.6 ms3.3 ms68.0 ms14.3 ms
Modal (400×300)2.9 ms6.8 ms87.5 ms34.8 ms
Page View (1200×800)17.5 ms50.2 ms178.0 ms429.0 ms
Large Scroll (2000×1500)54.0 ms201.8 ms735.2 ms984.2 ms
Very Large (4000×2000)171.4 ms453.7 ms1,800.4 ms2,611.9 ms

Run the benchmarks

git clone https://github.com/zumerlab/snapdom.git
cd snapdom
npm install
npm run test:benchmark

Development

Source layout:

  • src/api/ – Public API (snapdom, preCache)
  • src/core/ – Capture pipeline, clone, prepare, plugins
  • src/modules/ – Images, fonts, pseudo-elements, backgrounds, SVG
  • src/exporters/ – toPng, toSvg, toBlob, etc.
  • dist/ – Build output (snapdom.js, snapdom.mjs, preCache.mjs, plugins.mjs)

Build:

git clone https://github.com/zumerlab/snapdom.git
cd snapdom
git checkout dev
npm install
npm run compile

Test:

npx playwright install   # Required for browser tests
npm test
npm run test:benchmark

For detailed guidelines, see CONTRIBUTING.

Contributors

tinchox5 pdufour FlavioLimaMindera Jarvis2018 tarwin Amyuan23 kohaiy airamhr9 jswhisperer K1ender mosuzi 17biubiu av01d CHOYSEN pedrocateexte claude domialex elliots stypr mon-jai puneetdixit200 RexSkz RinZ27 sharuzzaman simon1uo titoBouzout ZiuChen adajoy

Sponsors

Special thanks to @megaphonecolin, @sdraper69, @reynaldichernando, @gamma-app, @jrjohnson, and @ryanander for supporting this project!

If you'd like to support this project too, you can become a sponsor.

Show your support

If SnapDOM saved you time, a ⭐ on GitHub helps other developers find it — that's the whole ask.

Shipping something built with SnapDOM? Add the badge to your README:

Built with SnapDOM

[![Built with SnapDOM](https://img.shields.io/badge/built%20with-SnapDOM-blue)](https://snapdom.dev)

Projects using SnapDOM

SnapDOM runs in production across 250+ public repositories (GitHub dependents graph). A few notable ones, each verified from its own package.json:

See the full gallery at snapdom.dev/made-with. Shipping SnapDOM? Open a PR to add your project — real, verifiable projects only.

License

MIT © Zumerlab