# Saving Current Frame on Mouse Click

**URL:** <https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427>\
**Category:** Coding Questions\
**Created:** [February 24, 2022, 6:29pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427 "2022-02-24T18:29:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eoinfen](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@eoinfen](https://discourse.processing.org/u/eoinfen)\
**Post date:** [February 24, 2022, 6:29pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/1 "2022-02-24T18:29:22Z")

</div>

I am trying to save the current frame when the mouse is clicked. I have tried using save() and saveFrame(), but both are saving the next frame that will be displayed instead of the current frame being displayed. Is there a way to do this? Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 24, 2022, 9:31pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/2 "2022-02-24T21:31:33Z")

</div>

Show your code please

---

<div class="post-metadata">

**Author:** ![eoinfen](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@eoinfen](https://discourse.processing.org/u/eoinfen)\
**Post date:** [February 24, 2022, 9:39pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/3 "2022-02-24T21:39:58Z")

</div>

Here’s my mousePressed() function. I’ve the usual setup() and draw() functions and frameRate(0.5) in setup().

```auto
void mousePressed() {
  if (mouseButton == RIGHT) {
    save("test.PNG");
  }
}

```

---

<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:** [February 25, 2022, 12:53am UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/4 "2022-02-25T00:53:36Z")

</div>

Hello @eoinfen,

> [@eoinfen](#):
>
> I have tried using save() and saveFrame(), but both are saving the next frame that will be displayed instead of the current frame being displayed.

The draw() canvas updates at the _end_ of draw().  
You are seeing the last frame for 5 seconds and are actually saving the current frame (in progress…) when you are pressing the right mouse button.

Example:

```auto
Thread newThread;

void setup()
  {
  size(200, 200);
  textSize(48);
  textAlign(CENTER, CENTER);
  text(frameCount, width/2, height/2-20);
  frameRate(1/5.0);
  thread("requestData");
  }

void draw()
  {  
  background(0);
  text(frameCount, width/2, height/2-20);
  thread("requestData");
  }

void mousePressed()
  {
  if (mouseButton == RIGHT) 
    {
    save(frameCount + ".PNG");
    println("mousePressed frameCount: ", frameCount);
    }
  }
  
void requestData() 
  {
  println("frameCount: ", frameCount);
  }

```

Reference:  
_[thread() / Reference / Processing.org](https://processing.org/reference/thread_.html)_  
_thread()_ runs independently of the main animation loop and is showing the actual frameCount.

`:)`

---

<div class="post-metadata">

**Author:** ![eoinfen](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@eoinfen](https://discourse.processing.org/u/eoinfen)\
**Post date:** [February 25, 2022, 1:58pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/5 "2022-02-25T13:58:00Z")

</div>

Hello @glv,

Thanks for your reply. So I’m guessing the only ways to save a frame that is currently displaying is to already have saved it at the end of draw(), or to save it at the start of the next call to draw()?

The only solution I can think of at the moment is to save every frame at the start of draw() using the same filename each time. Right-clicking the mouse could then change this filename, effectively preventing the first file from being overwritten.

---

<div class="post-metadata">

**Author:** ![Shwaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shwaa/32/9696_2.png) [@Shwaa](https://discourse.processing.org/u/Shwaa)\
**Post date:** [February 26, 2022, 3:09pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/6 "2022-02-26T15:09:35Z")

</div>

A way you could save the current frame is creating a PImage object with the same size as your sketch, then at the end of draw, copy the canvas to the PImage using PImage [copy()](https://processing.org/reference/PImage_copy_.html). Then, whenever the mouse is clicked, save the PImage and not your sketch canvas.

---

<div class="post-metadata">

**Author:** ![eoinfen](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@eoinfen](https://discourse.processing.org/u/eoinfen)\
**Post date:** [February 26, 2022, 5:01pm UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/7 "2022-02-26T17:01:55Z")

</div>

Hello @Shwaa,

Thank you for your suggestion. Copying the current canvas to something like a PImage in memory seems to be a much cleaner option than saving each image to disk. I think this will be perfect for what I am trying to achieve, but I have a feeling that I need to copy to a PImage at the start of draw() instead of the end: As far as I’m aware, draw() gets called directly after displaying the current image (regardless of how slow the framerate is) so if I copy at the end of draw(), the image I am trying to save will probably already be overwritten by the next image to be displayed.

I will give this a go and report back here.

Thanks again.

---

<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:** [February 27, 2022, 12:33am UTC](https://discourse.processing.org/t/saving-current-frame-on-mouse-click/35427/8 "2022-02-27T00:33:50Z")

</div>

Hello @eoinfen,

Good topic!  
I had fun exploring what happens at a slow framerate!

> [@eoinfen](#):
>
> I have a feeling that I need to copy to a PImage at the start of draw() instead of the end

Go with your feelings on this!

In _mousePressed()_ I used:

`imgLast.save("imageLast.png");`

You can watch the sketchbook folder update as well to experience the latency:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cc20ab1cdaa6d982bfed0b16cb653ae5cfbf08e9.png)

`:)`
