Export Map as Image
Capture the current map view as a downloadable image. Each library takes its own approach, compared side by side below.
Quick recommendations
Leaflet: Makes the most sense when you need to export a raster tile map and are willing to use html2canvas to capture the DOM-rendered map container.
MapLibre GL JS: A strong fit when you need direct canvas access via preserveDrawingBuffer for clean WebGL canvas export without external libraries.
OpenLayers: Best when you need canvas-based export with precise control over the rendered output via the map's renderSync and canvas API.
MapTiler SDK: Delivers MapLibre's canvas export for MapTiler SDK users, with automatic MapTiler style and attribution handling.
At a glance
| Leaflet | MapLibre | OpenLayers | SDK | |
|---|---|---|---|---|
| Coordinates | [lat, lng] | [lng, lat] | fromLonLat([lng, lat]) | [lng, lat] |
| Rendering | DOM + Canvas (2D) | WebGL (GPU, 60fps) | Canvas + WebGL | WebGL (GPU, 60fps) |
| Bundle size | ~40 KB gzipped | ~300 KB gzipped | ~90 KB gzipped (tree-shakeable) | ~360 KB gzipped (includes MapLibre) |
| Learning curve | Easiest (simple API, great tutorials) | Moderate (source/layer model, expressions) | Steepest (projections, OGC standards, verbose API) | Easy (MapLibre API with convenience helpers) |
| 3D support | No native 3D (2D only) | Full: terrain, fill-extrusion, globe, sky | Limited: WebGL layers, no native globe | Full (same as MapLibre) |
| Code lines | 14 | 15 | 19 | 15 |
Code snippets
Click a library to expand its code. Each snippet shows only the core logic: the scaffold handles HTML boilerplate.
Leaflet 14 lines Show code
const map = L.map('map').setView([48.8566, 2.3522], 13);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
L.marker([48.8584, 2.2945]).addTo(map).bindPopup('Eiffel Tower');
document.getElementById('export-btn').addEventListener('click', () => {
html2canvas(document.getElementById('map'), { useCORS: true }).then(canvas => {
const link = document.createElement('a');
link.download = 'map-export.png';
link.href = canvas.toDataURL();
link.click();
});
}); MapLibre GL JS 15 lines Show code
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 13,
preserveDrawingBuffer: true
});
map.addControl(new maplibregl.NavigationControl());
document.getElementById('export-btn').addEventListener('click', () => {
const canvas = map.getCanvas();
const link = document.createElement('a');
link.download = 'map-export.png';
link.href = canvas.toDataURL('image/png');
link.click();
}); OpenLayers 19 lines Show code
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 13 })
});
function exportMap() {
map.once('rendercomplete', function() {
var canvas = document.querySelector('#map canvas');
if (canvas) {
var link = document.createElement('a');
link.download = 'map-export.png';
link.href = canvas.toDataURL('image/png');
link.click();
}
});
map.renderSync();
} MapTiler SDK 15 lines Show code
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
container: 'map',
style: maptilersdk.MapStyle.STREETS,
center: [2.3522, 48.8566],
zoom: 13,
preserveDrawingBuffer: true
});
window.exportMap = () => {
const canvas = map.getCanvas();
const link = document.createElement('a');
link.download = 'map-export.png';
link.href = canvas.toDataURL('image/png');
link.click();
}; Things to know
Implementation-specific details that matter for export map as image.
Leaflet's DOM-based rendering requires html2canvas (external library) to capture tiles; CORS headers on the tile server must allow canvas export.
Set preserveDrawingBuffer: true in the Map constructor to enable canvas.toDataURL(); this has a minor performance cost.
Export uses map.renderSync() followed by canvas.toDataURL(); works reliably with both raster and vector renderers.
Same preserveDrawingBuffer approach as MapLibre; the SDK's canvas export method works identically.
Which library to use?
Export the current Leaflet map view as a PNG by rendering the map container with html2canvas() and saving it with canvas.toDataURL().
Makes the most sense when you need to export a raster tile map and are willing to use html2canvas to capture the DOM-rendered map container.
Export a MapLibre GL JS map as a PNG by enabling preserveDrawingBuffer and passing map.getCanvas().toDataURL() to a download link.
A strong fit when you need direct canvas access via preserveDrawingBuffer for clean WebGL canvas export without external libraries.
Export an OpenLayers map to PNG by waiting for the rendercomplete event, calling renderSync(), and reading the canvas with toDataURL().
Best when you need canvas-based export with precise control over the rendered output via the map's renderSync and canvas API.
Export a MapTiler SDK map view to PNG by enabling preserveDrawingBuffer and calling toDataURL() on the canvas returned by map.getCanvas().
Delivers MapLibre's canvas export for MapTiler SDK users, with automatic MapTiler style and attribution handling.
Build maps with AI
Generate code like these examples using mapping library skills in your IDE.