Atlaslog
Atlaslog / Field Notes / Tiles
Field note · Tiles

Exporting the map to an image throws 'canvas tainted'

Quick answer

The map displays correctly but the export button throws a SecurityError, or html2canvas returns a blank basemap area. The tiles were drawn without `crossOrigin`, or the tile server sends no `Access-Control-Allow-Origin` header.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet 1.9.x / html2canvas 1.4 / Chrome and Edge 90+
Symptom
Checklist
  1. Check the tile response for an `access-control-allow-origin` header
  2. Look for an `Origin` request header to confirm the request was a CORS request
  3. Load one tile into an `Image` with `crossOrigin='anonymous'` and watch `onload` versus `onerror`
  4. Confirm the export region contains no third-party images such as external icons in popups
  5. Decide between adding CORS headers and proxying same-origin
Fix

Set `crossOrigin: 'anonymous'` on the tile layer and add `Access-Control-Allow-Origin` for your site on the server, since both conditions are required; when the tile host cannot be changed, proxy it under your own domain and add the header there.

Code
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement':
Tainted canvases may not be exported.
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => console.log('可导出');
img.onerror = () => console.log('CORS 未通过,服务端缺少响应头');
img.src = 'https://tile.example.com/10/843/392.png';
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). Exporting the map to an image throws 'canvas tainted'. https://www.jingweizhi.com/en/cookbook/tile-cors-canvas.html (accessed 2026-11-03).