xxxxxxxxxx
92
var barkbrown;
var leafgreen;
var trees = [];
function setup() {
createCanvas(400, 400);
barkbrown = color("rgba(165,42,42,0.1)");
leafgreen = color("rgba(0,100,0,0.27)");
spawnTrees();
}
function spawnTrees() {
for (var x = -50; x < width + 50; x += random() * 100) {
trees.push(new Tree(x, 400, 12, null, random(), random()));
}
}
function mouseClicked() {
trees.splice(1);
spawnTrees();
loop();
}
class Tree {
constructor(x, y, d, bias, tstart, dt) {
this.sapling = createVector(x, y);
this.d = d;
this.up = bias ? bias : createVector(0, -3);
this.t = tstart ? tstart : 0;
this.dt = dt ? dt : 0.005;
}
sprout(d) {
if (d < 1) return;
var root = this.sapling.copy();
var n;
switch (true) {
case d == 0:
return;
case d == 1:
n = floor(noise(this.t) * 6);
break;
case d == 2:
n = floor(noise(this.t) * 5);
break;
case d >= 3:
n = floor(noise(this.t) * 4);
break;
case d >= 5:
default:
n = floor(noise(this.t) * 3);
break;
}
stroke(lerpColor(leafgreen, barkbrown, d/this.d));
strokeWeight(lerp(1,10, d/this.d))
for (var i = 0; i < n; i++) {
var mltplier = createVector(root.x, root.y)
.setHeading(
this.up.heading() + map(noise(root.x * root.y + this.t), 0, 1, -1.6, 1.6)
)
.setMag(25);
this.sapling.add(mltplier);
line(root.x, root.y, this.sapling.x, this.sapling.y);
// circle(root.x, root.y, 5);
if(random()>0.9){this.sprout(d - floor(1+noise(this.t)*3));}
this.sprout(d -1);
this.sapling = root.copy();
this.t += this.dt;
}
}
plant() {
this.sprout(this.d);
}
}
function draw() {
background(220);
for (const t of trees) {
t.plant();
}
noLoop();
}