H.Ref
H.Ref

Reputation: 23

Processing saveFrame every 40 msec

I'm trying to create a graphic synchronized with a video at 25 frames/sec. I'm trying to save one frame every 40 msec, but I can't get 25 frames per sec, but only one image. Can someone help me?

int t1;
int count;

void setup(){
  size(400,100);
  background(0);
  stroke(255);
  fill(250);
  line(10,0,10,100);
  line(260,0,260,100);

  for (int i=10;i<251;i+=10){

    t1=millis();
    while(millis()-t1 < 40) {
      count++;
    }
    rect(10,50,i,10);
    //saveFrame("line-####.png");
  }
}

void draw(){}

Upvotes: 2

Views: 421

Answers (1)

CrystalSpider
CrystalSpider

Reputation: 389

The problem in your code is that the canvas is updated only after the end of a frame, which means that your rectangles do exist but are not displayed until your setup() ends. This is why, even tho your code correctly waits 40 milliseconds before drawing a new rectangle, nothing is shown until the for loop ends: before ending the frame, Processing is waiting to complete the for loop.

However, in Processing draw() is called a certain amount of times per second, this amount of times is specified by the function frameRate() (default is 60).
Thus you can just set frameRate to 25 and save an image each time draw() is called.
noLoop() is used to stop calling draw(), in this case, once the rectangles are all drawn.

int count;

void setup()
{
  frameRate(25);
  size(400,100);
  background(0);
  stroke(255);
  fill(250);
  line(10,0,10,100);
  line(260,0,260,100);
}

void draw()
{
  rect(10,50,count,10);
  saveFrame("line-####.png");
  count += 10;
  if(count > 251)
  {
    noLoop();
  }
}

Upvotes: 3

Related Questions