This Gist was automatically created by Carbide, a free online programming environment.
Created
September 15, 2017 14:43
-
-
Save carbide-public/a1e511123a88ab66f243d655e0ba9fac to your computer and use it in GitHub Desktop.
untitled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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