As soon as a p5.Graphics element gets stored in a p5.Image element, it gets a bit fuzzier. Some smoother anti alias I think.
The effect is subtle, but visible to me.
Is there a way to prevent this?
I’m trying to make a small drawing app which I can use to try out all the brushes I might make with p5. I wrote an undo function which uses p5.Images to store previous states of the image. I wish those states could be stored in a lossless format, is that possible?
Or is there a way to do what I did by copying p5.Graphics objects? I haven’t yet worked out how…
My current version: https://editor.p5js.org/hapiel/sketches/rROK3rm_F
I originally used get() instead of myImg.copy, it is even still in the file (see line 118). So line 112 to 116 can be replaced with line 120. But then I copy everything that is on screen, and I want to only copy a single layer, which is layerDraw or stored in my object as layer.
I tried layer.get() but that doesn’t seem to work?
EDIT: Of course that didn’t work, because as usual, I forgot the keyword ‘this’. But now that it does work (locally, haven’t updated the editor file), it still produces fuzzy images…
EDIT2: also using get() I can’t reuse my old ‘add background’ system (112 - 115), so I would have to rewrite a bit if I want to use get I guess?
I just tried to replace my copy function with pixels. It makes the code a bit longer, but it kinda works
this.layer.loadPixels();
this.img[this.img.length - 1].loadPixels();
for (let i = 0; i < this.layer.pixels.length; i++) {
this.img[this.img.length - 1].pixels[i] = this.layer.pixels[i];
}
// this.img[this.img.length - 1].pixels = this.layer.pixels;
this.img[this.img.length - 1].updatePixels();
Why does the for loop work, but the simple line that I commented out does not? Don’t they do the same thing?
EDIT: I just discovered that I have to clone an array by saying array1 = [...array2]; or array1 = array2.splice();, but neither work here too… this.img[this.img.length - 1].pixels = [...this.layer.pixels];
Anyway, I can’t add a background to this, because the way I add the background is by adding it to a picture, and then copying the drawing on top. But the whole copying function is what gives the fuzzyness…
I’m still confused as to how to solve this. Perhaps I can add the background in another place (and then double check if this pixel swapping actually solves the fuzzyness )
EDIT: found a place to enter the background. All works now, except that I have this rather wasteful for loop in my code…
Thanks for all the info, you’ve been very helpful.
In the end, I just discovered that after all the modifications I made, and introducing a clear() before displaying the new content on screen, I got rid of the fuzzy lines too. Turns out I could use get() after all, and never needed to use anything as complex as copying the pixel data.
Silly me for looking in the wrong place, but at least it is now al working smooth! And with the get() replacing the for loop it can handle waaaay larger canvasses!