xxxxxxxxxx
46
let duration = 4000;
let timestamp = -duration;
let clicked = false;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
if (millis() - timestamp > duration) {
clicked = false;
console.log("timeout");
} else {
console.log("running", millis() - timestamp);
}
let ratio = 1 - (millis() - timestamp) / duration; // calculate percentage of time elapsed
ratio = constrain(ratio, 0, 1); // constrain ratio variable between 0 and 1
// draw time bar
noStroke();
if (clicked) {
fill(200);
} else {
fill(255, 200, 200);
}
rect(30, 30, 100, 30);
fill(0);
rect(30, 30, 100 * ratio, 30);
// draw object
if (clicked) {
fill(255, 200, 0);
circle(width/2, height/2, 100, 100);
} else {
fill(0, 200, 255);
circle(width/2, height/2, 100, 100);
}
}
function mousePressed() {
clicked = true;
timestamp = millis();
}