xxxxxxxxxx
115
let leaves = [];
let colors = [['#FF6347', '#FF4500', '#FFD700'], ['#6A5ACD', '#9370DB', '#BA55D3'], ['#32CD32', '#3CB371', '#228B22']];
let currentColorSet = colors[0];
let initialLeafCount = 100; // Initial number of leaves
let foliageSize = 180; // Initial size of the foliage
let foliageShrinkRate = 0.2; // Slower shrink rate for longer-lasting foliage
function setup() {
createCanvas(400, 400);
for (let i = 0; i < initialLeafCount; i++) {
leaves.push(new Leaf());
}
}
function draw() {
background('#87CEEB'); // Sky blue background
// Draw landscape
fill('#7CFC00'); // Grass ground color
rect(0, 350, 400, 50); // Grass ground
// Draw tree trunk
drawTrunk();
// Draw foliage if there's size left
if (foliageSize > 0) {
drawFoliage();
}
// Update and display leaves
for (let i = leaves.length - 1; i >= 0; i--) {
leaves[i].fall();
leaves[i].avoidMouse();
leaves[i].show();
if (leaves[i].y > height) {
leaves.splice(i, 1); // Remove leaf when it falls out of canvas
// Only shrink foliage if there are leaves falling
if (leaves.length > 0) {
foliageSize = max(foliageSize - foliageShrinkRate, 0);
}
}
}
// Ensure foliage vanishes as the last leaf falls
if (leaves.length == 0) {
foliageSize = max(foliageSize - foliageShrinkRate, 0);
}
}
function drawTrunk() {
fill('#8B4513'); // Trunk color
beginShape();
vertex(180, 400);
vertex(180, 250);
vertex(190, 200);
vertex(210, 200);
vertex(220, 250);
vertex(220, 400);
endShape(CLOSE);
}
function drawFoliage() {
// Dynamic foliage based on remaining size
for (let i = 0; i < 3; i++) {
fill(currentColorSet[i]);
ellipse(200, 160 - i * 20, foliageSize + i * 30, foliageSize - i * 10);
}
}
class Leaf {
constructor() {
// Expanded horizontal range to match foliage
this.x = random(200 - foliageSize/2, 200 + foliageSize/2);
this.y = random(160 - 20, 160);
this.speed = random(1, 3);
this.sizeX = random(8, 12);
this.sizeY = random(4, 6);
this.color = random(currentColorSet);
}
fall() {
this.y += this.speed;
}
show() {
noStroke();
fill(this.color);
ellipse(this.x, this.y, this.sizeX, this.sizeSizeY);
}
avoidMouse() {
let mousePos = createVector(mouseX, mouseY);
let leafPos = createVector(this.x, this.y);
let d = dist(mouseX, mouseY, this.x, this.y);
if (d < 50) {
let move = p5.Vector.sub(leafPos, mousePos);
move.setMag(5);
leafPos.add(move);
this.x = leafPos.x;
this.y = leafPos.y;
}
}
}
function mousePressed() {
// Cycle through color sets for variety
currentColorSet = colors[(colors.indexOf(currentColorSet) + 1) % colors.length];
// Update leaf colors to match the new palette
for (let leaf of leaves) {
leaf.color = random(currentColorSet);
}
}