# Why background() is not updated in a draw() loop

**URL:** <https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904>\
**Category:** Coding Questions\
**Created:** [March 3, 2019, 4:52pm UTC](https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904 "2019-03-03T16:52:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/elode/32/3952_2.png) [@elode](https://discourse.processing.org/u/elode)\
**Post date:** [March 3, 2019, 4:52pm UTC](https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904/1 "2019-03-03T16:52:49Z")

</div>

I start with Processing.

I’m studying Daniel Shiffman’s book “Learning Processing”.

In Exercise 6.5 of the book, I can not understand why background(0) which is in draw() is not updated each time a new line is drawn, erasing the previously drawn line, but only when endY returns to 0 at the end, in if(endY \> height) …

```auto
int endY;

void setup() {
  size(200, 200);
  frameRate(5);
  
  endY = 0;
}

void draw() {
  background(0);
  
  for (int y = 0; y < endY; y += 10) {
    stroke(255);
    line(0, y, width, y);
  }
  endY = endY + 10;
  
  if(endY > height) {
    endY = 0;
  }
}

```

It’s not very clear to me. Could someone explain to me?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 3, 2019, 5:25pm UTC](https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904/2 "2019-03-03T17:25:43Z")

</div>

in the case you want only one line,  
and that is moving down,  
the code could be much more simple

```auto
int endY=0;

void setup() {
  size(200, 200);
  frameRate(5);
  stroke(255);
}

void draw() {
  background(0);
  //text(nf(frameRate,1,1),10,9);
  // for (int y = 0; y < endY; y += 10) line(0, y, width, y);
  line(0, endY, width, endY);
  endY = endY + 10;
  if (endY > height) endY = 0;
}

```

because now you ( after background(0) == CLEAR )  
draw by a FOR loop as many lines as “endY” allows.  
hm, where the FOR loop comes from? not from:

> <https://github.com/shiffman/LearningProcessing/blob/master/chp06_loops/example_06_05_infiniteloop2/example_06_05_infiniteloop2.pde>

---

<div class="post-metadata">

**Author:** ![elode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/elode/32/3952_2.png) [@elode](https://discourse.processing.org/u/elode)\
**Post date:** [March 3, 2019, 6:47pm UTC](https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904/3 "2019-03-03T18:47:08Z")

</div>

Well… it’s Exercise 6.5, not Example 6.5  
[http://learningprocessing.com/exercises/chp06/exercise-06-05-one-line-at-a-time](http://learningprocessing.com/exercises/chp06/exercise-06-05-one-line-at-a-time)

And thank you for your explanation, it’s a little clearer for me … I hope …

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 3, 2019, 11:13pm UTC](https://discourse.processing.org/t/why-background-is-not-updated-in-a-draw-loop/8904/4 "2019-03-03T23:13:39Z")

</div>

> // rendering one line at a time using a for loop.

ok, i might have expected same like you.
