Increase size in time

Hello folks, I'm working on a very basic thing: I'd like to increase the size of an ellipse from a very tiny point to fill all the display window. This should happen in 20 minutes. The code I've attached below it works but so far I control the increase with the value of the variable rMove1 and I'd like to do it using something with time, since It should happen in 20 minutes exactly. I'm a newbie and I'm pretty sure there's an easy way to do it. Thank you in advance!

float radius1 = 1;
float rMove1 = 0.001;
int xPos1 = 200;
int yPos1 = 200;

void setup()
{
size(400, 400);
frameRate(30);
}
void draw()
{
 background(0);
 fill(255);
 noStroke();
 ellipse(xPos1, yPos1, radius1, radius1);
 radius1 += rMove1;
}

Answers

  • edited April 2015

    We can call millis() in order to get how many milliseconds have transpired since sketch began:
    https://processing.org/reference/millis_.html

    Then map() that range to width (or height) range in order to determine current diameter:
    https://processing.org/reference/map_.html

    // forum.processing.org/two/discussion/10286/increase-size-in-time
    
    static final int FPS = 4, DURATION = 20 * 60 * 1000; // 20 minutes
    float mapping;
    
    void setup() {
      size(600, 600, JAVA2D);
      smooth(4);
      frameRate(FPS);
    
      background(0);
      noStroke();
      ellipseMode(CENTER);
    
      mapping = (width - 1.0)/DURATION;
    }
    
    void draw() {
      int m = millis(), s = m/1000, r = DURATION/1000 - s;
    
      //int diam = (int) map(m, 0, DURATION, 0, width - 1);
      int diam = (int) (m*mapping); // faster than map()!
    
      if (m >= DURATION)  noLoop();
    
      String msg = "Diameter: " + diam + "  -  Elapsed: " + s + "  -  Remaining: " + r;
      frame.setTitle(msg);
    
      fill((color) random(#000000));
      ellipse(width>>1, height>>1, diam, diam);
    }
    
Sign In or Register to comment.