Skip to content

Instantly share code, notes, and snippets.

@surmistry
Forked from pjama/fractals.html
Created March 21, 2017 14:28
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 surmistry/075875310b9eade0fd9a172451f385a2 to your computer and use it in GitHub Desktop.
Save surmistry/075875310b9eade0fd9a172451f385a2 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1200" height="1000"></canvas>
<script>
function Point(x, y) {
this.X = x;
this.Y = y;
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var alpha = Math.PI * 0.275,
beta = -Math.PI * 0.35,
ratioA = 0.775,
ratioB = 0.45,
MIN_LENGTH = 5;
function drawLine(rootPoint, r, theta) {
context.beginPath();
var endX = rootPoint.X + r*Math.cos(theta);
var endY = rootPoint.Y + r*Math.sin(theta);
var endPoint = new Point(endX, endY);
context.moveTo(rootPoint.X, rootPoint.Y);
context.lineTo(endPoint.X, endPoint.Y);
context.stroke();
if (r*ratioA < MIN_LENGTH || r*ratioB < MIN_LENGTH) {
return;
}
drawLine(endPoint, r*ratioA, theta+alpha);
drawLine(endPoint, r*ratioB, theta+beta);
}
var startPoint = new Point(300, 500);
drawLine(startPoint, 200, Math.PI * 3/2);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment