I am trying to write some code that combines two images into a resultant image and display the combined data in a canvas element. I have two images loaded on the page from my local machine. I then create two new canvas elements to draw these images onto and then take the data from those elements for combination into the canvas on the page itself.
However, I am getting this error:
SecurityError: The operation is insecure.
I have looked at a few questions here on SO, such as SecurityError: The operation is insecure. using Htmlcanvas, but I can't see why this is happening when everything is on my local machine. There's no CORS request being made that I'm aware of.
Here's my code so far that isn't working as I expect:
<!DOCTYPE html>
<html>
<head>
<title>Image Mix Test</title>
<meta charset="UTF-8">
<script>
function init(event) {
console.log('init');
var img1 = document.getElementById('img1');
var img2 = document.getElementById('img2');
var canvas1 = document.createElement('canvas');
var canvas2 = document.createElement('canvas');
canvas1.width = img1.width;
canvas1.height = img1.height;
canvas2.width = img2.width;
canvas2.height = img2.height;
var ctx1 = canvas1.getContext('2d');
var ctx2 = canvas2.getContext('2d');
ctx1.drawImage(img1, 0, 0, ctx1.canvas.width, ctx1.canvas.height);
ctx2.drawImage(img2, 0, 0, ctx2.canvas.width, ctx2.canvas.height);
console.log(ctx1);
console.log(ctx2);
// *** The error is thrown on this next line... ***
var data1 = ctx1.getImageData(0, 0, ctx1.canvas.width, ctx1.canvas.height);
var data2 = ctx2.getImageData(0, 0, ctx2.canvas.width, ctx2.canvas.height);
// TODO: write code that combines image data.
}
window.addEventListener('load', init, false);
</script>
</head>
<body>
<img id="img1" src="../default/assets/images/Tile01.jpg" style="display:block;"><br>
<img id="img2" src="../default/assets/images/Tile03.jpg" style="display:block;"><br>
<canvas id="canvas"></canvas>
</body>
</html>
Any thoughts on why this is happening and how to correct it?