Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created August 14, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbide-public/2a514b093b3ffe7a01c1524c90734b9b to your computer and use it in GitHub Desktop.
Save carbide-public/2a514b093b3ffe7a01c1524c90734b9b to your computer and use it in GitHub Desktop.
untitled
var canvas = document.createElement('canvas')
canvas.width = 700
canvas.height = 400
canvas.style.width = '100%'
var ctx = canvas.getContext('2d');
function drawTree(x1, y1, length, angle, n){
var x2 = x1 + length * Math.cos(angle*Math.PI/180);
var y2 = y1 - length * Math.sin(angle*Math.PI/180);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = n < 2 ? "green" : "brown";
ctx.lineWidth = n - 1;
ctx.stroke();
if(n == 0) return;
drawTree(x2, y2, length*0.75, angle+27, n-1);
drawTree(x2, y2, length*0.75, angle-57, n-1);
}
drawTree(350.5, 367, 100, 90, 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment