Processing: How can I draw only every x frames?

前端 未结 1 1597
迷失自我
迷失自我 2020-12-12 02:39

I\'m experimenting with the following Code:

//3D Spectrogram with Microphone Input
//Modified by kylejanzen 2011 - https://kylejanzen.wordpress.com
//Based o         


        
相关标签:
1条回答
  • 2020-12-12 03:38

    You've got three options:

    Option 1: Call the frameRate() function to reduce the number of frames that get drawn per second.

    void setup(){
      size(500, 500);
      frameRate(5);
    }
    
    void draw(){
      background(0);
      ellipse(mouseX, mouseY, 20, 20);
    }
    

    Option 2: Use the frameCount variable and the modulus % operator to determine when X frames have elapsed.

    void setup(){
      size(500, 500);
    }
    
    void draw(){
      if(frameCount % 5 == 0){
        background(0);
        ellipse(mouseX, mouseY, 20, 20);
      }
    }
    

    Option 3: You could create your own variable that stores the number of frames that have elapsed.

    int framesElapsed = 0;
    
    void setup(){
      size(500, 500);
    }
    
    void draw(){
      framesElapsed++;
    
      if(framesElapsed == 5){
        background(0);
        ellipse(mouseX, mouseY, 20, 20);
        framesElapsed = 0;
      }
    }
    

    Note that for the simple case, this is just doing what the modulus operator in option 2 is doing. In that case, modulus is probably better. But this becomes useful if you want, for example, different things to happen at different times. In that case you'd have multiple variables keeping track of the "lifetime" of whatever you want to track.

    But for your example, option 2 is probably the best option.

    0 讨论(0)
提交回复
热议问题