We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am doing a simple experiment, and failing.
I have a simple setup where a circle follows the mouse pointer, using the standard 'easing' technique. I keep redrawing the 'background' of the canvas while successively increasing the x & y coords of the circle, in each call of the 'draw' function. The background of the canvas has been set as 'red'.
Now, I simply want to make the background of the canvas disappear (ie, become transparent) after a certain number of seconds. The problem is, when I set the background as transparent ('rgba(255, 255, 255, 0)'), the circle instead of following the mouse, starts to draw itself over like a thick line. This is equivalent to not calling the 'background' function inside the 'draw' function.
Not sure what I am doing wrong here. PS - I don't want a white background, I want a transparent background.
Answers
The sketch widow background cannot be transparent it is always opaque.
Edit -- this was a Processing desktop application answer, but the question is actually a p5.js in-browser question -- see answers below
@pranjalchaubey -- re:
What did you want to be visible then? The desktop wallpaper image? Black or gray?
As @quark says, the Processing background does not support an alpha channel -- you can only work with transparent images in buffers which are then flattened when they are applied to the main drawing area.
If you set a full-alpha color and then apply it using
background()
this is like painting your house with a bucket of water instead of paint -- your house is still solid, and will stay the same color it was before rather than magically turning into glass.If you really want to see the desktop wallpaper image through your sketch then there is a hack using JFrame from Java Swing that can make your entire application window partially transparent. However, this will affect everything, including the background and all other drawing contents. See the experiment from @Bird and @GoToLoop here:
@quark @jeremydouglas: things are a little different with p5 ;)
IIRC you use clear() instead of background in draw() and you can set canvas background once in setup using rgba value to set it as transparent.
@pranjalchaubey This is what @blindfish suggested and it actually works.
Here is demo code -
Ignore my answer above, I missed the fact that it was about Javascript.
Thanks guys for pitching in!
@jeremydouglass @quark - I am thanking my stars that p5 is here, this would have been a lot more difficult in Processing.
@blindfish - Blimey! Why I couldn't think of that!!!
@blyk - Thanks for going that extra mile and putting up a full fledged example. Much appreciated.