xxxxxxxxxx
57
// Circle class
class Circle {
constructor(x, y, r, c) {
this.x = x;
this.y = y;
this.r = r;
this.c = c;
}
// Method to draw the circle
show() {
fill(this.c);
ellipse(this.x, this.y, this.r);
}
// Method to update the circle's size
update() {
this.r = this.r + random(-5, 5);
}
}
// Array of circles
let circles = [];
function setup() {
createCanvas(500, 500);
// Create 100 circles with random positions, sizes, and colors
for (let i = 0; i < 100; i++) {
let x = random(width);
let y = random(height);
let r = random(10, 15);
let c = color(random(30), random(70), random(255));
let circle = new Circle(x, y, r, c);
circles.push(circle);
}
}
function draw() {
background(0);
frameRate(60);
// Update and draw each circle in the array
for (let i = 0; i < circles.length; i++) {
circles[i].update();
circles[i].show();
}
}
//clear the screen and reset the animation by pressing space bar
function keyPressed() {
if (key === "space") {
circle.reset();
}
}