
previous

page 15
next
adding the trunk

function tree() {
var trunk=150;
bk(trunk);
fd(trunk); vee(100);
}
function test() {
tree();
}
var trunk=150;
bk(trunk);
fd(trunk); vee(100);
}
function test() {
tree();
}
Looking good but we need to move it all down. This means we need to be able to move without drawing a line.
So, two new functions: pu() and pd() for pen up and pen down.
We'll also need a new global g_pen so fd() knows what to do.
function pu() {
g_pen='up';
}
function pd() {
g_pen='down';
}
function fd(d) {
g_ctx.beginPath();
g_ctx.moveTo(g_x,g_y);
var rad=g_h*Math.PI/180.0;
var dx=g_step*d*Math.sin(rad);
var dy=-g_step*d*Math.cos(rad);
g_x=g_x+dx; g_y=g_y+dy;
if (g_pen=='down') {
g_ctx.lineTo(g_x,g_y);
g_ctx.stroke();
} else {
g_ctx.moveTo(g_x,g_y);
}
}
g_pen='up';
}
function pd() {
g_pen='down';
}
function fd(d) {
g_ctx.beginPath();
g_ctx.moveTo(g_x,g_y);
var rad=g_h*Math.PI/180.0;
var dx=g_step*d*Math.sin(rad);
var dy=-g_step*d*Math.cos(rad);
g_x=g_x+dx; g_y=g_y+dy;
if (g_pen=='down') {
g_ctx.lineTo(g_x,g_y);
g_ctx.stroke();
} else {
g_ctx.moveTo(g_x,g_y);
}
}
previous
next