xxxxxxxxxx
48
let goLeft;
let speed=3;
function setup() {
createCanvas(400, 400);
x=width/2;
}
function draw() {
background(220);
// Version1_write 2 modes
// if(goLeft)
// x--
// else
// x++
//when some happen -> change mode
// if(x>width)
// goLeft=true
// else if(x<0)
// goLeft=false
//--------------
//version_2
// if (goLeft) x--
// else x++
// if ( x > width || x<0){
// goLeft=!goLeft}
//-----------------
let circleColor=0
if (goLeft){ x-=speed;
circleColor=circleColor+x*255/width }
else{x+=speed;
circleColor=circleColor+x*255/width}
if ( x > width || x<0){
goLeft=!goLeft}
fill(circleColor)
ellipse(x,height/2,50,50)
}