-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
55 lines (47 loc) · 1.99 KB
/
Copy pathtree.js
File metadata and controls
55 lines (47 loc) · 1.99 KB
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
var canv = document.getElementById("canvas");
canv.width = document.documentElement.clientWidth;
canv.height = document.documentElement.clientHeight;
ctx = canv.getContext('2d');
function randBetween(min, max) {
calc = (Math.random() * (max - min)) + min;
return calc;
}
function render(startX, startY, len, angle, width) {
ctx.beginPath();
ctx.save();
ctx.translate(startX, startY);
ctx.rotate(angle * Math.PI/180);
ctx.moveTo(0, 0);
ctx.lineTo(0, -len);
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.stroke();
if(len < 5) {
ctx.restore();
return;
}
if (!randomLenghtCheck) {
render(0, -len, len * branchScaleRatio, this.randBetween(minAngle, maxAngle), width * 0.8);
render(0, -len, len * branchScaleRatio, -this.randBetween(minAngle, maxAngle), width * 0.8);
} else {
render(0, -len, len * (branchScaleRatio + (Math.random() / 5 - 0.10)), this.randBetween(minAngle, maxAngle), width * 0.8);
render(0, -len, len * (branchScaleRatio + (Math.random() / 5 - 0.10)), -this.randBetween(minAngle, maxAngle), width * 0.8);
}
ctx.restore();
}
function renderNew(startX, startY, len, angle, width) {
ctx.clearRect(0, 0, canv.width, canv.height);
render(startX, startY, len, angle, width);
}
function callRender() {
treeSize = Number(document.getElementById("treeSize").value);
minAngle = Number(document.getElementById("minAngle").value);
maxAngle = Number(document.getElementById("maxAngle").value);
branchScaleRatio = Number(document.getElementById("branchScaleRatio").value);
randomLenghtCheck = document.getElementById("randomLenght").checked;
document.getElementById("labelMin").innerText = minAngle+" Minimum angle";
document.getElementById("labelMax").innerText = maxAngle+" Maximum angle";
document.getElementById("treeSizeLabel").innerText = treeSize+" Tree size";
document.getElementById("branchScaleRatioLabel").innerText = branchScaleRatio+"±0.1 Branch scale ratio[>0.8 !!!]";
renderNew(canv.width/2, canv.height-50, treeSize, 0, 15);
}