SketchBook

ball.balls

var rad = 40;
var xpos, ypos;

var xspeed = 2.8;
var yspeed = 2.2;

var xdirection = 1;
var ydirection = 1;

function setup() {
createCanvas(windowWidth,
windowHeight);
noStroke();
ellipseMode(RADIUS);

xpos = width / 2;
ypos = height / 2;
}

function draw() {
background(25, 50, 150);
fill(67, 160, 76);

xpos = xpos + xspeed * xdirection;
ypos = ypos + yspeed * ydirection;

if (xpos > width - rad || xpos < rad) {
xdirection *= -1;
}

if (ypos > height - rad || ypos < rad) {
ydirection *= -1;
}

rect(xpos, ypos, rad, rad);
}