Skip to content

SVG and UI as images

react-smart-copy/capture is a separate, opt-in entry (~3.3 kB, no dependencies). It never ships in your bundle unless you import it.

A live SVG chart, copied as a PNG image.
Weekly active users Mon Tue Wed Thu Fri 0 50 100 Weekly Active Users

SVG to PNG, no extra dependency

svgToPngBlob rasterises inside the click so Safari keeps the gesture, enforces pixel and time limits, and stops when the copy is cancelled.

import { svgToPngBlob } from 'react-smart-copy/capture';

copy({
  kind: 'image',
  blob: (context) => svgToPngBlob(chartRef.current, { signal: context?.signal }),
});
import { captureImage, svgToPngBlob } from "react-smart-copy/capture";
import { domToBlob } from "modern-screenshot"; // any rasteriser you like, for HTML
// Any <svg> (chart, logo, icon): rasterised natively, no rasteriser needed.
<CopyField.Root label="Chart" value={captureImage(() => chartRef.current)}>
<CopyField.Trigger>Copy chart</CopyField.Trigger>
</CopyField.Root>;
// Any HTML element (invoice card, receipt): bring the rasteriser.
<CopyField.Root
label="Invoice"
value={captureImage(() => invoiceRef.current, {
rasterize: (node, { scale }) => domToBlob(node, { scale }),
background: "#ffffff",
})}
>
<CopyField.Trigger>Copy as image</CopyField.Trigger>
</CopyField.Root>;
// Or by hand, e.g. from SVG markup:
const png = await svgToPngBlob('<svg viewBox="0 0 24 24">…</svg>', { width: 256 });

What you get beyond calling a rasteriser yourself:

  • Always a real PNG. Output is verified from its bytes; JPEG/WebP/canvas/data-URL outputs are re-encoded, anything else is refused
  • Size limits. maxPixels (default 4096 × 4096, Safari’s canvas limit) is checked before rendering and again on the output; SVG markup is capped at maxLength
  • Deadlines. timeoutMs (default 15 s) and an optional signal: AbortSignal; the rasteriser receives a signal it can honour
  • Safe SVG handling. SVG is decoded as an image, where browsers disable scripts and external loads, and it is never inserted into your page. Network URLs from a rasteriser are never fetched
  • Crisp by default. scale: 2; pass window.devicePixelRatio or anything up to 10

Failures are classified like everything else: invalid-payload (no element / unknown size), unsupported, too-large, timeout, aborted, blob-generation-failed. Rasterisers have real limits (cross-origin images taint the canvas, web fonts must be loaded, <video> and <iframe> rarely render), so offer a text fallback when capture fails.

A cross-origin image blocks the capture. If the element holds an image from another site that does not allow it, the browser refuses to hand out the picture. You get a blob-generation-failed whose message names the cause, and whose cause is the browser’s SecurityError. It is not retryable: isRetryableError and retry() treat it as final, because trying again cannot change what the image’s server says. Fix it at the source: serve the image with an Access-Control-Allow-Origin header and load it with crossorigin="anonymous", or leave it out of the captured element.