xxxxxxxxxx
40
let treeHeight;
let treeWidth;
let verticalLeafSpacing;
let horizontalLeafSpacing;
function setup() {
createCanvas(400, 400);
//Randomize tree variables
treeHeight=random(220,320);
treeWidth=random(25,45);
verticalLeafSpacing=random(30, 45);
horizontalLeafSpacing=random(30, 50);
}
function draw() {
background(24);
fill(0,250,0);
drawTree(200, 200);
}
//Draw tree
function drawTree(x, y){
//Draw trunk
rectMode(CENTER);
fill(130, 80, 50);
rect(x, y+30, treeWidth, treeHeight);
//Draw leaves
fill(0, 250, 0);
for (let i=0; i<5; i++) {
drawTriangle(x, y-treeHeight/2 + 50+verticalLeafSpacing*i, treeWidth*2+i*horizontalLeafSpacing, 50+i*5);
}
}
//Draw triangle
function drawTriangle(x, y, width, height) {
triangle(x-width/2, y, x, y-height, x+width/2, y);
}