xxxxxxxxxx
88
let numPetals = 10; // Number of petals in one layer
let amplitude = 50; // Amplitude of the oscillation
let time = 0; // Time variable for oscillation
let layers = 3; // Number of layers of petals
let layerGap = 40; // Distance between each layer of petals
let stemHeight = 200; // Height of the stem
function setup() {
createCanvas(600, 600);
angleMode(RADIANS); // Use radians for angle calculation
}
function draw() {
background(30); // Dark background for contrast
// Move origin to the center of the canvas (flower's base)
translate(width / 2, height / 2);
// Draw the flower
drawFlower();
// Move down to draw the stem right below the flower
drawStemAndLeaves();
}
function drawFlower() {
// Loop through multiple layers of petals
for (let l = 0; l < layers; l++) {
let layerOffset = l * layerGap; // Offset for each layer
// Loop to draw petals in the current layer
for (let i = 0; i < numPetals; i++) {
let angle = TWO_PI / numPetals * i;
let petalOscillation = amplitude + sin(time + angle + l) * 20;
push();
rotate(angle + time * 0.02 * (l + 1)); // Slow rotation over time for each layer
// Dynamic petal size with a slight random factor
let petalWidth = petalOscillation * (1 + noise(time * 0.1 + i) * 0.2);
let petalHeight = petalOscillation * 2;
// Color variation based on petal size and layer
let petalColor = map(petalOscillation, amplitude - 20, amplitude + 20, 150, 255);
fill(petalColor, 100 + l * 50, 200 - l * 50);
stroke(255, 100); // Soft stroke for outline
strokeWeight(1 + sin(time + i) * 2); // Dynamic stroke weight
// Draw each petal with ellipse
ellipse(layerOffset, 0, petalWidth, petalHeight);
pop();
}
}
// Update time to animate oscillation and movement
time += 0.03;
}
function drawStemAndLeaves() {
// Move down to draw the stem below the flower
translate(0, stemHeight / 2); // Move down to place the stem
// Draw the stem as a simple vertical line right below the flower
push();
stroke(100, 200, 100); // Greenish stem color
strokeWeight(8);
line(0, 0, 0, stemHeight); // Stem going downwards from the flower's base
// Draw leaves closer to the stem
drawLeaf(-20, stemHeight / 2, 30, 80, time * 0.5); // Left leaf (closer to stem)
drawLeaf(20, stemHeight / 2, 30, 80, -time * 0.5); // Right leaf (closer to stem)
pop();
}
function drawLeaf(x, y, leafWidth, leafHeight, leafOscillation) {
push();
translate(x, y); // Position the leaf along the stem
rotate(sin(leafOscillation) * 0.3); // Oscillating leaf movement
fill(100, 250, 100); // Green color for leaves
noStroke();
// Draw an ellipse as a leaf
ellipse(0, 0, leafWidth, leafHeight);
pop();
}