canvas跨域图片污染

436 阅读1分钟

做canvas粒子动画的时候出现了这个错误 一开始以为是跨域出问题了 问了腾讯云客服才知道是跨域污染 canvas使用跨域的图片就会出现污染图片的响应了

解决办法

图片响应如果可以控制的话,利用 cors 跨域,并在图片请求发起时增加 crossOrigin = "Anonymous" 设置;否则只能自己的网站做个代理,让网站与图片同源。

```Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.\
    at caculate ([http://127.0.0.1](http://127.0.0.1/) :5500/.vscode/test.html:73:23)\
    at HTMLImageElement.img.onload ([http://127.0.0.1](http://127.0.0.1/) :5500/.vscode/test.html:59:16)\
caculate @ test.html:73\
img.onload @ test.html:59\
load (async)