clear() PGraphic's function doesn't works fine with P2D renderer

edited January 2018 in Questions about Code


I work on a sketch where layers without colored background coexist and are refreshed each frame thanks to the clear() function. The basic g layer is only used for cleaning the frame. This is in the idea of export the PGraphics image and use them in another software, that why I want them with transparent pixels and no background.

But I had a surprising issue, and code example is better than words to explain it :

PGraphics pg;

void setup() {
  size(100, 100);
  pg = createGraphics(width, height); 

void draw() {
  pg.ellipse(mouseX, mouseY, 40, 40);
  image(pg, 0, 0);

This sketch does perfectly what I want, but if I switch to P2D, the PGraphics buffer doesn't clear anymore. Does anyone has an explanation ? What could I do to avoid this trouble ?




  • Answer ✓

    I find by myself the answer. clear() function need to be between the beginDraw() and endDraw() function with P2D, but not with normal rendering ! But why... One more subtlety of Processing that I can't explain !

  • edited January 2018

    @YvesB -- thank you for sharing your findings.

    Hmm, I wonder if that is a bug to report, or expected behavior -- I believe that clear should be required between beginDraw/endDraw in both cases, although perhaps with the default rendered can be more lax about this. Like with loadPixels, you can sometimes get away with certain things working, but the expectation is that for guaranteed results you must explicitly declare when you are manipulating a buffer and then commit changes.

Sign In or Register to comment.