My question might be stupid, but I'm stuck with a simple task.
I'm writing a web browser game where I load images from a local folder to a 2d canvas, then, at some time, redraw it with getImageData()
.
The code sample is the following
let img = new Image()
// img.crossOrigin = "anonymous"
img.src = "assets/hidden.png"
let ctx = document.getElementById("canvas").getContext("2d")
img.onload = () => {
console.log("loaded img", img.src)
ctx.drawImage(img, 0, 0)
let c = ctx.getImageData(0, 0, 10, 10)
}
The original problem was that calling getImageData()
causes Uncaught DOMException: The operation is insecure.
So I added img.crossOrigin = "anonymous"
(context.getImageData() operation is insecure) to overcome this, but now the image is not loaded at all. How do I normally use images in canvas?