xxxxxxxxxx
27
let x = 200;
let xSpeed = 5;
let colorr = 127.5; // Midpoint of color range
let colorSpeed;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
fill(colorr);
ellipse(x, 200, 30, 30);
colorSpeed = (255 * xSpeed) / width;
x += xSpeed;
colorr += colorSpeed;
if (x > width || x < 0) {
xSpeed *= -1;
colorSpeed *= -1;
}
colorr = constrain(colorr, 0, 255);
}