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.
Symptom
- `Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.`
- html2canvas produces a fully white basemap while page text and buttons render normally
- A test `Image` with `crossOrigin = 'anonymous'` fires `onerror` for the tile URL
Checklist
- Check the tile response for an `access-control-allow-origin` header
- Look for an `Origin` request header to confirm the request was a CORS request
- Load one tile into an `Image` with `crossOrigin='anonymous'` and watch `onload` versus `onerror`
- Confirm the export region contains no third-party images such as external icons in popups
- 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
- Setting `crossOrigin` without server-side CORS, so the tiles fail to load and the map becomes grey blocks
- Blaming the map canvas when a third-party logo image inside a popup is the actual tainting source
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
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).