简体   繁体   中英

accessing pixels[ ] array in p5.js (javascript)

I'm trying to loop through each pixel in the canvas, check and store its colour. If not white, (something happens), else, (nothing). There will be a nested loop, but I am confused with the 'idx' value, as written in the reference :

var d = pixelDensity;
  for (var i = 0; i < d; i++) {
    for (var j = 0; j < d; j++) {
      // loop over
      idx = 4*((y * d + j) * width * d + (x * d + i));
      pixels[idx] = r;
      pixels[idx+1] = g;
      pixels[idx+2] = b;
      pixels[idx+3] = a;
    }
  }

I guess it has to do with reading pixel matrix using (x,y) but I am not even sure it's necessary. I'm trying to make a target-based particle system, following this tutorial that uses text.

help?

This is my calculation:

index = 4 * ((this.y0 + j) * this.W + this.x0 + i)

Not sure about your pixeldensity. You are also squaring it.

I have noticed different behaviour on Windows and (ios+linux)

My code works on Windows only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM