xxxxxxxxxx
66
// Left and right dividers
let x1, x2;
// Speed of left and right dividers
let xspeed1, xspeed2;
// Range of randomly generated xspeeds
let range = 5;
// Range of alpha
let low = 0;
let high = 255;
// Alpha value
let a = low;
// Fade-in speed
let aspeed = 0.5;
// Background color
let bg = high;
// Margin
let m = 120;
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
noCursor();
randomSeed(0);
reset();
}
function reset() {
console.log("RESET");
a = low;
x1 = width * random(0.1, 0.5);
x2 = width - x1;
xspeed1 = random(-range, range); //random(1) > 0.5 ? -range : range;
// Go the opposite way
xspeed2 = random(-range, range); //xspeed1 > 0 ? -range : range;
}
function draw() {
background(bg);
// Is the background white or black?
// Make it the oppposite color
fill(bg > low ? low : high, a);
rect(0, 0, x1, height);
rect(x2, 0, width - x2, height);
a += aspeed;
// If fully faded in
if (a > high + m || a < low - m) {
x1 += xspeed1;
x2 += xspeed2;
if ((x1 > width || x1 < 0) && (x2 > width || x2 < 0)) {
if (x1 > width || x2 < 0) {
// If the background was white, make it black
bg = bg > low ? low : high;
console.log(bg);
}
reset();
}
}
//background(0);
}