xxxxxxxxxx
43
// for red, green, and blue color values
let r, g, b;
var xpos = 100;
var speed = 1;
function setup() {
createCanvas(400, 400);
// Pick colors randomly
r = random(255);
g = random(255);
b = random(255);
}
function draw() {
background(127);
xpos = xpos + speed;
if ((xpos > width - 100) || (xpos < 100)) {
speed = speed * -1;
}
// Draw a circle
strokeWeight(2);
stroke(r, g, b);
fill(r, g, b, 127);
ellipse(xpos, 200, 200);
}
// When the user clicks the mouse
function mousePressed() {
// Check if mouse is inside the circle
let d = dist(mouseX, mouseY, xpos, 200);
if (d < 100) {
// Pick new random color values
r = random(255);
g = random(255);
b = random(255);
}
}