I’m using a rectangle filled with white and a certain alpha value at the beginning of draw, and I would think that if done enough times that eventually the first thing drawn in setup would eventually be totally white. I’ve tested and it only becomes totally white if the alpha value is 128 or above.
For example, see if with this code the red circle ever becomes fully white:
And my main question would be since the lower alpha levels don’t add up enough to make full opacity how could I slow down the fade out without reducing frame rate? Using an alpha of over 127 is too fast.
Yeah, that is pretty good. It does switch it to thinking more in terms of animation instead of painting on the canvas. I need to think more about the consequences of that. (For example, if I use a lines alpha value to fade it I won’t be able to use it for how it relates to other lines.)
Here’s a version slightly modified from kll’s version above, which has randomised line fades (uses float instead of int for alpha and picks random alphaStep fade amount per line). I also removed the edge checking (as it looked better without), made 500 instead of 5 and changed the renderer to P2D as it runs faster when accelerated:
By default this is not the case – instead, in the default blendMode(), alpha pixels get a certain percentage of the way closer to the target color… the pixels never reach 100% value because, at a certain distance, the next step is so small that it drops below the rounding error. For discussion and test code, see:
You may use blendMode(ADD) to get the behavior you were expecting.
It adds 255 (*3/255) to something, so it adds 3 to the value each frame. What happens when you add 0 – or 0 (*3/255) to something? How many times do you need to add 0 to something before it changes…?
If you want to fade things to black against a black background, one method is to SUBTRACT a fraction of a white rectangle each frame.
Ok that makes sense now that you say it. I never really worked with blendModes so far.
It works fine in processing, but i still can’t seem to get it to work in p5, where the SUBTRACT blendMode seems to be only working with WEBGL which i have not much knowledge about but it seems to be only for 3D stuff?:
I haven’t tried in p5 – interesting fact about WEBGL. In general, attempting to wash a fading color repeatedly over the canvas can be an inflexible strategy as soon as you have elements that fade at different times and different ways – or don’t fade. The easiest thing to do if you are drawing a series of objects / segments and want fading trails to keep an array of drawn items and redraw them each frame, decreasing the opacity of each item in the list.
This approach is covered in-depth for p5 in:
The only change would be to make the tint of each trail point / segment dependent on its posiition in the for loop. Then the trails are fading.
There is a generalization of this to all canvas “objects.” Create one or more classes for your drawables (points, lines, shapes, sprites, whatever) and make sure that each one has a timeCreated. When you create the thing, stamp it with the current frameCount or millis() (depending on how you want sketch time to work). When you draw the thing, make it fade based on how old it is – by setting tint or its fill alpha etc. based on the difference between its timeCreated and the current time.
Yeah i already had a version of my sketch with arrays for the trails, but there were some drawbacks that led me to revert it to the workflow with background transparency.
Maybe i did something wrong, but the transparent circles overlapped kind of oddly, not the way they did with background transparency. Also the performance seemed to drop quite a bit. And i would’ve had create arraylists for every single item i wanted to draw, and there were a lot of them.
In the end i used a weird band-aid fix where i have a kind of “cleaner” object that crosses the screen once and “fixes” every pixel for the background transparency. The background is not completely black but im ok with that.
Working with multiple canvases and createGraphics() i use both objects with and withouth trails, so that works for me.