PGraphics weird transparency issue

Its weird, i cant explain it, so ill get straight to the point;

this code makes the edge of the line look dark and weird. it uses a translucent background (not pixelated, thats fine);

PGraphics pg;

void setup() {
   size(960,540,P2D);
   pg = createGraphics(64,64);
}

void draw() {
   background(#FFFFFF);
   pg.beginDraw();
   pg.stroke(#FF0000);
   pg.strokeWeight(15);
   pg.line(0,0,64,64);
   pg.endDraw();
   image(pg,0,0,540,540);
}

but this code makes the edge look fine, it is like the default color; it doesnt use a tranclucent background.

PGraphics pg;

void setup() {
   size(960,540,P2D);
   pg = createGraphics(64,64);
}

void draw() {
   pg.beginDraw();
   pg.background(#FFFFFF);
   pg.stroke(#FF0000);
   pg.strokeWeight(15);
   pg.line(0,0,64,64);
   pg.endDraw();
   image(pg,0,0,540,540);
}

does anybody have an idea to fix this?

Hi @Aryszin,

would you provide a screen shot on what you see ? On my box there is absolutely no difference except the background gets completely filled white on the first example instead not filled partially on the second one ?

Cheers
— mnse


yea sure here you go!

Ahh! Ok! Now I see what you mean. The reason for that is, that in the first example you draw to pg over and over again without clearing the old content, which has somehow an additive behaviour …
in the second example you clear the content, resp. fill white before drawing …

try this to see what’s ongoing …

PGraphics pg;

void setup() {
   size(960,540,P2D);
   pg = createGraphics(64,64);
}

void draw() {
   background(#FFFFFF);    
   pg.beginDraw();   
   if (mousePressed)
     pg.clear();
   pg.stroke(#FF0000);
   pg.strokeWeight(15);
   pg.line(0,0,64,64);
   pg.endDraw();
   image(pg,0,0,540,540);
}

Cheers
— mnse

1 Like

thanks, but what do i do if it is for a drawing programm?

Oh! I just noticed that if you are rendering the pgraphics in P2D (createGraphics(64,64,P2D)) its doesnt matter if you clear the pg…

no wait, the clear command does not work???

no wait, it does??? im very confused im sorry…

You are technically right, but by using this code, it still wont work. I need to render the pg using P2D cause its way faster! hope you have an answer

PGraphics pg;

void setup() {
   size(960,540,P2D);
   pg = createGraphics(64,64,P2D);
}

void draw() {
   background(#FFFFFF);    
   pg.beginDraw();   
   if (mousePressed)
     pg.clear();
   pg.stroke(#FF0000);
   pg.strokeWeight(15);
   pg.line(0,0,64,64);
   pg.endDraw();
   image(pg,0,0,540,540);
}

Hey @Aryszin do u need this

to be in draw() because the fact it’s being called repeatedly is the source of the bug.

Yes and it isn’t the source of the bug. Using P2D is tho.