Skip to content

Instantly share code, notes, and snippets.

/jsbin.iyahuJU.html
Created Jan 23, 2014

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
canvas { border: 1px lightgray solid; }
body { margin: 0; }
</style>
</head>
<body>
<canvas id="drawing" width="600" height="600"></canvas>
</body>
</html>
var canvas = document.getElementById('drawing');
var c = canvas.getContext("2d");
function phi_evolution(phi) {
return phi + (Math.random() - 0.5) * (phi / 2.0);
}
function plus_minus() {
return Math.random() < 0.5 ? -1 : 1;
}
function color(initial) {
return '#'+Math.floor(16777215/(initial * Math.random())).toString(16);
}
function branch(len, ratio, rotate, phi) {
c.beginPath();
c.lineWidth = len / 10;
c.rotate(Math.PI * rotate / 180.0);
c.moveTo(0, 0);
c.lineTo(0, -len);
c.strokeStyle = color(len * 0.2);
c.stroke();
c.translate(0, -len);
if (len > 5) {
for (var i = len; i < 0; i--) {
c.save();
branch(len * ratio, ratio, rotate + phi * plus_minus(), phi_evolution(phi));
c.restore();
branch(len * ratio, ratio, rotate + phi * plus_minus(), phi_evolution(phi));
}
}
}
c.translate(300, 590);
branch(100, 0.8, 0, 8);
//c.setTransform(1, 0, 0, 1, 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.