-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal.js
56 lines (45 loc) · 1.3 KB
/
fractal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
let slider = document.getElementById('slider');
let canvas = document.getElementById('canvas');
let angle = Number(slider.value);
let ctx = canvas.getContext('2d');
function setup() {
slider.addEventListener('input', (event) => {
angle = Number(slider.value);
render();
});
render();
}
function render() {
let width = canvas.width;
let height = canvas.height;
ctx.moveTo(width, height);
ctx.clearRect(0, 0, width, height);
ctx.beginPath();
ctx.fillStyle = 'grey';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = 'white';
let length = 150
let fromX = width / 2;
let fromY = height;
let toX = width / 2;
let toY = height - length;
drawLine(fromX, fromY, toX, toY);
branch(toX, toY, ctx, length, -angle);
branch(toX, toY, ctx, length, angle + 180);
ctx.stroke();
}
function branch(fromX, fromY, ctx, length, newAngle) {
let toX = length * Math.cos(newAngle * Math.PI / 180) + fromX;
let toY = length * Math.sin(newAngle * Math.PI / 180) + fromY;
if(length > 4) {
drawLine(fromX, fromY, toX, toY);
let newLength = length * 0.67;
branch(toX, toY, ctx, newLength, newAngle + angle);
branch(toX, toY, ctx, newLength, newAngle - angle);
}
}
function drawLine(fromX, fromY, toX, toY) {
ctx.moveTo(fromX, fromY);
ctx.lineTo(toX, toY);
}
setup();