How does draw() really work?

Im trying to understand how draw works at a closer level. My comp sci teacher told us it runs 60 times a second. Does it run everything inside draw at the exact same instant, then do it again the next 60th of a second? Or does it run it all one by one, and then what does it do when if it hits the last command and 1/60th of a second hasn't gone by yet? Or if it can't run all of the commands with that much time, then is that what makes framerate lag? And why does my millis() timer always have a different result everytime I run it?

Thank you for any help

Answers

  • edited March 2017

    My comp sci teacher told us it runs 60 times a second.

    • By default, the "Animation Thread" attempts to call back draw() at about 60 FPS (~16 ms) rate.
    • We can change that via frameRate(): https://Processing.org/reference/frameRate_.html
    • Pay attention, the key here is to attempt to keep that rate.
    • If draw() and all input triggers finish before 16 ms, the Thread sleeps the remaining difference.
    • Otherwise, there's no wait at all. And next draw() is called back ASAP.
  • So it does run all of the commands one by one? And what does it do if it can't finish all of the triggers before 16 ms? Also, why is it 'approx.' 16 ms, instead of exactly?

  • I like to think frameRate as setting a top limit in how often draw() gets called. You can try to increase this number to 200 or even 1000. Now, if you load a movie, a movie usually plays 30fps so is there a point to run faster than 30fps?

    Now, remember this is a top limit, but it is not guaranteed. You can have a while loop inside draw()blocking its normal execution times or even blocking it. Check the code below for a demo of the frameRate as calculated by Processing.

    Kf

    int posX=0;
    void setup() {
      background(0);
      fullScreen(P2D); 
      // size(displayWidth, displayHeight, P2D);
    
      ellipseMode(CENTER);
      textAlign(CENTER, CENTER);
      setDisplay();
    }
    
    void draw() {
    
      //text("fps :"+frameRate, 20, 20);
      ellipse(posX++, map(frameRate, 0, 100, height, 0), 5, 5);
      if (posX>width) {
        posX=0;
        setDisplay();
      }
    }
    
    void setDisplay() {
      background(0);   
      fill(255);
      text("100 fps", (width>>1) + 50, 50);
      text("50 fps", (width>>1)+50, height>>1);
      text("0 fps", (width>>1)+50, height-50);
      stroke(255);
      line(width>>1, 0, width>>1, height);
      noStroke();
      fill(0, 255, 0);
    }
    
  • And what does it do if it can't finish all of the triggers before 16 ms?

    It means the 60 FPS rate has failed for that frame. The sketch will suffer slowdown.

    Also, why is it 'approx.' 16 ms, instead of exactly?

    60 FPS = 1000/60 = 16.6...

  • edited March 2017

    you can write a draw that needs minutes or days to run when it's calculating stuff.

    but a normal sketch with 10 or 100 rectangles or whatever will run more or less at 60 FPS.

    But all commands are added up on an internal canvas and only displayed at the END.

    So during run you can't see any changes, only at the end.

  • And why does my millis() timer always have a different result everytime I run it?

    Read the reference. Millis is the total time taken from the start of the program, not just the time taken for that frame.

  • edited March 2017

    @john832 excuse me seriously what are these question?

    How does draw() really work?

    No idea

    Im trying to understand how draw works at a closer level.

    Fair, but why?

    My comp sci teacher told us it runs 60 times a second

    It does

    Does it run everything inside draw at the exact same instant

    How would that even be possible? :S

    Or does it run it all one by one

    Of course

    what does it do when if it hits the last command and 1/60th of a second hasn't gone by yet?

    Nothing

    Or if it can't run all of the commands with that much time, then is that what makes framerate lag?

    Yes

    And why does my millis() timer always have a different result everytime I run it?

    Your clock hasn't stopped

Sign In or Register to comment.