I cannot reach an understanding of how 2 methods work with canvas: drawImage getImageData in fact, I would like to parse the bitmap image into an array of RGBA points, be able to convert it and display it using the putImageData method. But only one thing works: drawImage or getImageData. I ask for help - explain how these methods influence each other.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Canvas to array </title>
</head>
<body>
<canvas id="canvas" width="1200" height="600" />
<script>
"use strict"
let imageData;
let My_img = new Image();
My_img.src = 'test1.jpg';
let cnv = document.getElementById("canvas");
let ctx = cnv.getContext("2d");
My_img.onload = function() {
ctx.drawImage(My_img, 0, 0);
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (let i=0; i<imageData.data.length; i+=4){
imageData.data[i]=0;
imageData.data[i+1]=255;
imageData.data[i+2]=0;
imageData.data[i+3]=222;
}
ctx.putImageData(imageData,300,300);
};
</script>
</body>
</html>