I am currently working my way through the Canvas tutorial on MDN.
Everything was going smoothly until I reached the Pixel manipulation, specifically a color picker example.
However, when using getImageData, I encountered this error:
MDNTutorialBaseppp.html:21 Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data. at HTMLCanvasElement.pick
Below is the code that I have been running:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
<script>
var img = new Image();
//img.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';
img.src = 'photo2.jpg';
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
img.onload = function() {
ctx.drawImage(img, 0, 0);
img.style.display = 'none';
};
var color = document.getElementById('color');
function pick(event) {
var x = event.layerX;
var y = event.layerY;
var pixel = ctx.getImageData(x, y, 1, 1);
var data = pixel.data;
var rgba = 'rgba(' + data[0] + ', ' + data[1] +
', ' + data[2] + ', ' + (data[3] / 255) + ')';
color.style.background = rgba;
color.textContent = rgba;
}
canvas.addEventListener('mousemove', pick);
</script>
</body>
</html>
I've been stuck on this issue for hours and haven't been able to figure out what's wrong. Can anyone help identify where I'm going wrong?