PGraphics saving to GIF with transparent background

I’m, trying to save two gif images with transparent backgrounds. The program runs once and does not need to loop. See a reduced program below. There is no line in the 1st image while the 2nd image has the line.

Why is the first image not saving properly ?

PGraphics pg;

void settings() {
  size(600, 600, P2D);
}

void setup() {
  pg = createGraphics(600, 600, P2D);
  pg.beginDraw();
  pg.clear();
  pg.stroke(90);
  pg.line(50, 50, 550, 550);
  // do a whole lot more drawing
  pg.endDraw();
  pg.save("1.png");

  
  pg.beginDraw();
  pg.clear();
  pg.stroke(120);
  pg.line(550, 50, 50, 550);
  // do a whole lot more drawing
  pg.endDraw();
  pg.save("2.png");  
  
  println("done");
}
1 Like

Hello @hareti,

See these issues and workaround/fix:

From issue above:

  // ---- include to fix ----
  pg.beginDraw();
  pg.endDraw();
  // ------------------------

Your code saves the expected PNG files with the above added.

I have encountered this issue in the past:

:)

1 Like

Brill, thanks. That’s fixed it.