在使用 canvasimgsrc 地址转换为 base64 调用 canvas.toDataURL() 时,遇到跨域问题并报错如下

Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. at img.onload

67571-rse1if4af1b.png

解决方案:

在 onload 之前添加 crossOrigin 参数

 var img = new Image();
 img.src = src;
 img.crossOrigin = '';
 img.onload = function () {
   // todo
 }

具体参考大佬方案