How to screenshot a div in JavaScript
Select the rendered element, call snapdom.toPng, and use the returned image in a preview or download.
snapdom.toPng(element) returns an HTMLImageElement for a preview. Use download() for a file or toBlob() for upload; examples follow.
Step 1: Install SnapDOM
npm install @zumer/snapdom@latest
If you don't use a bundler, you can also use the CDN build:
<script type="module"> import { snapdom } from "https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs"; </script>
Step 2: Pick the div you want to screenshot
Select the rendered element you want to export:
const el = document.querySelector('#card');
It doesn't need to be inside the viewport, but it must be attached to the document and not display: none. If it loads web fonts or images, wait for them first:
await document.fonts.ready;
await Promise.all(Array.from(el.querySelectorAll('img'), img => img.decode()));
For an iframe or a web component, see the iframe and Shadow DOM recipes.
Step 3: Capture it
One line:
import { snapdom } from '@zumer/snapdom'; const img = await snapdom.toPng(el);
The returned img is a real HTMLImageElement with the captured content as its src (a PNG data URL).
Step 4: Do something with the image
Append it to the page
const img = await snapdom.toPng(el); document.body.appendChild(img);
Download it as a file
const result = await snapdom(el); await result.download({ format: 'png', filename: 'card' });
Get a Blob to upload to a server
const blob = await snapdom.toBlob(el, { format: 'png' }); const form = new FormData(); form.append('file', blob, 'screenshot.png'); await fetch('/api/upload', { method: 'POST', body: form });
Full working example
Drop this into a fresh HTML file and open it in a browser:
<!DOCTYPE html> <html> <body> <div id="card" style="padding:24px;background:#4f46e5;color:#fff;border-radius:12px;"> <h2>Hello SnapDOM</h2> <p>This entire div is about to become a PNG.</p> </div> <button id="go">Capture</button> <script type="module"> import { snapdom } from "https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs"; document.getElementById('go').addEventListener('click', async () => { const img = await snapdom.toPng(document.getElementById('card')); document.body.appendChild(img); }); </script> </body> </html>
Or try it right here
The example above, running on this page — same div, same snapdom.toPng call:
Hello SnapDOM
This entire div is about to become a PNG.
Useful options
dpr: 2— pixel density (auto-detectswindow.devicePixelRatiofor retina; override here when you need a fixed value).scale: 2— multiply the captured size (zoom). Different fromdpr:scalemakes the output bigger,dprmakes it sharper at the same logical size.backgroundColor: '#fff'— fill transparent areas with a colour.embedFonts: 'auto'— the v3 default embeds used web fonts.exclude: ['.skip']— skip any element matching the selector.useProxy: 'https://your-proxy.example.com/'— fetch cross-origin images through a CORS proxy.
Frequently asked questions
Why not use the browser's built-in screenshot API?
Chrome 157+ has drawElementImage(), with restrictions on where drawable elements live; SnapDOM’s native engine guide explains it. The Screen Capture API (getDisplayMedia) captures a user-selected tab or screen and requires permission; it does not target an arbitrary DOM element.
Does this work with web fonts?
Yes. SnapDOM embeds used web fonts by default with embedFonts: 'auto', provided their files are accessible.
Can I screenshot something inside an iframe?
Same-origin iframes are supported out of the box. Cross-origin iframes are not: the browser blocks access to their content.
What about Shadow DOM?
Open shadow roots need no extra setup. See Capture Shadow DOM.
How big is the library?
SnapDOM has zero runtime dependencies. Check the distributed build you import for its current bundle size; official plugins are separate imports.