We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Should setAlpha() work as expected within a push/pop block? So far I've not had any success with this.
Modifying the example from the reference:
function setup() {
ellipse(0, 50, 33, 33); // Left circle
var c = color(204, 153, 0);
push(); // Start a new drawing state
strokeWeight(10);
c.setAlpha(100);
fill(c);
translate(50, 0);
ellipse(0, 50, 33, 33); // Middle circle
pop(); // Restore original state
fill(c);
ellipse(100, 50, 33, 33); // Right circle
}
The right circle fill(c) is also alpha adjusted, even though outside push/pop. Is this by design?
Answers
This depends on what you mean by "as expected". How do you expect this to work?
I would not expect colors to be affected by
push()
orpop()
. Those functions only affect transformations liketranslate()
androtate()
.from the reference:
Hmm interesting, guess I learned something new today!
But notice that the
color.setAlpha()
function is not listed in those functions.In Processing(Java),
pushMatrix()
andpushStyle()
are discrete -- in p5.js it looks like they are combined.When you push and pop, the states of the global variables controlled by fill(), stroke(), tint() etc. are pushed / popped.
But in your case, you created a variable --
var c
. That isn't a built-in Processing setting, it is your variable, and it isn't handled by push/pop.Instead: set the fill state, push, change the fill state, then use pop to access the previous fill state.
Something like this...?
Not that if you aren't drawing anything at all before you push then there is little point in using push.
I think I understand. Reassigning a variable changes its memory content regardless of setAlpha() or any other method, and push/pop has no bearing on that.
Thank you both for your time and your thoughts!