# Trying to eliminate draw flicker

**URL:** <https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530>\
**Category:** Coding Questions\
**Created:** [August 28, 2020, 6:03am UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530 "2020-08-28T06:03:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [August 28, 2020, 6:03am UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/1 "2020-08-28T06:03:19Z")

</div>

Thank you for reading this.

I had this working reasonably well several months ago but I must have forgotten to save the project. I had the moon orbiting the earth and the earth orbiting the sun with minimal flicker. I’ve read through many posts but cannot relate the answers to my project. What have I forgotten?

// The PShape object  
PShape earth;

float orbitx, orbity;  
float radius = 100;

float angle = 0.0;  
int i = 0;  
int timeEnd;

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

// Create the earth as a PShape  
earth = createShape(ELLIPSE, 0, 0, 50, 50);  
earth.setStroke(color(0, 0, 255));  
earth.setFill(color(0, 0, 255));

timeEnd = millis();  
}

void draw()  
{  
background(204);  
if (millis() \> timeEnd + 100)  
{  
timeEnd = millis();

```
i += 1;
if (i > 90)
{
  i = 0;
}  
angle = (i * TAU) / 90;
orbitx = (sin(angle) * radius) + width/2;
orbity = (cos(angle) * radius) + height/2;

translate(orbitx, orbity);
shape(earth);

```

}  
}

```auto

```

---

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [August 28, 2020, 7:31am UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/3 "2020-08-28T07:31:27Z")

</div>

I haven’t withdrawn this post, or if I did I didn’t mean to.

I discovered that I have background() function in the wrong place, so no more flickering. However, there’s probably a better way of doing this.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [August 28, 2020, 9:05am UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/4 "2020-08-28T09:05:26Z")

</div>

Haha, that doesn’t mean you withdrew your post, but that I withdrew mine, which I did because I thought I had misunderstood your question.😉

If you want to get rid of the flickering, you could either do that, or put the code for drawing outside of the if-statement (so `translate` and `shape`). Both of those options won’t be smooth though, so if you want that, you could just get rid of that condition all together.

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [August 28, 2020, 10:23am UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/5 "2020-08-28T10:23:58Z")

</div>

Well you have the `if (millis() > timeEnd + 100)` in there, drawing every 100 ms (10 FPS), so that might have something to do with it?

Apparently you solved it in a different way (changing where background is), so it could be interesting to see how you did it.

Btw, there’s also a [frameRate()](https://processing.org/reference/frameRate_.html) function.

---

<div class="post-metadata">

**Author:** ![Hyperion65](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hyperion65/32/10569_2.png) [@Hyperion65](https://discourse.processing.org/u/Hyperion65)\
**Post date:** [August 28, 2020, 2:27pm UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/6 "2020-08-28T14:27:04Z")

</div>

I am not sure I understand what you desire.  
If you want to eliminate the flicker, why not draw every single frame -\> remove the mills() stuff.

If you want the flicker but want to control it, I usually use modulo.  
-\>  
if (mills()%500 == 0)) {  
do something every 500 ms  
}

so you could control how often the earth is drawn…

---

<div class="post-metadata">

**Author:** ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)\
**Post date:** [August 28, 2020, 9:00pm UTC](https://discourse.processing.org/t/trying-to-eliminate-draw-flicker/23530/7 "2020-08-28T21:00:22Z")

</div>

Thank you Schred, raron and Hyperion65 for your replies.

I thought that I had to set up a timed event to trigger the Draw() function in the same way as other languages but it seems that I don’t. I’ll experiment further.  
By the way, I found my original code, silly me saved it under some obscure name that I’d forgotten.

I just confirmed that all I need to do is move the translate and shape functions outside of the if loop, as suggested. Thanks again.
