xxxxxxxxxx
252
let options = {
// overall appearance
transparent : false, // is the background transparent
colorscheme : 2, // 2 is dark theme, 3 is light theme
scale : 0.2,
w : 200,
h : 200,
text : true,
//animation
speed : 20,
animate : true,
// shape of the tree
steps : 9, //how many branches
width1 : 0.3, // trunk width ratio at base
width2 : 0.2, // trunk width ratio at tip
branchlength : [0.8,0.7], //ratio of length of branches
branchangle : [20,50], //angle of branches
// branchlength : [0.7,0.7],
// branchangle : [30,30],
leaflength : 2, //length of leaf compared to last branch
//dummy
dummy : 0
};
let iter = 0
let i = 0
let c = 0
let options1 = {
// colorscheme
transparent : options.transparent, // is the background transparent
colorscheme : options.colorscheme,
//size of the logo
scale : options.scale,
w : options.w,
h : options.h,
// shape of the tree
steps : options.steps, //how many branches
width1 : options.width1, // trunk width ratio at base
width2 : options.width2, // trunk width ratio at tip
branchlength : options.branchlength, //ratio of length of branches
branchangle : options.branchangle, //angle of branches
// branchlength : [0.7,0.7],
// branchangle : [30,30],
leaflength : options.leaflength, //length of leaf compared to last branch
//dummy
dummy : 0
}
let colorscheme1 = { //black-white-green
background : [0,0,0], //black
trunk : [255,255,255], //white
leaf : [0, 255 , 100], //green
numbers : [255,255,255] //white
}
let colorscheme2 = { //blue-on-white
background : [255], //white
trunk : [0,40,82], //navy blue
leaf : [0,40,82], //navy blue
numbers : [0,40,82] //navy blue
}
let colorscheme3 = { //yellow-highlight-dark
background : [0], //white
trunk : [0xE5,0xE5,0xE5], //light gray
leaf : [0xFC,0xA3,0x11], //yellow
numbers : [0xE5,0xE5,0xE5] //light gray
}
let colorscheme4 = { //yellow-highlight-light
background : [255], //white
//trunk : [0x14,0x21,0x3D], //navy blue
trunk : [0,40,82], //navy blue
leaf : [0xFC,0xA3,0x11], //yellow
numbers : [0xE5,0xE5,0xE5], //light gray
numbers : [0,40,82] //light gray
}
let colorscheme5 = { //yellow-highlight-light
background : [0], //white
trunk : [0xf1,0xde,0xde], //navy blue
leaf : [0x2F,0x80,0x35], //yellow
numbers : [0xE5,0xE5,0xE5] //light gray
}
colorschemes = [colorscheme1, colorscheme2, colorscheme3, colorscheme4, colorscheme5]
//draw one quadrilateral for trunk
function trunkUnit(angle, length, basepoint){
let x = 0,y=0
xs = [x-length*options1.width1/2, x-length*options1.width2/2, x+length*options1.width2/2, x+length*options1.width1/2]
ys = [y,y-length, y-length, y]
push()
noStroke()
fill(colors.trunk)
translate(basepoint[0], basepoint[1])
rotate(angle)
quad(xs[0], ys[0], xs[1], ys[1], xs[2], ys[2], xs[3], ys[3])
pop()
newbasepoint = [basepoint[0]+length*sin(angle), basepoint[1] - length*cos(angle)]
return newbasepoint
}
//draw a leaf
function leaf(basepoint, length, angle){
push()
l1 = length*0.6
l2 = length*0.3
x = basepoint[0]
y = basepoint[1]
translate(x,y)
rotate(angle)
noStroke()
fill(colors.leaf)
bezier(0,length*0.1,-l1, -l2, 0, -length, 0, -length)
bezier(0,length*0.1,+l1, -l2, 0, -length, 0, -length)
pop()
}
//draw a random root
function root(startx, starty){
endx = startx
endy = starty + random()*(starty-options.h)
push()
strokeWeight(3)
stroke(colors.trunk)
line(startx, starty, endx, endy)
pop()
}
//draw one trunk and two branches
function branchUnit(angle, length, basepoint, step = 0, lflag = true, rflag = true){
//determine the branch angles and lengths
b1length = options1.branchlength[step%2]
b1angle = options1.branchangle[step%2]
b2length = options1.branchlength[((step%2)+1)%2]
b2angle = options1.branchangle[((step%2)+1)%2]
//draw left branch
lbase = trunkUnit(angle - b1angle, length*b1length, [basepoint[0], basepoint[1]])
// if (lflag && step<3 && step > 0)
// root(basepoint[0], basepoint[1])
if (step<options1.steps)
branchUnit(angle - b1angle, length*b1length, lbase, step+1, lflag, false)
else
leaf(lbase, length*options1.leaflength, angle-b1angle)
//draw right branch
rbase = trunkUnit(angle + b2angle, length*b2length, [basepoint[0], basepoint[1]])
// if (rflag && step<3 && step>0)
// root(basepoint[0], basepoint[1])
if (step<options1.steps)
branchUnit(angle + b2angle, length*b2length, rbase, step+1, false, rflag)
else
leaf(rbase, length*options1.leaflength, angle+b2angle)
}
function drawBase(){
stroke(colors.trunk)
line(options1.w/4, options1.h, options1.w*0.75, options1.h)
push()
textFont('Times New Roman')
strokeWeight(0)
fill(colors.trunk)
textSize(options1.w/15)
textAlign(CENTER, TOP)
text('IIIT Hyderabad\nSilver Jubilee', options1.w/2, options1.h+options1.width1*5)
pop()
}
function drawTrunk(length, width){
newbasepoint = trunkUnit(0, length*options1.scale, [width/2,length])
branchUnit(0, length*options1.scale, newbasepoint)
}
function drawText(txt = '25', sz = options1.w/6){
push()
textFont('Curlz')
stroke(colors.trunk)
fill(colors.numbers)
textAlign(LEFT, BASELINE)
textSize(sz);
text(txt, options1.w/4, options1.h - options1.h/70);
pop()
}
function setup() {
c = createCanvas(windowWidth, windowHeight);
angleMode(DEGREES)
options1.w = windowWidth
options1.h = windowHeight
w = min(options1.w, options1.h)
options1.w = w
options1.h = w*3/4
// options1.w = 500
// options1.h = 500
colors = colorschemes[options1.colorscheme]
if (options1.transparent == true){
colors.background.push(0)
}
}
function draw() {
i = i + 1
background(colors.background);
//draw the baseline and text
if (options.text)
drawBase()
if (options.animate){
//calculate iter
maxiter = options.steps/2*options.speed
iter = (iter+1)%maxiter
//draw the tree
options1.steps = int(iter/options.speed)*2
minsize = 0.05
options1.scale = iter/maxiter * (options.scale-minsize) + minsize
}
else options1.steps = options.steps-1
drawTrunk(options1.h, options1.w)
//text : 0 to 25
if (options.text)
if (options.animate){
txt = int(iter/maxiter*25)+1
drawText(txt)
}
else drawText(25)
//save as png
// if (i==1)
// saveCanvas(c, 'MyCanvas.png')
//save many pngs
// if (i<maxiter)
// saveCanvas(c, 'light' + i , '.png')
}