Unexpected strokes are shown on P2D PGraphics

Hi~I tried to draw custom shapes(hexagon) on a transparent P2D PGraphics for later I would change the background color. I don’t want any strokes, but there are unexpected outlines of the shapes shown on the P2D PGraphic.
p
The buttom left hexagon is the shape which follows the mouse, others are drawn on the P2D PGraphics that slightly have outlines.
If I use normal PGraphics, there will be no strokes and that’s what I want. But I have to use P2D for some further reasons. I hope that someone can help me figuring out the problem. Here are the codes:

PShape hexagon;
PGraphics pg;

void setup(){
  size(400,400,P2D);
  smooth(8);
  
  hexagon = createShape();
  hexagon.setFill(color(239, 237, 220)); 
  hexagon.setStroke(false);
  hexagon.beginShape();
  for (int i = 0; i < 6; i++) {
    float angle = i * PI/3 + PI/6;
    hexagon.vertex(cos(angle) * 50, sin(angle)*50);
  }
  hexagon.endShape(CLOSE);
  
  pg = createGraphics(width,height,P2D);
  pg.smooth(8);
  pg.beginDraw();
  pg.noStroke();
  pg.endDraw();
}

void draw(){
  background(255);
  image(pg,0,0);
  shape(hexagon,mouseX,mouseY);
}

void mouseClicked(){
  pg.beginDraw();
  pg.shape(hexagon,mouseX,mouseY);
  pg.endDraw();
}
1 Like

I am not sure if this is the correct way or a hack, but I have found at times I have to make the stroke transparent with .setStroke(color(0,0)). Same with filling shapes too.

Hello,

I have had this issue before…

Try this:

With P2D or P3D renderer:

noSmooth();
size(400, 400, P2D);
pg = createGraphics(width, height, P2D);

With default renderer:

size(400, 400);
pg = createGraphics(width, height);

:)

1 Like

I’ve tried the way you said, as below:

void mouseClicked(){
  pg.beginDraw();
  hexagon.setStroke(color(0,0));
  pg.shape(hexagon,mouseX,mouseY);
  pg.endDraw();
}

unfortunately it doesn’t work :cry:

1 Like

Yes! It works! Thanks a lot ~~~ :smile:
I’ve edited as below:

pg = createGraphics(width,height,P2D);
pg.noSmooth();
3 Likes

Sorry, I did not look close enough, did not realize you were using PGraphics as well. Either way I would not have guessed that was due to smoothing. Glad you got it resolved.

Thanks for your help anyway. It’s surprising that I received your reply so soon :blush:

1 Like