-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Open
Description
Most appropriate sub-area of p5.js?
- Accessibility (Web Accessibility)Build tools and processesColorCore/Environment/RenderingDataDOMEventsFriendly error systemImageIO (Input/Output)LocalizationMathUnit TestingTypographyUtilitiesWebGLOther (specify if possible)To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Details about the bug:
- p5.js version: 1.2.0
- Web browser and version: Firefox 84.0.2 (should apply to all supported browsers)
- Operating System: Linux Pop_OS 20.10
- Steps to reproduce this:
Visit this example sketch. The initial expected behaviour is a blank canvas because the img.pixels
array has been assigned with the .map
function to be all 0 but the image is still drawn. Replacing the .map
with a for
loop works as expected.
Filing this as a bug because this behaviour is different from the canvas itself (see by uncommenting code at the end of the draw()
function), which reassigning the pixels
array with .map
still works.
The problem seems to be outdated variable referencing in p5.Image, possibly affecting p5.Graphics as well but I didn't test.
Activity
limzykenneth commentedon Jan 13, 2021
Actually it seems to affect the canvas's pixels array as well.
rt1301 commentedon Jan 14, 2021
@limzykenneth I would like to work on this. Can you suggest me how to get started with it?
limzykenneth commentedon Jan 14, 2021
This one is potentially a bit complicated and it still need to be determined as an actual bug first, ie. the behaviour is something that should be fixed and not something that's intended. To fix this will require looking at how
updatePixels()
is getting the pixels array to update in the relevant canvas image data array.[-]"pixels" array of p5.Image cannot be reassigned while canvas one can[/-][+]"pixels" array of p5.Image cannot be reassigned[/+][-]"pixels" array of p5.Image cannot be reassigned[/-][+]"pixels" array cannot be reassigned[/+]stalgiag commentedon Jan 19, 2021
When I test on Mac OS 10.15.7 with Firefox 84.0.2 the image is transparent with the
.map
modification of its pixels. For example in this slight modification of your example, I only see blue. Am I missing something?limzykenneth commentedon Jan 20, 2021
@stalgiag If I change the second to last line in your modified sketch and instead of mapping all values to 0, map all to 255, I still only see blue where I'm expecting a white square on blue background.
stalgiag commentedon Jan 20, 2021
Definitely should be a white square and yes it seems to happen on both the canvas and the image when using
map
.limzykenneth commentedon Jan 20, 2021
My theory is that
pixels
is setup to be a reference to the underlying image data array and by reassigning it the reference is replaced andupdatePixels()
is still looking at the underlying image data instead of thepixels
variable. I haven't look much into the code base so can't say much about what the fix could be though.ahujadivyam commentedon Jan 30, 2021
@limzykenneth Yes, this seems to be the case.
pixels is just reference to imageData data and reassigning it will just reassign the reference not change the actual data.
p5.js/src/core/p5.Renderer2D.js
Line 292 in f2f8bb1
p5.js/src/core/p5.Renderer2D.js
Line 395 in f2f8bb1
amanMahendroo commentedon Feb 23, 2021
Hi. This does not seem to be a bug in the code. The reason that the pixel update doesn't work is that the img.pixels array is a UInt8ClampedArray and not a regular javascript array. Hence it doesn't have the
Array.prototype.map()
function associated with it. You can update the image's pixels by using a for-loop or by using the set() function in the p5.js library.The blue screen that appears on using the for loop seems to be a bug in the p5.js web editor as this doesn't seem to happen in other editors or in the browser.
Hope this helped 😊
limzykenneth commentedon Feb 23, 2021
UInt8ClampedArray
does have amap
method but that is not the key for this issue. Even not usingmap()
(which is my original use case but it's too complicated to share), I can create a newUint8ClampedArray
say callednewPixels
, if I assignpixels = newPixels
,updatePixels()
will not populate the canvas with the values I set innewPixels
.ahujadivyam commentedon Feb 23, 2021
@limzykenneth As I mentioned in above comment #4993 (comment) , I think pixels (property / variable ) is just a reference to image data array which renderer is using internally and updating using that, and reassigning pixels array with map() or something else just makes pixels reference to other array and internal image data array is not getting affected
limzykenneth commentedon Feb 23, 2021
@divyamahuja Yes, that's what I was saying.
19 remaining items