MapSnippets logo MapSnippets logo

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
Full example
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: '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <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
Full example
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
Full example
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: '&copy; <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
Full example
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

Leaflet's DOM-based rendering requires html2canvas (external library) to capture tiles; CORS headers on the tile server must allow canvas export.

MapLibre GL JS

Set preserveDrawingBuffer: true in the Map constructor to enable canvas.toDataURL(); this has a minor performance cost.

OpenLayers

Export uses map.renderSync() followed by canvas.toDataURL(); works reliably with both raster and vector renderers.

MapTiler SDK

Same preserveDrawingBuffer approach as MapLibre; the SDK's canvas export method works identically.

Which library to use?

Build maps with AI

Generate code like these examples using mapping library skills in your IDE.