# PGraphics freezing when rendering to second PGraphics

**URL:** https://discourse.processing.org/t/pgraphics-freezing-when-rendering-to-second-pgraphics/36616
**Category:** Processing
**Created:** [April 26, 2022, 8:23pm UTC](https://discourse.processing.org/t/pgraphics-freezing-when-rendering-to-second-pgraphics/36616 "2022-04-26T20:23:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lack\_ofa\_better\_name](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@lack\_ofa\_better\_name](https://discourse.processing.org/u/lack_ofa_better_name)
#### Post date: [April 26, 2022, 8:23pm UTC](https://discourse.processing.org/t/pgraphics-freezing-when-rendering-to-second-pgraphics/36616/1 "2022-04-26T20:23:17Z")

</div>

I have a piece of code that requires two offscreen buffers. When i draw the first buffer into the second one with image(), the first buffer “freezes”.

I draw both graphics to the window in main,

```auto
void draw() {
  updateBuffer(buffers[0], buffers[1]);

  image(buffers[0], 0, 0);
  image(buffers[1], width/2, 0);
}

```

And they both draw as they should.

However when i add

```auto
  buffers[1].beginDraw(); 
  buffers[1].image(buffers[0], 0, 0); // Problem
  buffers[1].endDraw();

```

the first buffer “freezes” when i draw it in the window, however it still displays it’s updates inside the second buffer.

Interestingly enough, adding an empty beginDraw(), endDraw() fixes the problem.

```auto
  buffers[1].beginDraw(); 
  buffers[1].image(buffers[0], 0, 0); // Problem
  buffers[1].endDraw();
  buffers[0].beginDraw();
  buffers[0].endDraw();

```

Full snippet displaying the problem

```auto
PGraphics[] buffers = new PGraphics[2];

void setup() {
  size(1200, 600, P2D);

  for (int i = 0; i < 2; ++i) {
    buffers[i] = createGraphics(width/2, height);
    buffers[i].beginDraw();
    buffers[i].background(0);
    buffers[i].fill(i*255, 255 - i*255, 0);
    buffers[i].endDraw();
  }
}

void draw() {
  buffers[0].beginDraw();
  buffers[0].background(0);
  buffers[0].ellipse(frameCount*4%width/2, height/2, 10, 10);
  buffers[0].endDraw();

  buffers[1].beginDraw();
  buffers[1].background(0);
  buffers[1].ellipse(frameCount*4%width/2, height/2, 10, 10);

  //Uncomment this to cause buffers[0] to freeze
  //buffers[1].image(buffers[0], 0, 0); 
  buffers[1].endDraw();

  //Uncomment this to solve the buffers[0] freeze
  //buffers[0].beginDraw();
  //buffers[0].endDraw();

  image(buffers[0], 0, 0);
  image(buffers[1], width/2, 0);
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 26, 2022, 10:58pm UTC](https://discourse.processing.org/t/pgraphics-freezing-when-rendering-to-second-pgraphics/36616/2 "2022-04-26T22:58:29Z")

</div>

Hello @lack_ofa_better_name ,

My exploration of this:

> **Click here to see code!**
>
> ```auto
> PGraphics[] buffers = new PGraphics[2];
> 
> void setup() 
> {
> size(600, 300, P2D);
> 
> for (int i = 0; i < 2; ++i) 
> {
> buffers[i] = createGraphics(width/2, height, P2D); // Added P2D
> buffers[i].beginDraw();
> buffers[i].background(0);
> buffers[i].fill(i*255, 255 - i*255, 0);
> buffers[i].endDraw();
> }
> }
> 
> void draw() 
> {
> buffers[0].beginDraw();
> buffers[0].background(0);
> buffers[0].textAlign(CENTER, CENTER);
> buffers[0].textSize(96);
> buffers[0].text(frameCount, width/4, height/2);
> buffers[0].endDraw();
>   
> image(buffers[0], 0, 0);
> 
> buffers[1].beginDraw();
> buffers[1].background(0);
> buffers[1].image(buffers[0], 0, 0); 
> buffers[1].textAlign(CENTER, CENTER);
> buffers[1].textSize(48);
> buffers[1].text(frameCount, width/4, height/4);
> buffers[1].endDraw();
> 
> image(buffers[1], width/2, 0);
> }
>   
> void mousePressed() 
> {
> noLoop();
> }
> 
> void mouseReleased() 
> {
> loop();
> }
> 
> ```

I modified this:

```auto
buffers[i] = createGraphics(width/2, height, P2D); // Added P2D

```

That was fun!

`:)`
