xxxxxxxxxx
25
let canW = 400, canH = 400;
function setup() {
createCanvas(canW, canH);
}
let x=40,y=200, radius = 25;
let incX = 4, incY = 5;
function changeDir() {
if(x >= canW - 8 || x <= 0 + 8) {// we added 8 because ball was rebounding after going some pixels deep inside the boundary walls
incX *= (-1);
} else if(y >= canH - 8 || y <= 0 + 8) {
incY *= (-1);
}
}
function draw() {
background(0);
strokeWeight(0);
fill(200,100,200);
x += incX;
y += incY;
changeDir();
circle(x,y,radius);
}