Saving Current Frame on Mouse Click

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.

Show your code please

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

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

1 Like

Hello @eoinfen,

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:

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
thread() runs independently of the main animation loop and is showing the actual frameCount.

:)

1 Like

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.

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(). Then, whenever the mouse is clicked, save the PImage and not your sketch canvas.

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.

Hello @eoinfen,

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

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

:)