xxxxxxxxxx
308
let leaves = [];
let flowers = [];
let particles = [];
let patterns = [];
let currentPatternIndex = 0;
let backgroundImage;
function preload() {
// Load your background image
backgroundImage = loadImage('background.jpeg');
}
function setup() {
createCanvas(800, 600);
let a = createVector(width / 2, height);
let b = createVector(width / 2, height - 150);
let root = new Branch(a, b);
patterns.push(new FractalTree(width / 2, height));
// Generate some flowers
for (let i = 0; i < 10; i++) {
let flower = new KoreanFlower(random(width), random(height));
flowers.push(flower);
patterns.push(flower);
}
// Generate some particles
for (let i = 0; i < 100; i++) {
particles.push(new Particle(random(width), random(height)));
}
}
function mousePressed() {
if (currentPatternIndex === 0) {
let flower = new KoreanFlower(mouseX, mouseY);
flowers.push(flower);
patterns.push(flower);
} else if (currentPatternIndex === 1) {
let tree = new FractalTree(mouseX, mouseY);
patterns.push(tree);
// Generate additional flowers around the tree
for (let i = 0; i < 10; i++) {
let flower = new KoreanFlower(tree.x + random(-50, 50));
flowers.push(flower);
patterns.push(flower);
}
}
}
function draw() {
// Draw a semi-transparent background to create trails
image(backgroundImage, 0, 0, width, height);
background(0, 10);
// Attract leaves towards the average position of flowers
let attractionForce = createVector(0, 0);
let flowerCount = 0;
for (let i = 0; i < flowers.length; i++) {
if (flowers[i] instanceof KoreanFlower) {
flowers[i].display();
flowers[i].grow();
attractionForce.add(createVector(flowers[i].x, flowers[i].y));
flowerCount++;
}
}
// Update and display particles
for (let i = 0; i < particles.length; i++) {
particles[i].wander();
// Flee from the mouse position
particles[i].flee(createVector(mouseX, mouseY));
particles[i].update();
particles[i].display();
}
// Draw falling leaves
for (let i = 0; i < leaves.length; i++) {
fill(255, 0, 100, 100);
noStroke();
ellipse(leaves[i].x, leaves[i].y, 8, 8);
leaves[i].y -= random(0, 2); // Move leaves upward
}
// Draw patterns (trees and flowers)
for (let i = 0; i < patterns.length; i++) {
patterns[i].display();
patterns[i].grow();
}
}
// Particle class
class Particle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(random(-1, 1), random(-1, 1));
this.acceleration = createVector(0, 0);
this.maxSpeed = 1;
this.wanderRadius = 20;
this.wanderDistance = 50;
this.wanderAngle = 0;
}
wander() {
this.wanderAngle += random(-0.5, 0.5);
// Calculate the new position in a circle around the current position
let circleCenter = this.velocity.copy();
circleCenter.normalize();
circleCenter.mult(this.wanderDistance);
let offset = createVector(this.wanderRadius * cos(this.wanderAngle), this.wanderRadius * sin(this.wanderAngle));
let target = p5.Vector.add(this.position, circleCenter);
target.add(offset);
// Apply steering force towards the target
let steer = p5.Vector.sub(target, this.position);
steer.limit(0.1); // Adjust the limit for wandering behavior
this.applyForce(steer);
}
flee(target) {
let desired = p5.Vector.sub(this.position, target);
let d = desired.mag();
if (d < 50) {
// Adjust the factor to control the strength of fleeing
desired.setMag(map(d, 0, 50, this.maxSpeed, 0));
// Amplify the fleeing effect by multiplying desired vector by a factor
let amplifyFactor = 2.0; // Adjust the factor for stronger fleeing
desired.mult(amplifyFactor);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxSpeed);
this.applyForce(steer);
}
}
applyForce(force) {
this.acceleration.add(force);
}
update() {
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0); // Reset acceleration after update
// Contain particles within the canvas
this.position.x = constrain(this.position.x, 0, width);
this.position.y = constrain(this.position.y, 0, height);
}
display() {
fill(229,181,97);
ellipse(this.position.x, this.position.y,8,8)
}
}
function mousePressed() {
if (currentPatternIndex === 0) {
let flower = new KoreanFlower(mouseX, mouseY);
patterns.push(flower);
} else if (currentPatternIndex === 1) {
let tree = new FractalTree(mouseX, mouseY);
patterns.push(tree);
// Generate additional flowers around the tree
for (let i = 0; i < 10; i++) {
let flower = new KoreanFlower(tree.x + random(-50, 50));
patterns.push(flower);
}
}
}
// Rest of the code remains unchanged
class KoreanFlower {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 10;
this.outerColor = color(random(255), random(255), random(255), 150);
this.innerColor = color(random(255), random(255), random(255), 150);
this.centerColor = color(random(255), random(255), random(255), 150);
// Generate random size limits
this.minSize = random(5, 10); // Set your desired minimum size
this.maxSize = random(15, 35); // Set your desired maximum size
}
grow() {
if (this.size < this.maxSize) {
this.size += 1;
}
}
display() {
noStroke();
// Outer petals
fill(this.outerColor);
for (let i = 0; i < 6; i++) {
let angle = radians(60 * i);
let petalX = this.x + this.size * cos(angle);
let petalY = this.y + this.size * sin(angle);
ellipse(petalX, petalY, this.size * 1.5, this.size * 1.5);
}
// Inner petals
fill(this.innerColor);
for (let i = 0; i < 6; i++) {
let angle = radians(60 * i + 30);
let petalX = this.x + this.size * 0.7 * cos(angle);
let petalY = this.y + this.size * 0.7 * sin(angle);
ellipse(petalX, petalY, this.size, this.size);
}
// Flower center
fill(this.centerColor);
ellipse(this.x, this.y, this.size * 0.6, this.size * 0.6);
}
}
// Fractal Tree class
class FractalTree {
constructor(x, y) {
this.tree = [];
let a = createVector(x, y); // Adjust the y-coordinate to increase height
let b = createVector(x, y - 140); // Adjust the y-coordinate to increase height
let root = new Branch(a, b);
this.tree.push(root);
this.branchCount = 0;
this.branchLimit = 200; // Adjust the branch limit here for more leaves
this.leavesFallen = false;
}
grow() {
if (this.branchCount < this.branchLimit) {
for (let i = this.tree.length - 1; i >= 0; i--) {
if (!this.tree[i].finished) {
this.tree.push(this.tree[i].branchA());
this.tree.push(this.tree[i].branchB());
this.branchCount += 2; // Increment the counter for each new branch
}
this.tree[i].finished = true;
}
} else if (!this.leavesFallen) {
for (let i = 0; i < this.tree.length; i++) {
if (!this.tree[i].finished) {
let leaf = this.tree[i].end.copy();
leaves.push(leaf);
}
}
this.leavesFallen = true;
}
}
display() {
for (let i = 0; i < this.tree.length; i++) {
this.tree[i].show();
this.tree[i].jitter();
}
}
}
// Branch class (used by Fractal Tree)
class Branch {
constructor(begin, end) {
this.begin = begin;
this.end = end;
this.finished = false;
}
show() {
stroke(44,35,33);
strokeWeight(6); // Increase stroke weight
line(this.begin.x, this.begin.y, this.end.x, this.end.y);
}
branchA() {
let dir = p5.Vector.sub(this.end, this.begin);
dir.rotate(PI / 4);
dir.mult(0.7); // Increase the multiplier for longer branches
let newEnd = p5.Vector.add(this.end, dir);
let b = new Branch(this.end, newEnd);
return b;
}
branchB() {
let dir = p5.Vector.sub(this.end, this.begin);
dir.rotate(-PI / 6);
dir.mult(0.7); // Increase the multiplier for longer branches
let newEnd = p5.Vector.add(this.end, dir);
let b = new Branch(this.end, newEnd);
return b;
}
jitter() {
this.end.x += random(-1, 1);
this.end.y += random(-1, 1);
}
}