CORS issue with canvas only when image is preloaded

爱⌒轻易说出口 提交于 2019-12-02 10:54:34

The browser needs to know to check for CORS permissions when the HTTP request is made (i.e. to include an Origin header etc).

When you create a new Image object, it uses the cached data from the <img> element.

Add a crossorigin attribute to the existing <img> element or remove that <img> element entirely.

I found a better solution to my problem. @Quentin's solution is principally right, but it's just not practicable. I tried to update all <img>-tags, however in my page I have dozens of packages that are using the <img>-tag as well.

For example Leaflet. It's like Google Maps, but with Open Street Maps. You just don't have the control over to set attributes like crossorigin in custom icons.

So there had to be another solution. It feels bad to use it, but it does the job.

const src = new URL(imgUrl);
src.searchParams.append('cors', Date.now());
return src.href;

This code appends to your URL a query parameter and forces Chrome to reload the image without being cached.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!