Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getImageData cross-origin error

Tags:

People also ask

Does the canvas has been tainted by cross origin data?

As soon as you draw into a canvas any data that was loaded from another origin without CORS approval, the canvas becomes tainted. A tainted canvas is one which is no longer considered secure, and any attempts to retrieve image data back from the canvas will cause an exception to be thrown.

How do you allow cross origin use of images and canvas?

var canvas = document. createElement("canvas"); var ctx = canvas. getContext("2d"); var img = new Image(); img. crossOrigin = "anonymous"; img.

What is Crossorigin in image?

The crossorigin attribute on an <img> tag specifies that CORS is supported when loading an image from a third party server or domain.

Which property of the image tag allow images from third party sites that allow cross origin access to be reused with canvas?

The crossorigin attribute is a CORS settings attribute. Its purpose is to allow images from third-party sites that allow cross-origin access to be used with canvas .


Precursor: I know there have been a few questions already asked about this topic, but none of them seem to offer a straight JavaScript only solution.

So I ran into this error where I am trying to get the pixel data from a canvas using something like context.getImageData(), my exact code can been seen here or below:

<canvas id="imageCanvas" width="600" height="800"></canvas>
// Load Image
var canvas = document.getElementById('imageCanvas');

var image = new Image();
image.src = 'http://emoticons.pw/emoticons/emoticon-faces-002-medium.png';
image.onload = function() {

    width=image.width;
    height=image.height;

    var context = canvas.getContext('2d');
    context.fillStyle="#024359"; // canvas background color
    context.fillRect(0, 0, width, height);
    context.drawImage(this,0,0);

    imageData = context.getImageData(0,0,width, height); // PROBLEM HERE

    context.putImageData(imageData, 0, 0);
}

I get the following errors in Chrome:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data.

and then a security error as well. I don't want to make server changes or start chrome with a weird instruction thing. I feel like there has to be something I can do in JavaScript.

Using local images only is not a problem, but when trying that, I got the same error!

I am trying to do this without a server, if I put this on my "default" godaddy web server, are all my problems solved? I heard rumors dropbox could also simulate a server close enough?