xxxxxxxxxx
/**
* Countdown Generator (v1.0.2)
* GoToLoop (2020-Jun-23)
*
* https://Discourse.Processing.org/t/displaying-from-an-array/21251/7
*
* https://OpenProcessing.org/sketch/400949
* https://Trinket.io/processing/0d6647fcd7
*/
'use strict';
const COUNTDOWN = 20;
var gen = countdown(COUNTDOWN); // generator which counts from 20 to 0
function setup() {
createCanvas(100, 100).mousePressed(resetCountdown); // click to restart
frameRate(1); // 1 FPS draw() callback
fill('yellow').stroke(0).strokeWeight(2);
textAlign(CENTER, CENTER).textSize(50);
}
function draw() {
const { done, value } = gen.next(); // get next countdown value
done && noLoop(); // pause sketch when countdown is done
background(done && 'red' || 'blue'); // red bg when countdown is done
text(value, width >> 1, height >> 1); // display countdown centralized
print(done, value); // log generator's properties done & value
}
function resetCountdown() {
gen = countdown(COUNTDOWN); // restart w/ a new countdown generator
loop(); // resume sketch
}
function* countdown(count) { // generator function*
count = abs(~~count); // make sure count is a positive integer
while (count) yield count--; // yield current count & decrease it by 1
return count; // return 0 & finish this generator
}