# PGraphics clear() is not working?

**URL:** <https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296>\
**Category:** Coding Questions\
**Created:** [December 25, 2022, 2:17pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296 "2022-12-25T14:17:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![FaisalKhatri](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@FaisalKhatri](https://discourse.processing.org/u/FaisalKhatri)\
**Post date:** [December 25, 2022, 2:17pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/1 "2022-12-25T14:17:32Z")

</div>

I’m trying to clear pixels in PGraphics object. It’s not working? or am I missing something? Here’s a little code for illustration.

```auto
PGraphics pg;

void setup() {
  size(600, 600);
  background(0);
  fill(255);
  stroke(255);
  pg = createGraphics(width, height);
}

void draw() {
    
    // Create random cirles on canvas
    ellipse(random(width), random(height), 5, 5);
    
    if (mousePressed)
    {
      // Create a line from origin to mouse cursor on seperate canvas
      pg.beginDraw();
      pg.clear();
      pg.stroke(255);
      pg.line(0, 0, mouseX, mouseY);
      pg.endDraw();
      image(pg, 0, 0);
    }
}

```

![java_1BW9bSEorO](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/f/cfe8bd193656edd4a4f89154a2d58c3719cad6cf.gif)

If I only want one line as I move my mouse around, how would I achieve that? I can’t use background(0) in draw method because it will erase everything.  
I have tried pg.background(0, 0) and it still won’t work.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 25, 2022, 3:01pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/2 "2022-12-25T15:01:18Z")

</div>

Hi @FaisalKhatri,

The issue don’t comes from PGraphics. It works as expected. Think a bit about your code.

Hint: You draw to your main context without deleting, even if your pg gets cleared. 🙂

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![FaisalKhatri](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@FaisalKhatri](https://discourse.processing.org/u/FaisalKhatri)\
**Post date:** [December 25, 2022, 4:32pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/3 "2022-12-25T16:32:05Z")

</div>

Can you help me correct my code to achieve this?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 25, 2022, 4:44pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/4 "2022-12-25T16:44:11Z")

</div>

In the main draw() method add the the statement `background(0)` as the first statement. It will now give you the single line but erases the old stars causing them to flicker.

To correct that either

- create another PGraphics for the stars or
- remember the position of each star and draw them all every frame

---

<div class="post-metadata">

**Author:** ![FaisalKhatri](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@FaisalKhatri](https://discourse.processing.org/u/FaisalKhatri)\
**Post date:** [December 25, 2022, 7:38pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/5 "2022-12-25T19:38:17Z")

</div>

Thanks for the ideas. I was able to achieve it by drawing my line to the main canvas and circles on PGraphics. Kinda switched them. Now my code looks like this

```auto
void draw() {
    
    background (0);
    
    // Create random cirles on canvas
    pg.beginDraw();
    pg.ellipse(random(width), random(height), 5, 5);
    pg.endDraw();
    image(pg, 0, 0);
    
    // Create line from origin to cursor
    if (mousePressed)
    {
      line(0, 0, mouseX, mouseY);
    }
}

```

But question still remains, how to use clear function?

**Is there a way to completely reset PGraphics object?**

(I don’t want to create another instance on each frame)

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 25, 2022, 7:58pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/6 "2022-12-25T19:58:16Z")

</div>

Hi @FaisalKhatri,  
The below code do not create a new instance.

> [@FaisalKhatri](#):
>
> ```auto
> pg.beginDraw();
> pg.ellipse(random(width), random(height), 5, 5);
> pg.endDraw();
> 
> ```

The instance is created by the createGraphics call from within your setup.  
So you can call the clear() method to reset the PGraphics pixels.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![FaisalKhatri](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@FaisalKhatri](https://discourse.processing.org/u/FaisalKhatri)\
**Post date:** [December 25, 2022, 8:04pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/7 "2022-12-25T20:04:34Z")

</div>

Yes I know I’m not creating new instance. It’s just something I don’t want to do as an alternative 🥲

---

<div class="post-metadata">

**Author:** ![FaisalKhatri](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@FaisalKhatri](https://discourse.processing.org/u/FaisalKhatri)\
**Post date:** [December 25, 2022, 8:10pm UTC](https://discourse.processing.org/t/pgraphics-clear-is-not-working/40296/8 "2022-12-25T20:10:38Z")

</div>

Alright I found this [issue](https://github.com/processing/processing-docs/issues/544#issuecomment-392256546) on GitHub which explains how PGraphics actually work.  
So it turns out that PGraphics is an off-screen canvas and we are using `Image()` function to draw everything that is in PGraphics to the main canvas. So basically everything we see is main canvas.

So which is why `clear()` won’t erase my lines because they are drawn on the main canvas and not PGraphics. It all makes sense now. PGraphics is working as it should. I did not understand it correctly.

I’ll keep this topic as it is, in case if anyone else has the same confusion.  
Thanks!
