Difference between revisions of "FSOSS 2010/processing.js/timing"
(Created page with '<source lang="JavaScript"> →FSOSS 2010 Andor Salga Example of timing: // pixels/second // 250 will go from 0 to 500 in 2 seconds float velocity = 250.0f; float …') |
(No difference)
|
Revision as of 15:10, 27 October 2010
/*
FSOSS 2010
Andor Salga
Example of timing
*/
// pixels/second
// 250 will go from 0 to 500 in 2 seconds
float velocity = 250.0f;
float position = 0.0f;
int deltaTimer = 0;
boolean wasteCycles = false;
void setup(){
size(500, 500);
}
void draw(){
background(#663399);
// displacement = velocity * seconds
position += velocity * getDelta()/1000.0f;
// Wrap the object
if(position > 500){
position = 0;
}
// Regardless of lag, garbage collection or rendering speed,
// position will increase by the correct velocity.
if(wasteCycles){
for(int i = 0; i < 10000; i++){
for(int j = 0; j < 10000; j++){
}
}
}
rect(position, height/2.0f, 40, 40);
}
/*
Returns the number of milliseconds which
have elapsed since last draw
*/
float getDelta(){
float millisElapsed = millis() - deltaTimer;
deltaTimer = millis();
return millisElapsed;
}