I'm completely new to JS, I want to learn how to do the following:
Get an image data (convert it into an array of pixels so I could edit it) and then return the edited array back from the pixels editing function so that I could use these edited values to draw an edited image.
I'm not even sure if that's the way to go about this, but here's what I got so far:
var img = new Image();
img.src = 'img.png';
var canvas = document.getElementById('canvas');
var canvasEdited = document.getElementById('canvasEdited');
var ctx = canvas.getContext('2d');
var arr = [];
img.onload = function() {
ctx.drawImage(img, 0, 0);
function editPixels(ctx) {
for (i of ctx) {
// edit pixels values
arr.push(i - 10);
}
}
console.log(arr);
function drawEditedImage() {
var ctxEdited = canvasEdited.getContext('2d');
ctxEdited.drawImage(img, 0, 0);
}
};
Console output shows an array with length:0
.
Why doesn't it pushes edited pixels to arr
?