PGraphics transparency and blending mode
in
Programming Questions
•
2 years ago
Hi,
I think it's pretty simple but my brain is foggy today.
What i'd like is to draw the three circles on screen blending in each other using the ADD mode.
Actually i switched this sketch to noLoop() in order to show what i want to visually achieve.
If you comment noLoop() function you'll see the behavior i'm trying to achieve, but it isn't visually correct (each frame add the content on the top of the previous one). I'd like to keep the halo transparency.
I think i need to add something in the render mothod of the haloClass (smth like pg.background(0)).
But if i add pg.background(0) i don't get the transparency.
I tried width pg.rect(0, 0, width, height) filling it with a low alpha buy doesn't look like so nice.
Sorry if this confusing, here's the source :
I think it's pretty simple but my brain is foggy today.
What i'd like is to draw the three circles on screen blending in each other using the ADD mode.
Actually i switched this sketch to noLoop() in order to show what i want to visually achieve.
If you comment noLoop() function you'll see the behavior i'm trying to achieve, but it isn't visually correct (each frame add the content on the top of the previous one). I'd like to keep the halo transparency.
I think i need to add something in the render mothod of the haloClass (smth like pg.background(0)).
But if i add pg.background(0) i don't get the transparency.
I tried width pg.rect(0, 0, width, height) filling it with a low alpha buy doesn't look like so nice.
Sorry if this confusing, here's the source :
- ArrayList hcs = new ArrayList<haloCircle>();
- void setup() {
- size(400, 400);
- smooth();
- imageMode(CENTER);
- hcs.add(new haloCircle(180, new PVector(20, 40)));
- hcs.add(new haloCircle(200, new PVector(0, 0)));
- hcs.add(new haloCircle(100, new PVector(0, 0)));
- noLoop();
- }
- void draw() {
- background(0);
- translate(width/2, height/2);
- for (int i = hcs.size() - 1; i >= 0; i--) {
- haloCircle hc = (haloCircle)hcs.get(i);
- hc.update();
- hc.render();
- if(hc.isFinished())hcs.remove(i);
- }
- }
- class haloCircle {
- PGraphics pg;
- PVector pos;
- float radius;
- float haloWidth;
- boolean finished;
- haloCircle()
- {
- pg = createGraphics(width, height, JAVA2D);
- pos = new PVector(0, 0);
- radius = 80;
- haloWidth = 40;
- finished = false;
- }
- haloCircle(float w_, PVector pos_)
- {
- pg = createGraphics(width, height, JAVA2D);
- pos = pos_;
- radius = 80;
- haloWidth = 40;
- radius = w_ / 2;
- finished = false;
- }
- void update()
- {
- if (radius > 0) {
- radius--;
- }
- else {
- finished = true;
- }
- }
- void render()
- {
- pg.beginDraw();
- // There's must be some kind of magic there
- pg.translate(pg.width/2, pg.height/2);
- pg.smooth();
- pg.noFill();
- for (int i = 0; i < haloWidth; i++) {
- pg.stroke(255, haloWidth - i);
- pg.ellipse(pos.x, pos.y, (radius * 2) + i, (radius * 2) + i);
- }
- pg.endDraw();
- image(pg, 0, 0);
- }
- boolean isFinished()
- {
- return finished;
- }
- }
1