# How to freeze a draw loop?

**URL:** <https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741>\
**Category:** Beginners\
**Created:** [August 18, 2018, 2:05am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741 "2018-08-18T02:05:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrongexit](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@wrongexit](https://discourse.processing.org/u/wrongexit)\
**Post date:** [August 18, 2018, 2:05am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/1 "2018-08-18T02:05:38Z")

</div>

I have a draw loop that makes a new graphic each time. I want to be able to pause the loop by clicking the mouse when I see a graphic that I like. But everything I’ve tried fails to stop on the current frame. It’s always the next frame, or a later one, no matter how slow the frame rate. For example,

`if (mousePressed == true) { delay(20000); }`

pauses things for 20 seconds, but it seems to take a frame or two until things stop.

I haven’t found any info that addresses this sort of thing…I’m doing something wrong. Suggestions?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 18, 2018, 2:24am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/2 "2018-08-18T02:24:17Z")

</div>

```auto
// Discourse.Processing.org/t/how-to-freeze-a-draw-loop/2741/2
// GoToLoop (2018-Aug-18)

static final int DELAY = 5 * 1000; // 5 seconds
static final float FPS = .5; // 2 seconds

void setup() {
  size(300, 150);
  frameRate(FPS);
}

void draw() {
  getSurface().setTitle("Frame: " + frameCount);
  background((color) random(#000000));
}

void mousePressed() {
  pauseFrame();
}

void pauseFrame() {
  delay(DELAY);
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 18, 2018, 2:26am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/3 "2018-08-18T02:26:15Z")

</div>

You can use `loop()` and `noLoop()`. You can find hem in the reference. Another way, which is not documented in the reference, is to do this:

```java
void mousePressed(){
   looping = !looping; //EDIT ******
}

```

This toggles between loop and noLoop, exactly as I suggested in the first line. however, this way is more straight forward. Not sure if it will stop right at the frame that you want. Just give it a try and see if it works for you.

Kf

---

<div class="post-metadata">

**Author:** ![wrongexit](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@wrongexit](https://discourse.processing.org/u/wrongexit)\
**Post date:** [August 18, 2018, 2:36am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/4 "2018-08-18T02:36:03Z")

</div>

This works! Thank you!

---

<div class="post-metadata">

**Author:** ![wrongexit](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@wrongexit](https://discourse.processing.org/u/wrongexit)\
**Post date:** [August 18, 2018, 2:38am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/5 "2018-08-18T02:38:54Z")

</div>

> [@kfrajer](#):
>
> void mousePressed(){ looping != looping; }

I get an error: Syntax error on token"!=", invalid AssignmentOperator

But the other suggestion works. Thanks!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 18, 2018, 2:41am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/6 "2018-08-18T02:41:20Z")

</div>

> [@wrongexit](#):
>
> I get an error: `Syntax error on token "!=", invalid AssignmentOperator`.

Should be instead: `looping = !looping;` or `looping ^= true;` 🤓

---

<div class="post-metadata">

**Author:** ![wrongexit](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@wrongexit](https://discourse.processing.org/u/wrongexit)\
**Post date:** [August 18, 2018, 2:49am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/7 "2018-08-18T02:49:18Z")

</div>

This seems to stop it on the next frame, not the current one. 🤨

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 18, 2018, 3:12am UTC](https://discourse.processing.org/t/how-to-freeze-a-draw-loop/2741/8 "2018-08-18T03:12:50Z")

</div>

My mistake… I will correct my post.

> [@kfrajer](#):
>
> Not sure if it will stop right at the frame that you want

We will need to see some code to see if it is possible to store your current state, or use another approach different to looping.

Kf
