xxxxxxxxxx
38
// Grayscale values
let g = 0;
let gspeed = 10;
// Size values
let s = 0;
let sspeed = 1;
function setup() {
createCanvas(400, 400);
}
function draw() {
// Draw background at g
background(g);
// Change grayscale value
g += gspeed;
// Bounce grayscale value
gspeed = bounce(g, 0, 255, gspeed);
// Draw a rectangle of size s
rect(width / 2, height / 2, s);
// Change size value
s += sspeed;
// Bounce size value
sspeed = bounce(s, 0, 200, sspeed);
}
// Bounce the direction of change of a value
function bounce(value, lower, upper, speed) {
// Bounce the direction of change of a value
if (value < lower || value > upper) {
speed *= -1;
}
return speed;
}