lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.62k stars 1.32k forks source link

I simply cannot get this to work, no matter the source of the image (The canvas has been tainted by cross-origin data) #128

Closed teenangst closed 5 years ago

teenangst commented 7 years ago

No matter if the image is in the same place as the javascript and html - I get a an error

color-thief.min.js:18 Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.

I am using this which seems to work for other people but does not work for me:

var image = document.querySelector('#img');
image.onload = function(){
    var colorThief = new ColorThief();
    var color = colorThief.getColor(image); 
   console.log(color);
};

What do I have to do to make this work at all?

teenangst commented 7 years ago

Update, just built a php ajax api for this which will be more stable and faster anyway; this is no longer urgent.

danielehrhardt commented 7 years ago

Can you send you code?

moizmb commented 7 years ago

@teenangst Could you post a snippet of the code that worked for you

danielehrhardt commented 7 years ago

@moizmb do it simply like this :)

var img = document.createElement('img');
    img.setAttribute('crossOrigin', '');
    img.setAttribute('src', this.data.src.small)
    img.addEventListener('load', () => {
        var vibrant = new Vibrant(img);
        var swatches = vibrant.swatches()
        for (var swatch in swatches)
            if (swatches.hasOwnProperty(swatch) && swatches[swatch])
                this.colorPalette.push({hex: swatches[swatch].getHex()})
    });
shuiRong commented 7 years ago

I have the same error.(not on a http-server) It's ok , if i use http-server to run theindex.html.

AlfredJKwack commented 5 years ago

Ahem,

Browsers these days don't like you loading stuff straight from the filesystem. Here's a good explanation of CORS if you want to know more.

Try this or your local equivalent instead:

[me@macOs]$ cd ~/GitHubRepos/color-thief
[me@macOs]$ python -m SimpleHTTPServer 8000
[me@macOs]$ open http://localhost:8000

Good luck.

th0rgall commented 5 years ago

I had some success by using this StackOverflow answer and modifying the color-thief code on a fork to set the following property after it initializes an.

Insert after https://github.com/null2/color-thief/blob/master/js/color-thief.js#L55

img.crossOrigin = 'anonymous';

It works but I need to invoke the getColor() action a 2 times for it to work, and errors always occur the first time.

AlfredJKwack commented 5 years ago

@th0rgall

Not sure if that would work with data urls.


if ( img.substring(0,5) !== 'data:' )
      img.crossOrigin = "Anonymous";
AntiMoron commented 5 years ago

Same here.

lokesh commented 5 years ago

Closing out the issue. I've added info on CORs issues to the demo page and fleshed out the docs: https://lokeshdhakar.com/projects/color-thief/#faq

If you are having trouble using the script still, check out the instructions on getting support: https://lokeshdhakar.com/projects/color-thief/#support