开发者

Cross-origin image loading and manipulating

开发者 https://www.devze.com 2023-04-10 10:58 出处:网络
I wrote a piece of javascript, stored on domain A, which is using CORS to enable me to embed it on a page from domain B yet still read data from domain A. I would also like it to fetch images from dom

I wrote a piece of javascript, stored on domain A, which is using CORS to enable me to embed it on a page from domain B yet still read data from domain A. I would also like it to fetch images from domain A and manipulate the pixel data using the html canvas. Normally, canvas.getImageData throws a security exception because I am not allowed to read the pixel data. Chrome allows me to do it (and supports the Image.crossOr开发者_JAVA技巧igin attribute). However, Firefox and various other browsers do not.

Is there a workaround to load and manipulate the pixel data of an image from domain A in a script which is loaded from domain B?


No, you just have to wait for other browsers to ship support for the crossorigin attribute. In the case of Firefox that means waiting about a month. For others, who knows...

Though actually... you could use cross-site XHR (which is somewhat better supported) to fetch the image data, build a data: URI from it, set an image's src to that data: URI, and then draw that image to the canvas. That might work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号