Turn any element into a PNG, JPEG, WebP, SVG or canvas. html2shot works as a drop-in replacement for html2canvas,
but the browser does the drawing, so your screenshot matches the page more closely and is ready sooner.
PNG Β· JPEG Β· WebP Β· SVG Β· html2canvas API Β· web fonts Β· shadow DOM Β· modern CSS Β· Chrome, Safari, Firefox
Live demo Β· Quick start Β· Benchmark Β· API Β· Options Β· Coming from html2canvas Β· Limits Β· FAQ Β· Sponsor
β Star this repo to follow new releases Β Β·Β π Found a page that renders wrong? Β Β·Β π Sponsor the project
html2shot is a small JavaScript library that turns any part of a web page into an image, right in the visitor's browser. Give it an element (a chart, a receipt, a social card, a whole page) and you get back a PNG, JPEG, WebP, SVG or canvas, ready to download, upload, copy to the clipboard or share. It works with any framework, needs no server or headless browser, and can replace html2canvas without changing your code.
Browsers have no built-in way to screenshot an element, so libraries fake it. html2canvas, the best known, redraws the page with its own CSS engine. Anything it hasn't implemented comes out wrong or blank: modern colours like color-mix(), masks, backdrop-filter, many text effects. Newer libraries let the browser draw instead, but copy every computed style onto every element, which gets slow on big pages. html2shot lets the browser draw too, and writes each distinct style only once. It's the most accurate of the six libraries we tested in Chrome, Safari and Firefox, and about twice as fast as the next best on large pages.
npm install html2shotimport { toPng } from 'html2shot';
const png = await toPng(document.querySelector('#card'));
// "data:image/png;base64,..." : use it as an <img> src, or download itWithout a build step:
<script src="https://cdn.jsdelivr.net/npm/html2shot@1"></script>
<script>
html2shot.toPng(document.querySelector('#card')).then((png) => {
const link = document.createElement('a');
link.download = 'card.png';
link.href = png;
link.click();
});
</script>Already using html2canvas? Change the import. The rest of your code stays the same:
- import html2canvas from 'html2canvas';
+ import html2canvas from 'html2shot';
const canvas = await html2canvas(element, { scale: 2 });Six libraries, 18 test pages (text and web fonts, flex and grid cards, forms, SVG sprites, shadow DOM, tables, color-mix(), masks, iframes, sticky and vertical text, a 400-row page), in three browser engines. The browser's own screenshot of each page is the reference. The score is the share of pixels that match it (no colour channel more than 48 of 255 apart). Anti-aliased text never matches exactly, so even a perfect result scores a little under 100% on pages with a lot of text.
| Library | Chromium: match | WebKit: match | Firefox: match | Typical time (Chromium) | 400-row page (Chromium) |
|---|---|---|---|---|---|
| html2shot | 98.9 % | 99.8 % | 99.9 % | 136 ms | 452 ms |
| snapdom | 98.8 % | 99.3 % | 99.3 % | 160 ms | 772 ms |
| modern-screenshot | 97.8 % | 98.7 % | 98.9 % β | 119 ms | 1404 ms |
| html2canvas-pro | 98.0 % | 97.5 % | 98.8 % | 152 ms | 522 ms |
| html-to-image | 96.3 % | 93.4 % | 97.5 % | 112 ms | 4059 ms |
| html2canvas | 92.3 % * | 93.0 % * | 94.0 % * | 180 ms | 609 ms |
Averages cover the pages a library finished. * html2canvas throws on the color-mix() page in every browser (it can't parse the colour), so its average covers the other 17. β modern-screenshot timed out after 20 s on the iframes page in Firefox. "Typical time" is the median over the 18 pages. Measured with Playwright at 1Γ scale on Windows 11, html2shot 1.0.0, snapdom 3.9, modern-screenshot 4.7, html2canvas-pro 2.6, html-to-image 1.11, html2canvas 1.4. Per-page results are in bench/results.json; run it yourself with npm run bench.
What the numbers mean in practice:
- html2canvas and html2canvas-pro redraw the page with their own CSS engine. Anything they haven't implemented (
color-mix(), masks,backdrop-filter, some text effects) comes out wrong or blank. - html-to-image and modern-screenshot let the browser draw, like html2shot, but copy every computed style onto every element. Large pages get slow; html-to-image needs four seconds for 400 rows.
- html2shot lets the browser draw and writes each distinct style once, as a shared class, leaving out whatever the copy inherits or gets from the browser anyway. It has the best score in all three engines, and is the fastest on the 400-row page: about 1.7Γ faster than snapdom in Chromium and 2.3Γ in WebKit and Firefox.
Every function takes an element and optional options, and returns a promise.
| Function | Returns |
|---|---|
toPng(element) |
PNG data URL |
toJpeg(element) |
JPEG data URL, on white (JPEG has no transparency) |
toWebp(element) |
WebP data URL (PNG in browsers that can't encode WebP) |
toSvg(element) |
SVG data URL: vector, with fonts and images embedded |
toBlob(element) |
Blob, PNG by default, ready for fetch, FormData or the clipboard |
toCanvas(element) |
HTMLCanvasElement, transparent background |
toDataUrl(element) |
data URL of any type ('image/png', 'image/jpeg', 'image/webp') |
html2shot(element) |
the default export: html2canvas-compatible canvas on white |
clearCache() |
forgets embedded images and fonts |
import { toBlob, toCanvas } from 'html2shot';
// Upload a screenshot
const body = new FormData();
body.append('screenshot', await toBlob(element), 'screenshot.png');
await fetch('/api/feedback', { method: 'POST', body });
// Copy it to the clipboard
await navigator.clipboard.write([new ClipboardItem({ 'image/png': toBlob(element) })]);
// Print-quality, at three times the size
const canvas = await toCanvas(element, { scale: 3 });| Option | Default | What it does |
|---|---|---|
scale |
devicePixelRatio |
Pixels per CSS pixel. 2 gives a sharp image on most screens. |
backgroundColor |
transparent | Fills the background. html2shot() and toJpeg() use white; null keeps it transparent. |
width, height |
the element's size | Size of the screenshot in CSS pixels. |
filter |
(element) => boolean. Return false to leave an element and its contents out. |
|
ignoreElements |
(element) => boolean. Return true to leave an element out (html2canvas's name). |
|
onclone |
(document, element) => void. Change the copy before it is drawn, e.g. reveal hidden parts. |
|
style |
Extra CSS for the screenshot's root, e.g. { borderRadius: '0', margin: '0' }. |
|
fonts |
true |
Embed the web fonts the element uses. Turn off if you only use system fonts. |
imageTimeout |
15000 |
Milliseconds to wait for each image or font. |
proxy |
URL prefix for images on servers without CORS, e.g. '/proxy?url='. See Limits. |
|
placeholder |
transparent | Image shown instead of images that can't be loaded (a data URL). |
onError |
(url, error) => void. Called for each image, font or stylesheet that couldn't be loaded. |
|
cache |
true |
Keep embedded images and fonts for the next screenshot. |
quality |
0.92 |
JPEG and WebP quality, from 0 to 1. |
type |
'image/png' |
Image type for toBlob and toDataUrl. |
canvas |
Draw into this canvas instead of a new one. |
await toPng(element, {
scale: 2,
backgroundColor: '#fff',
filter: (el) => !el.classList?.contains('no-screenshot'),
onError: (url) => console.warn('Could not load', url),
});import html2canvas from 'html2shot'returns a canvas with a white background, like html2canvas.scale,backgroundColor,width,height,ignoreElements,onclone,canvasandimageTimeoutwork the same.useCORS,allowTaint,logging,foreignObjectRendering,removeContainer,windowWidth,windowHeight,scrollX,scrollY,xandyare accepted and ignored. html2shot always loads images with CORS, never taints the canvas, and doesn't need the window size.- With a
<script>tag, adddata-html2canvasto also definewindow.html2canvas:
<script src="https://cdn.jsdelivr.net/npm/html2shot@1" data-html2canvas></script>What you get by switching: color-mix(), oklch(), masks, backdrop-filter, clip-path, blend modes, gradient text, web fonts from Google Fonts and shadow DOM all render, because the browser draws them.
- Fonts: web fonts, including cross-origin stylesheets like Google Fonts. Only the faces and
unicode-rangesubsets your screenshot uses are embedded, so a page with ten language subsets embeds one or two small files. - Images:
<img>withsrcsetandsizes,<picture>, lazy images that haven't loaded yet, CSS backgrounds,border-image, masks,image-set(), and images inside::beforeand::after. - SVG: inline SVG, icon sprites with
<use href="#icon">and gradients or filters defined elsewhere on the page. - Live content: what's drawn on a
<canvas>, the current frame of a<video>, and same-origin iframes. - Forms: typed values, checkboxes, radio buttons, selected options, textareas. Password fields stay masked.
- Components: shadow DOM and slots, so web components (Lit, Shoelace, Stencil) look right.
- Scroll position: scrolled containers show what you see, without scrollbars.
- Pseudo-elements:
::before,::after,::marker,::placeholder,::first-letterand::first-line. - Your page stays untouched: html2shot copies the element into a detached document. Nothing on the page moves, flickers or changes, and nothing is left behind.
- Images on other servers need CORS. A browser won't let any script read pixels from another site unless that site allows it (
Access-Control-Allow-Origin). Most CDNs do. For those that don't, setproxyto a URL on your own server that fetches the image. Images that still can't be loaded become theplaceholderand are reported toonError. - Cross-origin iframes can't be read by any script, so they come out empty. Same-origin iframes work.
- Browser-only: html2shot needs a real browser. Called during server-side rendering, it rejects with a clear error. Call it from an event handler or
useEffect. - Very large screenshots are scaled down to fit the browser's canvas limit (about 16,000 pixels per side, 268 megapixels in total), so the call doesn't fail.
Is it a drop-in replacement for html2canvas? For almost all code, yes: same call, same options, same canvas result. See Coming from html2canvas.
Why is my image blank?
It's usually an image from another server without CORS. Pass onError to see which URL failed, then enable CORS on that server or set proxy.
How do I make the image sharper?
Use scale: 2 or scale: 3. The default matches the screen, so a 1Γ screen gives a 1Γ image.
Can I capture the whole page?
Yes: toPng(document.body). html2shot uses the full scroll height of <body> and <html>.
Can I capture something that's hidden?
Use onclone to show it in the copy, e.g. onclone: (doc, el) => { el.style.display = 'block'; }. The page itself doesn't change.
React, Vue, Svelte, Angular?
Pass the DOM element: a ref in React (toPng(ref.current)), a template ref in Vue, bind:this in Svelte, ElementRef.nativeElement in Angular. Call it from an event handler or after mount, never during server-side rendering.
Does it work in Safari and Firefox? Yes. CI runs the pixel-comparison tests in Chromium, WebKit and Firefox on every change.
TypeScript? Types are included.
html2shot is free and always will be. Sponsorships pay for the time it takes to keep screenshots accurate as browsers change: new CSS features, engine quirks in Chrome, Safari and Firefox, and the benchmark that keeps every release honest.
| Who | You get | |
|---|---|---|
| β Individuals | Any amount, monthly or one-time | Our thanks, a sponsor badge on your GitHub profile, and your name in this README |
| π Companies | Teams whose products take screenshots with html2shot | Your logo in this README, with a link to your website |
| ποΈ Founding sponsors | The first companies to back the project | A large logo at the top of this README and the live demo, and a say in the roadmap |
Be the first sponsor and your logo goes right here.
Using html2shot in your project? Add the badge:
[](https://github.com/html2shot/html2shot)See CONTRIBUTING.md. npm run check runs every check, npm run test:browser the real-browser tests and npm run bench the benchmark.