How to get a snapshot of a PGraphics "paused" frame and use it as a PImage at a predefined condition in time?

If I use static sketches I can create a PImage right away and use it in a PGraphics buffer, then use it as a BufferedImage successfully with getNative().
How do I get a snapshot of this PGraphics buffer in time if I have a moving/animated object and a sketch with frames? This snapshot (a future frame) can be a “pause” of the animation in time or if a condition is met. e.g.:
“if frame count=X then pause”
“if a condition is met then pause”
then get the PImage

With static sketches I get the one and only frame at the end. But how could I get a specified frame in time (after x seconds or minutes) or if a condition is met? Does this have to do something with the delay() method?. How do I pause the animation or “movement” and then get a PGaraphics/PImage at this very moment?

the PImage save() serve a similar purpose, but I do not need an image file, I just need to pass the PGaraphics/PImage buffer “paused” object to getNative().

thanks!

Hello.
You don’t really need to “pause”

PImage buff;
float now;
float interval = 4530;

void setup() {
  size(400, 400);
  buff = createImage(400, 400, RGB);
  now = millis();
}

void draw() {
  background((millis()/10)%255, 180, ( millis()/8)%255);
  noStroke();
  for (int i = 0; i< 15; i++) {
    float x = random(width);
    float y = random(height);
    float r = random(30);
    color c = color(x%255, y%255, 180);
    fill(c); 
    ellipse(x, y, r, r);
  }




  if (millis() - now > interval) {
    save();
    now = millis();
  }

  image(buff, 0, 0, width/2, width/2);
}

void mouseClicked() {
  save();
}

void save() {
  loadPixels();
  buff.loadPixels();
  for (int i =0; i< pixels.length; i++) {
    buff.pixels[i]  = pixels[i];
  }
  updatePixels();
  buff.updatePixels();
}

But if you want to you can. Look in the reference for noLoop(),loop() and redraw().

2 Likes

excellent example, straight to the point reply. I will try to use those “buffer save” timings in a multi instance sketch environment that expose multiple buffers. hopefully with some tweaks it will work… I will mark as a solution. Thanks a lot !

2 Likes