Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use html2canvas(element, options) in a browser to render an HTML element into a canvas, then append the canvas or export it as an image. It reconstructs the element from the DOM and CSS; it does not take a native screenshot of the browser’s pixels. That distinction matters for CSS fidelity, cross-origin images, and server-side use.

Install html2canvas and capture an element

Install the package in a project that uses a JavaScript bundler:

npm install @html2canvas/html2canvas
# or: yarn add @html2canvas/html2canvas
# or: pnpm add @html2canvas/html2canvas

Import it, select the element after it exists in the document, and await the returned Promise:

import html2canvas from '@html2canvas/html2canvas';

const element = document.querySelector('#capture');
if (!element) throw new Error('Could not find #capture');

const canvas = await html2canvas(element);
document.body.appendChild(canvas);

The element can be a card, chart, section, or other DOM node. The result is a browser <canvas>, not a PNG file yet. The official getting-started documentation also describes a CDN build for pages that do not use a bundler: html2canvas getting started.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Call it from a button

In a regular script, wrap the asynchronous capture in an event handler:

document.querySelector('#download').addEventListener('click', async () => {
  const element = document.querySelector('#capture');
  if (!element) return;

  const canvas = await html2canvas(element);
  document.body.appendChild(canvas);
});

Ensure the selected element has been rendered and its content is ready before starting the capture. If the page is still loading images or updating a chart, wait for that application-specific work first; html2canvas does not make unfinished page content complete by itself.

Save the canvas as a PNG

Use the canvas toDataURL() method and an anchor with a download filename:

const canvas = await html2canvas(document.querySelector('#capture'));
const link = document.createElement('a');
link.download = 'screenshot.png';
link.href = canvas.toDataURL('image/png');
link.click();

This creates a PNG download in the browser. If an image from another origin has tainted the canvas, exporting it can fail with a security exception; resolve the image’s CORS setup rather than treating export as a way around browser security. The project’s examples include the download approach.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Crop, resize, and shape the capture

The optional second argument controls how html2canvas renders. The documented options include crop coordinates and dimensions, output scale, background, cloning hooks, exclusions, and viewport dimensions. Here is a crop scaled for the current display’s pixel ratio:

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
const canvas = await html2canvas(document.querySelector('#capture'), {
  x: 100,
  y: 100,
  width: 400,
  height: 300,
  scale: window.devicePixelRatio,
});

Crop and output scale

  • x and y set the crop’s position; width and height set its size.
  • scale affects the output resolution. The documented default is the browser’s device-pixel ratio. Setting it explicitly can help you control output dimensions, but a higher scale also creates a larger canvas and can reach browser limits sooner.

Transparent background and page-only changes

Set backgroundColor: null when you need transparency instead of a solid background. Use onclone to modify the cloned document that html2canvas renders, without changing the live page. For example, remove a control from the cloned version:

const canvas = await html2canvas(document.querySelector('#capture'), {
  onclone(clonedDocument) {
    clonedDocument.querySelector('.capture-controls')?.remove();
  },
  backgroundColor: null,
});

To omit content, add data-html2canvas-ignore to an element, or pass an ignoreElements predicate in the options. These are useful for excluding buttons or controls that should remain visible on the page but not appear in the output. Check the configuration options for the complete option list and current signatures.

Capture a long element

For a tall region, set the rendering window dimensions to the element’s scroll dimensions:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const element = document.querySelector('#capture');
const canvas = await html2canvas(element, {
  windowWidth: element.scrollWidth,
  windowHeight: element.scrollHeight,
});

This can help render content beyond the visible viewport, but it does not remove canvas size limits. For a long page, consider splitting the content into smaller captures if the output is blank, clipped, or incomplete.

Why images are missing: cross-origin rules

An image loaded from another origin may be omitted or may taint the canvas. The browser enforces this policy; html2canvas cannot bypass it. Setting useCORS: true asks the browser to load images with CORS, but it works only if the image server returns the required CORS response header.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

For example, you can request CORS-aware loading like this:

const canvas = await html2canvas(document.querySelector('#capture'), {
  useCORS: true,
});

If you control the image host, configure its response to allow the page’s origin as appropriate. If you do not control it, the documented alternative is a proxy that accepts a ?url= parameter and returns the resource in a same-origin-safe form. A proxy must be configured deliberately; do not expose an unrestricted URL-fetching endpoint, which could be abused to retrieve internal resources.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The allowTaint option controls whether tainted images are allowed into the canvas; it does not grant permission to read or export tainted pixels. See the project’s FAQ and configuration reference for CORS and proxy details.

What html2canvas can and cannot reproduce

html2canvas traverses the DOM and renders elements by implementing CSS properties. It does not capture the browser’s already-painted pixels. As the project puts it, “The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot.” Read its About page for the rendering model and compatibility information.

That makes it useful when client-side DOM rendering is what you need, but pixel differences are possible when a page uses CSS or browser features the renderer does not support or fully implement. Same-origin iframes can be rendered recursively; cross-origin iframes and sandboxed iframes without allow-same-origin cannot be read. Flash and Java applets are not rendered.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

For a fidelity-sensitive capture, compare the output against the intended view in the browser and test the specific styles and embedded content your page uses. The official documentation does not provide a universal accuracy percentage or performance benchmark, so neither should be assumed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Canvas size, blank output, and clipping

Canvas dimension and total-area limits vary by browser and device. The html2canvas FAQ gives rough guidance of about 32,767 pixels per dimension for Chrome/Chromium, Firefox, and desktop Safari, while noting that area limits and iOS Safari behavior vary. These are not guarantees for a particular device or page.

Very large canvases may become blank or partially rendered without throwing an exception. If a capture fails this way, try these steps:

  1. Reduce scale to lower the output dimensions.
  2. Capture a smaller element or crop with x, y, width, and height.
  3. For a long region, render smaller sections separately rather than building one enormous canvas.
  4. Set windowWidth and windowHeight to the element’s scroll dimensions when the visible viewport is truncating content.

Because both per-dimension and area limits depend on the browser and hardware, validate the largest capture on the actual devices you support rather than relying on a single nominal maximum.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can html2canvas run in Node.js?

No. html2canvas targets modern evergreen browsers, including Firefox, Chromium-based browsers, and Safari, and depends on browser APIs. It is not suitable for Node.js server-side rendering. For server-side screenshot jobs, use a real-browser automation tool such as Puppeteer or Playwright, which the project FAQ points to as alternatives.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

The distinction is practical: html2canvas reconstructs an element from a page’s DOM in the browser where it runs; Puppeteer or Playwright can launch or control a browser in a Node.js job and capture rendered browser output. Choose based on whether you need client-side DOM rendering or server-side browser automation.

Or skip the browser setup

If you need a screenshot from a URL without installing browser-side code, ScreenshotNeo is a website screenshot API and MCP server. Its API returns an image or PDF from one GET request. For example, save a WebP screenshot of Stripe with cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for parameters and response details. It accepts cookie/consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo’s free plan.

Troubleshooting common html2canvas problems

Symptom Likely cause What to try
Images are absent The image host does not provide CORS headers, or the image cannot be loaded in a browser-safe way. Try useCORS: true with a host that allows the required origin, or configure a controlled same-origin proxy.
PNG export throws a security error A cross-origin image tainted the canvas. Correct CORS or proxy handling; allowTaint does not make tainted pixels exportable.
Output differs from the page The renderer reconstructs DOM and CSS rather than taking a native pixel screenshot; a property or embedded feature may be unsupported. Check the project’s supported features, simplify the affected styling, or use a real-browser capture method when pixel output is required.
Iframe content is missing The iframe is cross-origin or sandboxed without allow-same-origin. Use accessible same-origin content where appropriate; browser security prevents reading inaccessible frames.
Output is blank or cut off The canvas is too large for the current browser/device, or the render window does not include the full content. Reduce scale, capture smaller pieces, and set window dimensions to the element’s scroll dimensions for long content.
The selected element is not captured The selector returned null, or the element was not yet rendered when capture began. Verify the selector and call html2canvas only after the element and its required content are present.

Choosing the right capture approach

Use html2canvas when the capture should happen in the browser and reconstructing a DOM region is acceptable. Use a headless browser such as Puppeteer or Playwright when the job must run in Node.js or requires actual browser-rendered screenshot output. Before committing to either approach, evaluate the dimensions that must fit, how external images and frames are served, the CSS and browser features the page relies on, and whether output controls such as cropping or transparent backgrounds are required.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does html2canvas capture an element’s exact pixels?

No. It rebuilds the output from DOM and styles, so the result can differ from the browser’s actual rendering.

Can I use html2canvas to capture a cross-origin iframe?

No. Cross-origin frames, and sandboxed frames without allow-same-origin, are not readable to the library.

Does useCORS make every remote image work?

No. The image server must return the required CORS response header for the page’s origin.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.