Does loadPixels() take "fill()" function into account ?

Hi there, ready to look at a beginner's problem ?

I wrote (what i thought would be) a simple function : get (x, y)-point's color values :

function getPix(_x, _y) { loadPixels(); let off = (_x + _y * width) * pixelDensity() * 4; let result = [ pixels[off], pixels[off + 1], pixels[off + 2], pixels[off + 3] ]; return result; }

The function seems to work fine : console.log(off) sends back the right index, according to the "_x" & "_y" values, and i get the background's RGBA values.

Now, in Draw, i have an object displaying something. (say a blue square at 0, 0)

var col = color(0, 0, 255, 255); { fill(col); rect(0, 0, 10, 10); }

But if i am to call getPix(0, 0); again, i still get my background's color as a result : how comes ? My guess is : loadPixels() loads before my object is shown, but since i'm using this function for the first time, i surely have some misconceptions about the way it works.

The original code is slightly more complex, and i may give it if needed, but this exemple is still pretty much alike.

Thanks for any answer to come :)


  • Answer ✓

    Well, the problem came from a misconception indeed :

    Since my sketch includes a camera object, i assumed loadPixels() took the translate informations into account, and started a (0, 0) (god knows why).

    Adding my camera's X & Y displacement values to _x and _y solved it, obviously.

    Hope this could help someone, some day, somehow

  • Thank you for sharing your solution.

    loadPixels() took the translate informations into account

    Notice your first post doesn't mention translate. When you have a problem like this, it is recommended to provide an MCVE so to get better feedback.


  • No problem !

    True for the lack of MCVE : I'll provide one next time :)

Sign In or Register to comment.