xxxxxxxxxx
40
let x = 200;
let color = 0;
let xspeed = 2;
let colorValue = 1;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
fill(color);
ellipse(x, 200, 50, 50);
//moves to the right by xspeed pixel
x += xspeed;
//conditional if larger than 400, xspeed change direction
if (x > width) {
xspeed = xspeed * -1;
//conditional if x is less than zero, xpeed gains direction
} else if (x < 0) {
xspeed = xspeed * -1;
}
//increase y value by yspeed
color += colorValue;
//if y value hits limit at 255, reduce number
if (color > 255) {
colorValue = colorValue * -1;
//if y value less than 0, increase number
} else if (color < 0) {
colorValue = colorValue * -1;
}
}