color-thief icon indicating copy to clipboard operation
color-thief copied to clipboard

"The canvas has been tainted by cross-origin data" caused by img attribute order on some browsers

Open fernandomachado90 opened this issue 5 years ago • 11 comments

The crossOrigin attribute allows images that are loaded from external origins to be used in canvas like the one they were being loaded from the current origin. Using images without CORS approval taints the canvas. Once a canvas has been tainted, you can no longer pull data back out of the canvas. By loading the canvas from cross origin domain, you are tainting the canvas.

You can prevent this by setting crossorigin="anonymous".

However, CRAZILY enough, the order of the attribute on the img element does matter. I've been writing HTML since 2005 and this is the first time I found something like this. The crossorigin attribute must come before the src. On Chrome the order did not matter, but on Safari (and other mobile browsers) it solved the problem.

<img src="...image.jpg" crossorigin="anonymous" /> will result in Unhandled Rejection (SecurityError): The operation is insecure.

while <img crossorigin="anonymous" src="...image.jpg" /> works just fine.

Writing this down here so it can be added to the documentation and hopefully help someone in the future.

fernandomachado90 avatar Aug 17 '20 23:08 fernandomachado90

this also work within order of codes

const img = new Image();
img.src = '...image.jpg';
img.crossOrigin = 'anonymous';
// not working on some ios safari
const img = new Image();
img.crossOrigin = 'anonymous';
img.src = '...image.jpg';
// this code works

EB-Plum avatar Sep 23 '20 11:09 EB-Plum

What if i'm using it on an image url?

let currentImage = data[data.length - 1].data[i].image;

const fac = new FastAverageColor();
fac.getColorAsync(currentImage);

Where would the crossorigin="anonymous" go?

dmm22 avatar Feb 11 '21 00:02 dmm22

this is not working

ShvedDmytro avatar Aug 12 '21 14:08 ShvedDmytro

In my case, the canvas stops displaying the image if I add img.crossOrigin = 'anonymous'; And adding the line in my saveImage() function, just before the line imageToSave.src = canvas.current.toDataURL('image/png', 1.0) also does not work. I have setup cors json via AWS c-line on my server to accept all origins and headers.

zubin-madon avatar Mar 27 '22 17:03 zubin-madon

I solved the issue by using a dummy GET parameter in the src. <img crossOrigin="anonymous" src={`${url}?dummy=parameter`} /> (I'm using React) https://www.hacksoft.io/blog/handle-images-cors-error-in-chrome

ShahriarKh avatar Apr 30 '22 11:04 ShahriarKh

In my case, the canvas stops displaying the image if I add img.crossOrigin = 'anonymous'; And adding the line in my saveImage() function, just before the line imageToSave.src = canvas.current.toDataURL('image/png', 1.0) also does not work. I have setup cors json via AWS c-line on my server to accept all origins and headers.

SAME HERE. I tried the fix w/ the dummy parameter and it also didn't work.

armstmol01 avatar Jul 31 '22 05:07 armstmol01

i tryed all ways. It doesn't work for me

ALexanderMarginal avatar Aug 13 '22 10:08 ALexanderMarginal

What if i'm using it on an image url?

let currentImage = data[data.length - 1].data[i].image;

const fac = new FastAverageColor();
fac.getColorAsync(currentImage);

Where would the crossorigin="anonymous" go?

Same here

denvudd avatar Jun 07 '23 13:06 denvudd

for me, I have 2 issues relative to cors+canvas :

  • img.crossorigin = "anonymous" work (but image.crossOrigin = "anonymous" don't.. ), case matter
  • even with crossorigin set to anonymous, context.getImageData throw error : "Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data."

arilanto avatar Feb 23 '24 14:02 arilanto

If the image doesnt appears, you need to enter into the web using a local host connection. I use to try with XAMMP APACHE. By default, my image doesnt have changes if i wanna change his pixel estructure, somebody have a idea? :c

gardur-sa avatar Apr 06 '24 18:04 gardur-sa

Have encountered the same issue when trying to get color of the image located in my Roku TV with this URL http://192.168.1.11:8060/query/icon/12.

elshnkhll avatar Apr 13 '24 20:04 elshnkhll