xxxxxxxxxx
let circleX; // X coordinate of center of our circle
let velocityX; // velocity of the circle in the X dimension
let diameter; // Diameter of our circle in pixels
let circles = [];
let circleY;
let velocityY;
//e
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
for (let i = 0; i < 5; i++)
{
circles.push(new Circle(random(0,windowWidth),random(0,windowHeight)));
}
noStroke();
// Initialize variables
circleX = 10;
diameter = 80;
velocityX = 8;
circleY = 10;
velocityY = 8;
}
function draw() {
// Erase screen
background(0);
for (let i = 0; i < circles.length; i++)
{
circles[i].draw();
}
if (mouseIsPressed)
{
// If we clicked within the circle change color to red
if (dist(mouseX,mouseY,circleX,circleY) < (diameter/2))
{
fill(255,0,0);
}
else {
}
}
ellipse(circleX, circleY, diameter, diameter);
}
class Circle {
constructor(x,y) {
this.x = x;
this.y = y;
this.r = random(0,255);
this.g = random(0,255);
this.b = random(0,255);
this.a = random(0,255);
this.radius = random(5,windowHeight/2);
this.velocityX = random(1,10);
this.velocityY = random(1,10);
}
draw() {
this.r = this.r + 1;
this.g = this.g + 1;
this.b = this.b + 1;
if (this.r > 255)
this.r = 0;
if (this.g > 255)
this.g = 0;
if (this.b > 255)
this.b = 0;
fill(this.r, this.g, this.b, this.a);
this.x=this.x+this.velocityX;
this.y=this.y+this.velocityY;
// Check if we reached the edge of the screen. If so, reverse velocity
if (this.y >= windowHeight)// Off right side of the screen
this.velocityY=this.velocityY*-1;
if(this.x >=windowWidth)
this.velocityX=this.velocityX*-1;
{
}
if (this.x <= 0) // Off the left side of the screen
{
this.velocityX = this.velocityX * -1;
}
if (this.y <= 0)// Off right side of the screen
{
this.velocityY = this.velocityY * -1;
}
ellipse(this.x, this.y, this.radius*2, this.radius*2);
}
}