For the original question, I find another solution with the use of sin
function setup() {
createCanvas(400, 400);
}
function draw() {
angleMode(DEGREES);
let s = sin(frameCount);
let sinColor = map(s, -1, 1, 0, 255);
background(sinColor);
}
edit: there was an inappropriate word so the post was flagged. No wonder there are no words like that on this forum. 0 tolerance policy. I suppose that is a good way to ensure a safe learning environment for younger people
Nice demo, @glv. The sinusoidal oscillation enables the transition to proceed rapidly through the medium gray center of each phase, and slowly around the black and white endpoints. The sinusoidal concept is also useful for other phenomena, such as motion, randomization, and spacings of drawn objects.
Edit (December 18, 2020):
Here is a revised version of the code, so that we can monitor the numerical value of the background color:
function setup() {
createCanvas(200, 160);
frameRate(10);
}
function draw() {
let sinColor = int(127.5+ 127.5*sin(frameCount*TAU/720));
background(sinColor);
meter(sinColor);
}
function meter(status) {
push();
fill(0);
rect(10, height - 50, 40, 30);
fill(255);
text(status, 18, height - 30);
pop();
}
Due to the sine curve, the numbers change slowly in the vicinity of 0 and 255 and rapidly in the vicinity of 127.
I want to achieve something similar but black and white color should change only when the user clicks. my reference is this website - https://darkblackscreen.com. I have tried capturing click event but not sure how to proceed. Please help.