xxxxxxxxxx
108
let leaves = [];
let colorPalettes = [
['#FFD700', '#FFA500', '#FF8C00'], // Gold to Orange
['#ADD8E6', '#87CEEB', '#00BFFF'], // Light to Dark Blue
['#90EE90', '#32CD32', '#228B22'], // Light to Dark Green
];
let currentPaletteIndex = 0; // To cycle through the palettes
function setup() {
createCanvas(400, 400);
// Initialize fewer leaves for a less dense effect
for (let i = 0; i < 100; i++) {
leaves.push(new Leaf());
}
}
function draw() {
background('#87CEEB'); // Sky blue background
// Draw landscape
fill('#32CD32'); // Green grass ground
rect(0, 350, 400, 50); // Simple ground
// Draw tree
drawTree();
// Update and display leaves
for (let leaf of leaves) {
leaf.fall();
leaf.avoidMouse();
leaf.show();
}
}
function drawTree() {
// Complex trunk
fill('#8B4513'); // Trunk color
beginShape();
vertex(180, 400);
vertex(180, 250); // Start higher for a taller trunk
vertex(190, 200); // Adjust for a taller appearance
vertex(210, 200); // Adjust for a taller appearance
vertex(220, 250); // End higher for a taller trunk
vertex(220, 400);
endShape(CLOSE);
// Multiple layers of larger foliage with varying colors for a taller tree
let colors = colorPalettes[currentPaletteIndex];
for (let i = 0; i < colors.length; i++) {
fill(colors[i]);
ellipse(200 + i * 15, 160 - i * 20, 180 + i * 10, 140 - i * 10);
}
}
class Leaf {
constructor() {
this.x = random(160, 240);
this.y = random(120, 190);
this.speed = random(1, 3);
this.sizeX = random(8, 12);
this.sizeY = random(4, 6);
// Select a color from the current palette, allowing for slight variation
let baseColor = color(random(colorPalettes[currentPaletteIndex]));
this.color = color(red(baseColor), green(baseColor), blue(baseColor), random(150, 200));
}
fall() {
this.y += this.speed;
if (this.y > height) {
this.x = random(160, 240);
this.y = random(120, 190);
// Recalculate color for new leaves
let baseColor = color(random(colorPalettes[currentPaletteIndex]));
this.color = color(red(baseColor), green(baseColor), blue(baseColor), random(150, 200));
}
}
show() {
noStroke();
fill(this.color);
ellipse(this.x, this.y, this.sizeX, this.sizeY);
}
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 the color palettes
currentPaletteIndex = (currentPaletteIndex + 1) % colorPalettes.length;
// Update leaves with the new color palette
leaves.forEach(leaf => {
let baseColor = color(random(colorPalettes[currentPaletteIndex]));
leaf.color = color(red(baseColor), green(baseColor), blue(baseColor), random(150, 200));
});
}