一个前端操作canvas出现跨域问题的解决方案
出现现象:
前端项目中需要使用canvas对网络图片进行操作,
当使用到toDataUrl等操作就会报错:
SecurityError: the operation is insecure
Uncaught DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.
这个时候可能你会和我一样使劲百度谷歌找到一堆这样的回答:
设置了crossOrigin = 'anonymous
image.setAttribute('crossOrigin', 'anonymous');
- 把图片转成base64,用canvas的方法(这不是死循环?)
最后使用了以下方法实现: