Map mouseX to frameRate?

edited January 2016 in Questions about Code

First time poster here.

I made this little animation sketch and I want to have mouseX control the frameRate in draw. It won't do it. Can anyone explain to me why it won't and how to work around this? Thanks!

void setup(){
  size(300, 300);
  background(#04B1CE);
  noFill();
  colorMode(HSB);

}

void draw(){

  frameRate(mouseX); //Draw ceases to loop with frameRate as mouseX, becomes static.

  strokeWeight(random(3, 70));
  stroke(random(255), 255, 255); //HSB
  float rainbow_size = random(200, 270);

  line(150, 150, 300, 0);

    stroke(random(255), 255, 255);

  line(150, 150, 0, 0);

    stroke(random(255), 255, 255); 

  line(150, 150, 150, 300);

    stroke(random(255), 255, 255);
  }

Answers

  • Answer ✓

    Ah, an interesting problem! When your sketch starts, the initial value of mouseX is 0. And when you call framerate(0), you get no redrawing at all! And since no redrawing is happening, mouseX's value is never updated either! So you're stuck with a framerate of 0!

    The fix is quite simple - just add 1 to mouseX!

    frameRate(mouseX+1);
    
  • Haha wow - I figured it was something simple like that but I just couldn't seem to wrap my head around it.

    Thanks for taking the time to reply!

Sign In or Register to comment.