We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › ramp object
Page Index Toggle Pages: 1
ramp object (Read 660 times)
ramp object
Aug 31st, 2009, 5:52am
 
Here is a simple generator , for making 0 to 1 or -1 to 1 ramps, that are interpolated over a duration in ms.



Code:

/*
a ramp class.

Ramp()
- duration in milliseconds
- current millis()
- starting point (-1 or 0)



*/



Ramp r1, r2;

void setup()
{
 size (300,300);
r1 = new Ramp();
r2 = new Ramp();
}

void draw()
{

 noStroke();
 fill (255,20); rect(0,0,width,height);


 fill (120);
 
 r1.trigger();
 r2.trigger();
 stroke(120 - (120* r1.rampValue));
 strokeCap(PROJECT);
strokeWeight (10 * r1.rampValue);

 line (0, height, width * r1.rampValue, height - (height * r1.rampValue));

}

void mousePressed() {

r1 = new Ramp(random(20000), millis(), 0);


}



class Ramp {
 float rampValue;    // current ramp value
 float rampStartMillis; // constructor init millisecond
 float rampDuration; // ramp duration in ms
 boolean run; // ramp trigger state
 int range; // -1 to 1 ( full ramp ) or 0 to 1 ( half ramp)
Ramp () {
rampDuration = 0;
rampStartMillis = 0;
run = false;
rampValue = 0;
range = 0;
}  
Ramp (float duration, float startTime, int rampRange) {
 rampDuration = duration;
 rampStartMillis = startTime;
 run = true;
 range = rampRange;  
}
   
   
     void trigger() {
 if (rampValue == 1) {run = false;}
       if (run) {
     rampValue =  lerp(range,1, constrain((millis()-rampStartMillis)/rampDuration, 0, 1));
       }
   
     
     }
   
}
Page Index Toggle Pages: 1