SnapDOMGitHub8K
Documentation
zumerlab/snapdom

SnapDOM API Reference

The complete SnapDOM surface: the reusable snapdom(el) object, the one-step shortcut methods, and the export-only options each exporter accepts.

Shortcut methods Capture options
At a glance

Call snapdom(el) to get a reusable result object: clone once, export many times to SVG, PNG, JPG, WebP, Canvas or Blob, or trigger a download. For a single export, the snapdom.to* shortcut methods do the same in one call. Every method also accepts the full set of capture options.

Usage patterns

PatternWhen to use
snapdom(el)Reusable: one clone → many exports (PNG + JPG + download).
snapdom.toPng(el)Shortcut: 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);

snapdom(el, options?)

Returns a reusable object with export methods. The capture (deep clone + asset inlining) runs once; each method on the returned object reuses it, so exporting several formats from the same element does not re-clone the DOM.

{
  url: string;
  meta: Readonly<{
    w0: number; h0: number;
    vbW: number; vbH: number;
    targetW: number; targetH: number;
    contentX: number; contentY: number;
    clip: Readonly<{ x: number; y: number; width: number; height: number }> | null;
  }>;
  toRaw(): string;
  toImg(): Promise<HTMLImageElement>; // deprecated
  toSvg(): Promise<HTMLImageElement>;
  toCanvas(options?: {
    crop?: { x: number; y: number; width: number; height: number };
  }): Promise<HTMLCanvasElement>;
  toBlob(options?): Promise<Blob>;
  toPng(options?): Promise<HTMLImageElement>;
  toJpg(options?): Promise<HTMLImageElement>;
  toWebp(options?): Promise<HTMLImageElement>;
  download(options?): Promise<void>;
}

toImg() is deprecated: use toSvg() instead.

Shortcut methods

Each shortcut captures and exports in a single call. They accept the same element and the full set of capture options.

MethodDescription
snapdom.toImg(el, options?)Returns an SVG HTMLImageElement (deprecated)
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

Exporter-specific options

Some exporters accept a small set of export-only options in addition to the global capture options.

toCanvas({ crop }) and result.meta

The reusable result can rasterize a rectangular window of its canonical SVG. crop is expressed in SVG viewBox CSS pixels and rewrites the viewBox before image decode, so a long capture can become page- or tile-sized canvases without allocating one full-page bitmap. result.meta is the frozen geometry of that serialized capture.

Use the two-step API shown here. The one-step snapdom.toCanvas(el, options) shortcut does not accept an export crop.

MetadataDescription
w0 / h0Logical capture-box size.
contentX / contentYExact logical-content origin inside the SVG viewBox.
vbW / vbHComplete viewBox size, including bleed and padding.
clipResolved capture clip window, or null.
const result = await snapdom(page, { dpr: 1 });
const { contentX, contentY, w0, h0 } = result.meta;

const firstTile = await result.toCanvas({
  crop: {
    x: contentX,
    y: contentY,
    width: Math.min(2048, w0),
    height: Math.min(2048, h0),
  },
  dpr: 1,
});

A partly outside window is intersected with the viewBox. Empty, non-finite, fully outside or non-SVG crop requests reject. Each resulting canvas remains subject to the browser's raster limits after scale × dpr. See the tiled full-page walkthrough and live mosaic for a complete loop and the memory trade-offs.

download()

OptionTypeDefaultDescription
filenamestringsnapdomDownload name.
format"png" | "jpeg" | "jpg" | "webp" | "svg""png"Output format for the downloaded file.

Example:

await result.download({
  format: 'jpg',
  quality: 0.92,
  filename: 'my-capture'
});

toBlob()

OptionTypeDefaultDescription
type"svg" | "png" | "jpeg" | "jpg" | "webp""svg"Blob type to generate.

Example:

const blob = await result.toBlob({ type: 'jpeg', quality: 0.92 });

Try the API

Drop SnapDOM into your project and capture your first element in one line.

Open the demo Install from npm