dotPos(density, stageWidth, stageHeight) {
const imageData = this.ctx.getImageData(0, 0, stageWidth, stageHeight).data;
const imageData = this.ctx.getImageData(0, 0, stageWidth, stageHeight).data;
const particles = [];
let i = 0;
let width = 0;
let pixel;
for (let height = 0; height < stageHeight; height++) {
++i;
const slide = i % 2 === 0;
width = 0;
if (slide === 1) {
width += 6;
}
for (width; width < stageWidth; width += density) {
pixel = imageData[(width + height * stageWidth) * 4 - 1];
if (pixel !== 0 && width > 0 && width < stageWidth && height > 0 && height < stageHeight) {
particles.push({
x: width,
h: height,
});
}
}
}
I'm studying the code above but I don't know what the below line means:
pixel = imageData[(width + height * stageWidth) * 4 - 1];
please help me.
The ImageData's
datais an Uint8ClampedArray that represents the pixels of the ImageData linearly. Each item of this flat array represents the Red, Green, Blue, and Alpha value of every pixel.So to walk through each pixels, you actually need to jump 4 items at a time, hence the
* 4.Since this data is flat, to restore a cartesian model, you need to multiply the
yvalue by the width of the ImageData, and add thexvalue to this.So
(width + height * stageWidth) * 4corresponds to the point at coords{x: width, y: height}.The
-1would get the previous pixel's alpha channel, linearly: ifwidthis zero, then it will get the alpha from the last point on the previous row, which to be honest seems very weird since atwidth: 0height: 0then this would returnundefined.Usually we do
(x + y * width) * 4 + 3to get the alpha channel at the coordinate we are actually at in our iteration.Yes, they do actually avoid this
undefinedcase in the condition&& width > 0but this honestly sounds like a fluke and doesn't change the fact that this code doesn't smell very good.And this is not the weirdest thing happening here:
will always evaluate to either
trueorfalse, never anything else, so theif (slide === 1)condition will never match.If I were you, I wouldn't try to learn from this and search other examples instead.
And this is without considering the double declaration of the
imageDataconstant, which must be a copy-pasta error since this code would throw a SyntaxError with it.