xxxxxxxxxx
28
var x, y, vx, vy;
var rad = 10;
var speed = 5;
function setup() {
createCanvas(400, 400);
x = width / 2;
y = height / 2;
var a = Math.random() * 100;
vx = cos(a) * speed;
vy = sin(a) * speed;
}
function draw() {
background(250);
fill(0)
circle(x, y, rad);
x += vx;
y += vy;
if(x >= width - rad || x <= rad) {
x = constrain(x, rad, width - rad);
vx = -vx;
}
if(y >= height - rad || y <= rad) {
y = constrain(y, rad, width - rad);
vy = -vy;
}
}