-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
74 lines (65 loc) · 1.85 KB
/
background.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let maxLevel = 5;
let branches = 2;
let sides = 5;
let gapBetweenTwoBranches = Math.random() * 150 + 150;
let lengthOfTheBranches = Math.random() * 150 + 150;
let spread = Math.random();
let angle = Math.PI * 2 * spread;
ctx.translate(canvas.width / 2, canvas.height / 2);
function drawLine(level) {
if (level > maxLevel) {
return;
}
ctx.strokeStyle = "white";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(lengthOfTheBranches, 0);
ctx.stroke();
for (let i = 1; i < branches + 1; i++) {
ctx.save();
ctx.translate(gapBetweenTwoBranches * i, 0);
ctx.scale(0.5, 0.5);
ctx.save();
ctx.rotate(angle);
drawLine(level + 1);
ctx.restore();
ctx.save();
ctx.rotate(-angle);
drawLine(level + 1);
ctx.restore();
ctx.restore();
}
}
for (let i = 0; i < sides; i++) {
drawLine(0);
ctx.rotate((Math.PI * 2) / sides);
}
window.addEventListener("click", () => {
maxLevel = 5;
branches = 2;
sides = 5;
gapBetweenTwoBranches = Math.random() * 150 + 150;
lengthOfTheBranches = Math.random() * 150 + 150;
spread = Math.random();
angle = Math.PI * 2 * spread;
ctx.clearRect(
-canvas.width / 2,
-canvas.height / 2,
canvas.width,
canvas.height
);
for (let i = 0; i < sides; i++) {
drawLine(0);
ctx.rotate((Math.PI * 2) / sides);
}
});
window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.translate(canvas.width / 2, canvas.height / 2);
});