Each pixel uses 4 bytes in the imageData array (r, g, b, a). So here is how to get pixel data at row:x, column:y
> function getColorAtPixel(imageData, x, y) {
const {width, data} = imageData
return {
r: data[4 * (width * y + x) + 0],
g: data[4 * (width * y + x) + 1],
b: data[4 * (width * y + x) + 2],
a: data[4 * (width * y + x) + 3]
}
}
Here's is my code but it won't get a rectangular area that I want for some reason:
function getMainPartCoordinates (i, runtime, maskData, referenceData, pieceData) {
var mData = maskData.data;
var pData = pieceData.data;
var rData = referenceData.data;
var rowsAndColumns = Math.sqrt (runtime.globalVars.NumberOfPieces);
for (var j = i % rowsAndColumns * 117; j < i % rowsAndColumns * 117 + 117; j++){
for (var k= i % rowsAndColumns * 117; k < i % rowsAndColumns * 117 + 117; k++) {
if (getColorAtPixel (mData, k, j).a == 0)
setColorAtPixel (pData, rData, k, j);
else
break;
}
}
piecesDC[i].loadImagePixelData(pieceData);
}
Is there something wrong with this cause I get whole rows and I just want a rectangular area. :D