html2canvas导出openlayer地图的时候跨域报错:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
html2canvas(map.getViewport(), {
useCORS: true,
allowTaint: true,
}).then(canvas => {
let dataURL = canvas.toDataURL("image/png");
printJS({ printable: dataURL, type: 'image', documentTitle: projectConfig.title }) //printJS打印
});
allowTaint属性是是否允许跨域图片污染画布,默认是false,如果设置为true,解决了导出报错问题,但是导出的图片是空白。
所以还的从源头上解决跨域。openlayer在source下设置 crossOrigin: "anonymous",即可