Clear PGraphic after image()

Hey, I have some problem I can’t figure out:
I want to draw a circle on a screen by the mouseX mouseY cords (I want the draw to stay on the screen so I refresh the background un the draw function) , and above it I’m trying to draw a smaller circle that is keeping the mouseX mouseY cords. So I thought to use PGraphics and to draw the smaller circle and use clear every frame.
the problem is that when I use the Image() function to show the PGraphics Its stays on the screen so I see the trail of the smaller circle.

Does anyone have any idea what can I do differently? :thinking:

The problem is that you try to keep that part of your graphics that is changed when the image()-function is called. You could solve your problem by switching your graphics.

PGraphics background;
void setup() {
  size(300, 300);
  background=createGraphics(300, 300);
  background.beginDraw();

  //you can draw whatever you wanna draw :)
  for (int i=0; i<10000; i++) {
    background.stroke(random(255), random(255), random(255));
    background.strokeWeight(4);
    background.point(random(300), random(300));
  }

  background.endDraw();
}

void draw() {
  background(background);
  stroke(0);
  fill(0, 0);
  strokeWeight(3);
  circle(mouseX, mouseY, 30);
  youcanevenchangeit();
}


//change the background
void youcanevenchangeit() {
  background.beginDraw();
  background.stroke(random(255), random(255), random(255));
  background.strokeWeight(4);
  background.point(random(300), random(300));
  background.endDraw();
}
1 Like

Amazing!
Had no idea that I can use the background function instead of the image.
Thank you :slight_smile:

1 Like