xxxxxxxxxx
96
// Creating an array to store the flowers
let flowers = [];
//variables
//To make sure only 10 flowers spawn each time
let maxFlowers = 10;
//setting a uniform speed for the spinning flowers
let spinSpeed = 0.01;
let startX = 0;
let startY = 0;
function setup() {
createCanvas(400, 400);
}
function draw() {
//refreshing the background each time
background("#9dc183");
//iterating over every item in the flower array
for (let flower of flowers) {
push();
//makes the drawing start at the center of the flower
translate(flower.x, flower.y);
//makes the flower spin from the center
rotate(flower.angle);
drawFlower(flower.size, flower.color);
pop();
flower.angle += spinSpeed;
}
//if statement that will call the generate flower function as long as the flower array isnt longer than 10 and will generate every second
if (flowers.length < maxFlowers && frameCount % 60 == 0) {
generateFlower();
}
}
//function to generate the flowers
function generateFlower() {
let flower;
let PositionClose = false;
//uses a while loop to randomly generate the flower's properties
while (!PositionClose) {
flower = {
x: random(50, width - 50),
y: random(50, height - 50),
size: random(40, 80),
//choosing a pastel color pallette
color: color(random(200, 255), random(200, 255), random(200, 255)),
angle: 0
};
//checks if the flowers are overlapping
PositionClose = true;
//compares the flower with other flowers in the array
for (let otherFlower of flowers) {
let distance = dist(flower.x, flower.y, otherFlower.x, otherFlower.y);
//adjusts the condition for flower spacing so that flowers dont overly overlap
if (distance < (flower.size + otherFlower.size)*0.8) {
PositionClose = false;
break;
}
}
}
//adding to the flowers array
flowers.push(flower);
}
//function to draw the flower
function drawFlower(size, petalColor) {
//adding petals
let petalCount = 10;
let petalLength = size * 0.8;
let petalWidth = size * 0.3;
stroke(20);
strokeWeight(2);
fill(petalColor);
//for loop to draw petals
for (let i = 0; i < petalCount; i++) {
push();
//making a consistent spacing between petals
rotate(TWO_PI / petalCount * i);
ellipse(petalLength / 2, 0, petalLength, petalWidth);
pop();
}
//draws flower center
fill(255, 204, 0);
stroke(20);
ellipse(0, 0, size * 0.4);
}