# Background() causing weirdness with get()

**URL:** https://discourse.processing.org/t/background-causing-weirdness-with-get/4959
**Category:** Coding Questions
**Created:** [October 28, 2018, 5:34pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959 "2018-10-28T17:34:05Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![P-Joe-S](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@P-Joe-S](https://discourse.processing.org/u/P-Joe-S)
#### Post date: [October 28, 2018, 5:34pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/1 "2018-10-28T17:34:05Z")

</div>

I’m trying to use get() to make patterns using the previous frame, so I’m calling get() at the start of draw().

If I use background() after that to clear the frame, I find that what get() has given me is not the previous frame but the frame before that.

I’ve made a simple 2D sketch that shows the issue. It draws alternating green and blue circles along the bottom of the window. When I use background() I see alternating frames of all green or all blue circles:

![ProcessingBackground](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/470e4d132082456e4cd3da0cb3d680fe78230c52.jpeg) !

However, if I remove background() and just draw a rect() the same size as the screen, which I would think should be equivalent to using background() in this sketch, I find that get() has given me the previous frame as intended and I get a mixture of green and blue circles.

The grey circles I tried adding before get() to see what would happen. They appear in the right place, but if background() is used they’re drawn on top of the wrong frame.

What’s going on here?

Here’s my code

```auto
PImage getWindow;

void setup() {
  size(400, 300, P3D);
  frameRate(2);
}

void draw() {

  fill(150);
  ellipse( 40 + (width / 5) * (frameCount % 5), 270, 50, 50);

  getWindow = get();

  //remove this to see the difference
  background(128);

  fill(128);
  rect(0, 0, width, height);

  fill( 0, 255 * ((frameCount + 1) % 2), 255 * (frameCount % 2));
  ellipse( 40 + (width / 5) * (frameCount % 5), 270, 50, 50);

  beginShape();
  texture(getWindow);
  vertex( 50, 10, 0, 0, 0);
  vertex( 350, 10, 0, width, 0);
  vertex( 350, 235, 0, width, height);
  vertex( 50, 235, 0, 0, height);
  endShape(CLOSE);
}

```

---

<div class="post-metadata">

### Author: ![P-Joe-S](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@P-Joe-S](https://discourse.processing.org/u/P-Joe-S)
#### Post date: [October 28, 2018, 5:35pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/2 "2018-10-28T17:35:27Z")

</div>

Here’s what it looks like with rect() rather than background:

![ProcessingRect](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/82f54edc5d47ac6b54ec497fd26c57c831dcf440.jpeg)

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 7:24pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/3 "2018-10-28T19:24:51Z")

</div>

Why don‘t you just get the Image as the last Part of draw? It Would still save the Image to getWindow, But Would make it easier to See what is Happening without having to think about which Frame you see.

---

<div class="post-metadata">

### Author: ![P-Joe-S](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@P-Joe-S](https://discourse.processing.org/u/P-Joe-S)
#### Post date: [October 30, 2018, 11:49am UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/4 "2018-10-30T11:49:09Z")

</div>

Thanks, that does produce the right result, though I still don’t understand why.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 30, 2018, 5:41pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/5 "2018-10-30T17:41:39Z")

</div>

Well, trying to get something from the last Frame in the Next Frame will have to happen after draw is called, which means there were a lot of things happening between the last Frame and the Start of your code in the new Frame. There might be something that Messed with your code in between there, that is avoided by just doing it last in the previous Frame. Try to always keep everything in the Same Frame.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [November 1, 2018, 6:24pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/6 "2018-11-01T18:24:14Z")

</div>

> [@P-Joe-S](#):
>
> f I use background() after that to clear the frame, I find that what get() has given me is not the previous frame but the frame before that.

That isn’t what is happening. You have a very complex sketch model that makes it difficult to test what is going wrong, and difficult to explain / understand.

> [@P-Joe-S](#):
>
> What’s going on here?

Start with something really really simple, like this:

```auto
void setup() {
  size(400, 300, P3D);
  noLoop();
}
void draw() {
  fill(255, 0, 0);
  ellipse( width/2, height/2, 100, 100);
  fill(128);
  rect(0, 0, width, height);
}

```

This sketch draws a red ellipse, then covers it with a grey rectangle. What happens? Is it what you expected? Why is this happening?

```
size(400, 300, P3D); // <---- 3D ????

```

…what happens when you draw multiple 2D objects at the same depth in 3D space?

Try changing “P3D” to “P2D”.

---

<div class="post-metadata">

### Author: ![P-Joe-S](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@P-Joe-S](https://discourse.processing.org/u/P-Joe-S)
#### Post date: [November 2, 2018, 1:54pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/7 "2018-11-02T13:54:56Z")

</div>

I had kindof forgotten that I was using P3D, but I get the same results for my sketch in P2D.

```auto
PImage getWindow;

void setup() {
    size(400, 300, P2D);
    frameRate(2);
}

void draw(){
  
  fill(150);
  ellipse( 40 + (width / 5) * (frameCount % 5) , 270, 50, 50);
  
  getWindow = get();
  
  //remove this to see the difference
  background(128);
  
  fill(128);
  rect(0,0, width, height);
   
  fill( 0, 255 * ((frameCount + 1) % 2), 255 * (frameCount % 2));
  ellipse( 40 + (width / 5) * (frameCount % 5) , 270, 50, 50);
  
  image(getWindow, 50, 10, 300, 225);
  
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [November 2, 2018, 3:54pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/8 "2018-11-02T15:54:54Z")

</div>

With the last code you posted, I’m not seeing a difference when I comment out “remove this to see a difference.” The two sketches look the same.

 ![55%20AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e0b5b24c079b3503b8472a7859569fea89eeb0d2.jpeg)

---

<div class="post-metadata">

### Author: ![P-Joe-S](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@P-Joe-S](https://discourse.processing.org/u/P-Joe-S)
#### Post date: [November 2, 2018, 5:05pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/9 "2018-11-02T17:05:25Z")

</div>

The difference is definitely there for me when I specify P2D, but I just tried both leaving it unspecified and also FX2D, both of which displayed correctly (blue and green together as per your screenshot).

I’m using 3.4 if that makes a difference.

This isn’t a problem for me at this stage, since I’ve worked around it in the 3D sketch where I first encountered it, but I’m still mystified as what was going on.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [November 3, 2018, 5:11pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/10 "2018-11-03T17:11:37Z")

</div>

> [@P-Joe-S](#):
>
> The difference is definitely there for me when I specify P2D

Strange – I can’t reproduce that on Processing 3.4 macOS 10.12, but it could be something to do with OpenGL. I **can** explain what is going on in P3D – background and rect operate differently in 3D space. If you don’t need 3D, just use default / Java2D – otherwise you can use depth, blendmode, or hints to prevent counter-intuitive layer composition or “z-fighting.”

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 3, 2018, 9:53pm UTC](https://discourse.processing.org/t/background-causing-weirdness-with-get/4959/11 "2018-11-03T21:53:22Z")

</div>

Thanks for reporting, this is indeed a rare bug in Processing which is hard to reproduce.

I managed to reproduce it on Windows 10 1803, but only on Intel GPU, Nvidia works fine.

I filled an issue on GitHub and we will see what can be done: [https://github.com/processing/processing/issues/5694](https://github.com/processing/processing/issues/5694)
