I am loading a black and white image from my computer on a web page and then drawing the image in a canvas. Then I want to retrieve the color of the first pixel. Here's the JavaScript function I am using:
function loadImage(){
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var img=document.getElementById("first");
ctx.drawImage(img,0,0);
var z1=ctx.getImageData(0, 0, 1, 1).data[0];
document.getElementById("1").value=z1;
};
I am getting the following error:
Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
Any ideas how I can achieve my requirement? This is part of a project I'm working on where I want to create a genetic algorithm that draws a given picture. An important part of this project is comparing the pixels from the initial image (loaded in a canvas) to a canvas populated by pseudo-random pixels. I then introduce small variance to the pixels to evolve it towards the initial image.
If I draw the canvas manually (using fillRect) I can retrieve the pixel data using 'getImageData'. However, when I draw the canvas using an image (drawImage), I get the above mentioned error.
Please help.
Edit: I've looked at: How to fix getImageData() error The canvas has been tainted by cross-origin data?
But this is not the case. I am loading the image from my own computer. I simply have the .html, .js and .png files in the same folder and run the .html file.