I am completely new to JS, I want to learn how to do the following:
Get the image data (convert it to an array of pixels so that I can edit it), and then return the edited array from the pixel editing function so that I can use these edited values to draw the edited image.
I'm not even sure that this can be done, but here is 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) {
arr.push(i - 10);
}
}
console.log(arr);
function drawEditedImage() {
var ctxEdited = canvasEdited.getContext('2d');
ctxEdited.drawImage(img, 0, 0);
}
};
Console output shows an array of c length:0.
Why doesn't it push the edited pixels up to arr?
source
share