PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteTo save Leaflet polygons as a PNG, wait until the map and its layers have finished loading, capture the map container, then convert the resulting canvas with toDataURL('image/png') and download it. For a DOM-based capture, html2canvas can include the visible map controls and surrounding HTML; if you use leaflet-image, render polygon paths with Canvas and expect HTML controls and divIcon content to be left out. In either case, cross-origin tiles without suitable CORS permission can disappear or make the canvas unreadable.
Choose the right way to export the map
The right method depends on what the PNG must contain. html2canvas reconstructs the visible DOM, so it suits a map export that should include controls, legends, labels, or nearby HTML. leaflet-image composes map layers into a canvas and is more focused on the map itself, but it does not include HTML-based controls, legends, or L.divIcon content. Both approaches remain subject to browser canvas security rules: they cannot read pixels from images that have not granted cross-origin access.
| Approach | Best fit | Important limitation |
|---|---|---|
html2canvas |
Capturing the visible DOM composition, including controls and surrounding HTML. | Reconstructs the DOM rather than taking a pixel-perfect screenshot; CSS support and cross-origin restrictions apply. html2canvas documentation. |
leaflet-image |
Composing Leaflet map layers into a canvas, including paths rendered with Canvas. | HTML controls, legends, and L.divIcon content are not included; source tiles and markers must support CORS. leaflet-image README. |
| Static-map service | Repeatable server-side or branded exports. | Not a capture of the current client-side DOM. Check the provider’s capabilities and tile licensing for your use case. The leaflet-image README describes the Mapbox Static Image API as a simpler and faster option for some cases. leaflet-image README. |
If the goal is simply to download what a user sees, start with html2canvas. If your export should consist of map layers and your paths can render on Canvas, consider leaflet-image. For unattended, repeatable output, a static-map service may be a better fit than exporting a live browser map.
Prepare the Leaflet map before capture
Give the map a real size
Leaflet needs a map container with nonzero width and height. Set its dimensions in CSS rather than relying on content to size it:
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
#map {
width: 100%;
height: 500px;
}
Leaflet’s Quick Start uses a sized container before initializing the map. Leaflet Quick Start. A zero-height or hidden container can yield a blank or incomplete export even if your map code runs without an error.
Wait for the visible map state to finish loading
Do not capture immediately after creating the map. Wait for the raster tiles and any overlays or polygon data needed in the export. You can listen for the relevant layer load events, or track your own data-loading promises and start capture only when they resolve. With asynchronous layers, wait for each required layer rather than treating the first tile response as proof that the whole map is ready.
Configure raster layers for pixel access
For tile pixel access through Leaflet, configure the tile layer’s crossOrigin option where appropriate. The tile server must also return compatible CORS headers; a client-side option cannot grant permission that the server does not provide. Apply the same checks to every raster source, including image overlays, not just the basemap. Leaflet documents the tile-layer option and its role in accessing tile pixel data. Leaflet TileLayer reference.
Rank #2
Render polygons with Canvas for leaflet-image
If you choose leaflet-image, render polygon paths using a Canvas renderer. You can pass renderer: L.canvas() when creating a path layer or set preferCanvas: true on the map. Leaflet documents both choices. Leaflet Canvas reference. This is specific to the layer-composition route; html2canvas captures the rendered DOM instead.
Capture the map container with html2canvas
Load html2canvas in your page using the installation method appropriate to your application, then capture the actual map element. This example returns a PNG download:
async function saveLeafletPng() {
const mapElement = document.getElementById('map');
if (!mapElement) throw new Error('Map element #map was not found');
const canvas = await html2canvas(mapElement, {
useCORS: true,
backgroundColor: '#fff',
scale: window.devicePixelRatio
});
const link = document.createElement('a');
link.download = 'leaflet-map.png';
link.href = canvas.toDataURL('image/png');
link.click();
}
The official html2canvas example uses the same canvas-to-PNG download pattern. html2canvas examples.
Rank #3
What the options do
useCORS: trueasks html2canvas to load eligible cross-origin images with CORS. The remote server must grant permission with its response headers; this option does not bypass browser policy.backgroundColor: '#fff'makes the exported canvas have a white background rather than relying on transparency.scale: window.devicePixelRatioincreases output resolution on high-density displays. A larger scale also creates a larger canvas, so very large maps can encounter browser canvas-size or memory limits.
To omit an unwanted control from the capture, mark it with data-html2canvas-ignore. The html2canvas configuration and limitations are described in its documentation. html2canvas configuration.
Keep attribution visible
If the exported image includes map tiles, preserve any attribution required by the tile or map provider. Attribution rules depend on the data and service you use; exporting a screenshot does not remove those obligations. The leaflet-image README specifically cautions that generated map images generally need the source map’s attribution. leaflet-image README.
Recommended Free Tools
Use leaflet-image when you only need map layers
leaflet-image is designed to export Leaflet layers without a server component, using Canvas and CORS. Its README documents the callback-based capture flow. leaflet-image README. A basic pattern is:
leafletImage(map, function (error, canvas) {
if (error) {
console.error('Could not export map:', error);
return;
}
const link = document.createElement('a');
link.download = 'leaflet-map.png';
link.href = canvas.toDataURL('image/png');
link.click();
});
Ensure polygon paths use Canvas before calling the export routine. This route is not a way to capture the whole browser composition: HTML controls, legends, and L.divIcon markers are omitted. Raster tiles and markers must also be served in a way that permits canvas access. If those constraints do not match your desired output, capture the DOM with html2canvas or use a static-map service for a separately rendered image.
Or skip the browser setup
If you need a screenshot of a web page containing a Leaflet map rather than a purpose-built export of Leaflet layers, ScreenshotNeo can capture the page through one GET request. It removes cookie or consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. Its MCP server provides screenshot tools for AI agents, and its plans include 1,000 screenshots a month free with no card; paid plans start at $5 for 3,000.
For an endpoint request and available capture options, see the ScreenshotNeo documentation. The following cURL call saves the target page as WebP:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/map -o shot.webp
Replace https://example.com/map with your page URL and YOUR_API_KEY with your key. ScreenshotNeo is a page screenshot service, not a substitute for Leaflet’s own layer export when you need a controlled map-only image or have specific attribution and rendering requirements. Learn about ScreenshotNeo, then sign up for 1,000 free screenshots a month with no card.
Troubleshoot blank, missing, or unreadable exports
- The output is blank. Confirm the selected map element exists and has nonzero width and height. Wait for tiles, polygon data, and overlays to finish loading before capture. Leaflet Quick Start.
- Basemap tiles disappear. Check that each tile source allows cross-origin use. Set html2canvas
useCORSand Leaflet’s tile-layercrossOriginas appropriate, but also verify the server’s CORS response headers. If the server does not grant access, use a same-origin proxy or a provider that supports CORS. html2canvas FAQ. toDataURL()throws a security error. The canvas may be tainted by a cross-origin image that did not grant permission. Check all raster tiles and overlays, not only the first missing image. Route resources through a same-origin proxy or replace the non-CORS source. html2canvas explicitly cannot circumvent browser content-policy restrictions. html2canvas Getting Started.- Polygons are absent with leaflet-image. Ensure the path layers use Canvas with
renderer: L.canvas()or configure the map withpreferCanvas: true. Leaflet Canvas reference. - Controls or custom markers are missing. This is expected with
leaflet-imagefor HTML-based controls, legends, andL.divIconcontent. Usehtml2canvasif those visible DOM elements belong in the image. - The capture is cropped. Capture the actual map element rather than an unrelated wrapper. If the target extends beyond the viewport, inspect html2canvas’s
windowWidthandwindowHeightconfiguration and set them to the relevant scroll dimensions when needed. html2canvas configuration. - The PNG is too large or the browser runs out of memory. Reduce the capture area or explicitly lower
scale. High-DPI scaling increases pixel dimensions and memory demand; browser canvas limits vary, so there is no single safe maximum for every device. - A control appears in the export but should not. Add
data-html2canvas-ignoreto the unwanted element when using html2canvas. html2canvas configuration. - Attribution is absent. Include the attribution in the captured composition or add it to the final export as required by the provider’s terms. Check the licenses and attribution rules for the map and tile sources you use.
Quality, reliability, and cost considerations
Resolution versus canvas limits
Increasing scale improves pixel density but raises canvas dimensions and memory use. For a large full-page map or high-DPI output, test on the browsers and devices you support, and reduce the capture size or scale if export fails. html2canvas reconstructs the page from DOM and styles, so it is not guaranteed to reproduce every browser-rendered detail pixel for pixel. html2canvas documentation.
Reliability depends on the resources
Client-side exports depend on the map being ready, the browser having enough resources, and every external image being usable in a canvas. A missing tile can therefore be a CORS or loading issue rather than a polygon-rendering bug. For consistent automated output, a server-side or static-map workflow can avoid relying on an end user’s current browser state, but you still need to verify that the chosen service supports your desired styling, layers, and licensing terms.
Tile terms and attribution are part of the export
Do not assume that permission to display tiles on a web page automatically grants permission to redistribute them in exported images. Check the terms for the tile provider and any underlying map data, retain required attribution, and use a provider or proxy architecture that permits your intended export.
Frequently Asked Questions
Does html2canvas take a literal screenshot of the Leaflet map?
No. It reconstructs the DOM and styles into a canvas, so its output can differ from a pixel-for-pixel browser screenshot.
Can leaflet-image include Leaflet controls and divIcon markers?
No. Its documented export omits HTML-based controls, legends, and L.divIcon content.
Why does toDataURL fail even when useCORS is true?
The image server must grant cross-origin access. If any image in the canvas lacks permission, the canvas may be tainted and unreadable.
Quick Recap
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.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minute




