I have the below (tree drawing) code in Javascript. I found it online, I have some more work with it, and I want to include it in a for loop. But I want the translate
translate (width / 2, height / 2 );
to be inside the function (not in draw). I didn’t know how to do it. Can you help me to do that?
function setup()
{
createCanvas(windowWidth, windowHeight);
}
function draw ()
{
background (100);
translate (width / 2, height / 2 );
branch(80);
}
function branch (len)
{
for (var j = 0; j < 10; j++)
{
branch[j] =
{
xPos: -2000 + (j * 200),
yPos: height/2 + 95
};
}
noLoop ();
angleMode(DEGREES);
push ();
if (len > 10)
{
strokeWeight (map(len, 10, 100, 1, 15));
stroke (70, 40, 20);
line (0,0,0, -len);
translate (0, -len);
rotate (random (-20, -30));
branch (len * random(0.7, 0.9));
rotate (random (50, 60));
branch (len * random(0.7, 0.9));
}
else
{
var r = 80 + random (-20, 20);
var g = 120 + random (-20, 20);
var b = 40 + random (-20, 20);
fill (r, g, b, 150);
noStroke();
ellipse (0, 0, 10);
beginShape ();
for (var i = 135; i > 40; i--)
{
var rad = 15;
var x = rad * cos (i);
var y = rad * sin (-i) +20;
vertex (x,y)
}
endShape (CLOSE);
}
pop ();
}
I need to move the translation to inside the branch function.