xxxxxxxxxx
84
let flowers = []; // Array to store flower objects
function setup() {
createCanvas(800, 800);
stroke(1);
// Generate initial flowers
for (let i = 0; i < 10; i++) {
flowers.push(new Flower(random(width), random(height)));
}
}
function draw() {
background(200, 50, 150); // Dark background for contrast
// Update and display all flowers
for (let flower of flowers) {
flower.grow();
flower.display();
}
// Occasionally add new flowers
if (random(1) < 0.01) {
flowers.push(new Flower(random(width), random(height)));
}
}
function mousePressed() {
// Create a burst of flowers around the mouse position
for (let i = 0; i < 10; i++) {
let angle = random(TWO_PI);
let distance = random(50, 150);
let x = mouseX + cos(angle) * distance;
let y = mouseY + sin(angle) * distance;
flowers.push(new Flower(x, y));
}
}
// Flower class
class Flower {
constructor(x, y) {
this.x = x; // Position x
this.y = y; // Position y
this.size = random(5, 10); // Initial size of the flower (smaller to start)
this.maxSize = random(30, 80); // Maximum size the flower can grow to
this.petalCount = int(random(5, 12)); // Number of petals
this.colors = [
color(random(200, 255), random(100, 200), random(100, 200)), // Petal color
color(random(255), random(200), random(100)), // Center color
];
this.growthRate = random(0.1, 0.3); // Speed at which the flower grows
this.bloomed = false; // Whether the flower has fully bloomed
this.angle = random(TWO_PI); // Random rotation angle for variety
}
grow() {
if (this.size < this.maxSize) {
this.size += this.growthRate; // Gradually increase size
} else {
this.bloomed = true; // Mark as bloomed when max size is reached
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle);
// Draw petals in a circular arrangement
fill(this.colors[0]);
for (let i = 0; i < this.petalCount; i++) {
let angle = (TWO_PI / this.petalCount) * i;
let petalX = cos(angle) * this.size;
let petalY = sin(angle) * this.size;
ellipse(petalX, petalY, this.size); // Petal shape
}
// Draw the center of the flower
fill(this.colors[1]);
ellipse(0, 0, this.size);
pop();
}
}