xxxxxxxxxx
156
let flowers = []; // Array to hold individual flower objects
let maxFlowers = 10000; // Maximum number of flowers
let followSpeed = 0.05; // Speed at which flowers follow the mouse
let flowerGenerationInterval = 100; // Time in milliseconds between generating new flowers
function setup() {
createCanvas(800, 700);
noLoop(); // Stops draw() from looping initially
loop(); // Start the draw loop to animate flowers
setInterval(generateFlowers, flowerGenerationInterval); // Continuously generate new flowers
}
function draw() {
background('#272410'); // Refresh background
// Update and draw all flowers
for (let flower of flowers) {
updateFlower(flower);
drawFlower(flower.x, flower.y, flower.size, flower.rotation, flower.type);
flower.rotation += 0.01; // Update rotation angle for animation
}
}
function mousePressed() {
// Create a new flower and add it to the array
if (flowers.length < maxFlowers) {
let size = random(30, 60);
let flowerType = floor(random(3)); // Choose a random flower type
flowers.push({
x: mouseX + random(-50, 50), // Initial position with some offset
y: mouseY + random(-50, 50),
size: size,
rotation: random(TWO_PI), // Random initial rotation
type: flowerType
});
}
}
function updateFlower(flower) {
// Move the flower towards the mouse with some randomness
let dx = mouseX - flower.x;
let dy = mouseY - flower.y;
let distance = dist(flower.x, flower.y, mouseX, mouseY);
if (distance > 1) {
flower.x += (dx / distance) * followSpeed;
flower.y += (dy / distance) * followSpeed;
}
// Add some random movement to each flower
flower.x += random(-0.5, 0.5);
flower.y += random(-0.5, 0.5);
}
function drawFlower(x, y, size, rotation, type) {
switch (type) {
case 0:
drawDaisy(x, y, size, rotation);
break;
case 1:
drawTulip(x, y, size, rotation);
break;
case 2:
drawRose(x, y, size, rotation);
break;
}
}
function drawDaisy(x, y, size, rotation) {
let petalCount = 9; // Number of petals
let petalLength = size; // Length of petals
let petalWidth = size / 3; // Width of petals
// Draw petals
stroke(0);
fill('rgb'); // Yellow color for petals
push();
translate(x, y);
rotate(rotation); // Apply rotation
for (let i = 0; i < petalCount; i++) {
push();
rotate(TWO_PI / petalCount * i);
ellipse(0, -size / 2, petalWidth, petalLength);
pop();
}
pop();
// Draw center
fill(255, 204, 0); // Yellow color for the center
noStroke();
ellipse(x, y, size / 2);
}
function drawTulip(x, y, size, rotation) {
let petalCount = 6; // Number of petals
let petalWidth = size / 2; // Width of petals
// Draw petals
stroke(0);
fill(255, 105, 180); // Pink color for petals
push();
translate(x, y);
rotate(rotation); // Apply rotation
for (let i = 0; i < petalCount; i++) {
push();
rotate(TWO_PI / petalCount * i);
ellipse(0, -size / 2, petalWidth, size); // Tulip petals are more rounded
pop();
}
pop();
// Draw center
fill(255, 180, 193); // Light pink center
noStroke();
ellipse(x, y, size / 3);
}
function drawRose(x, y, size, rotation) {
let petalCount = 10; // Number of petals
let petalWidth = size / 3; // Width of petals
// Draw petals
stroke(0);
fill('#E67DDA'); // Red color for petals
push();
translate(x, y);
rotate(rotation); // Apply rotation
for (let i = 0; i < petalCount; i++) {
push();
rotate(TWO_PI / petalCount * i);
ellipse(0, -size / 2, petalWidth, size / 1.5); // Rose petals are more elongated
pop();
}
pop();
// Draw center
fill(255); // Dark red center
noStroke();
ellipse(x, y, size / 4);
}
function generateFlowers() {
// Continuously generate new flowers near the mouse pointer
if (flowers.length < maxFlowers) {
let size = random(30, 60);
let flowerType = floor(random(3)); // Choose a random flower type
flowers.push({
x: mouseX + random(-50, 50), // Initial position with some offset
y: mouseY + random(-50, 50),
size: size,
rotation: random(TWO_PI), // Random initial rotation
type: flowerType
});
}
}