My intention is to create a fade effect for thousands of shapes on the canvas (They appear, and then fade into blackness).
This is the solution I came up with, and have found other people using.
The problem is that the shapes never fade away completely. The lower the alpha (slower the fade), the brighter the ghost that is left behind. Here’s a sample sketch that illustrates my problem. Click to mouse to create more ellipses.
Can anybody explain to me why the objects stop fading?
Really??
That’s super interesting. I just tried on openprocessing.org and still have the same problem. Though I just noticed now that it’s way more apparent on my hdmi monitor than my laptop’s, so maybe the screen has something to do with that.
This is an image of the issue, if you can’t see the dots, than it must me a monitor thing. I really hope someone can help me with this, I have no idea how to troubleshoot it further.
I can see the circle in the black; you can see this effect better if you invert colors.
I wrote a snippet of code to plot the color over time; you can see the values in the console.
Thanks for taking the time to put that together @glv It’s a helpful visualization of what’s going on, and after some playing around I found that the fill’s opacity needs to be above 125ish for the get() command to return pure white, which gives me a better understanding of what’s going on.
It occurred to me to see if the issue happens in Photoshop as well, which it does. So this must be an inherent math thing rather than a processing thing. I think I’ll try experimenting with blend modes over opacity when I have time later. I have a feeling the solution’s in there somewhere.
Do you need to reset the blendMode to something else after drawing the rect over the screen? When I use blendMode(MULTIPLY) as shown, nothing else I draw in the program appears on the screen.
@philbutrin yes, you’ll need to change it back to default. blendMode(BLEND) will do it. You can also use the push/pop methods to revert back to previous settings
The reason that this happens with repeated use of blendMode(BLEND) is, in a nutshell, Zeno’s paradox. See prior discussion with example test sketches and some digging into the math here:
@jeremydouglass Thanks for taking the time to share the math, though I can’t say I understand it. I was under the impression that adding say, pure white with 5 alpha meant that you were adding 5/255 to the canvas until the whole thing became white. If you’re willing to to explain what’s actually going on here I’m super interested in hearing it, but if not I’m at least getting the effect I’m looking for even if I don’t understand what’s going on.
That would be true in blendMode(ADD). Then you add 5.
But with blendMode(BLEND) you get dest * (1 - srcAlpha) + src * (srcAlpha).
fill(0, 10) is 10/255 = ~0.039.
So it leaves 96% of a canvas pixel value each time it is applied.
96% of 13 is 12.46, which is saved as an int =12. (canvas pixels are ints). BUT:
96% of 12 is 11.52… which is 12. So a pixel that is at 12, when covered with fill(0, 10), will stay 12. And then 12. And then 12…
Watch it happen in this example sketch – it shrinks to 12, then stays there:
float dest = 255; // white canvas pixel
int src = 0; // black fill pixel
float srcAlpha = 10.0/255; // black fill alpha channel
for(int i = 0; i<100; i++) {
print(dest, " | ");
float result = dest * (1 - srcAlpha) + src * (srcAlpha); // blendMode(BLEND)
dest = round(result); // results saved as int in original pixel
}
12 is the specific resulting value for 10/255, but for different inputs you get different final values (towards white or black) where the final value gets stuck before reaching 0 (or 255).