-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.html
More file actions
150 lines (138 loc) · 5.45 KB
/
tree.html
File metadata and controls
150 lines (138 loc) · 5.45 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<title>GenTree</title>
</head>
<!--Thanks to Marlen Julie, http://stackoverflow.com/users/4321140/marlen-julie for original canvas tree code-->
<body onload="init();">
<div id="slider" style='float:left; padding: 0px 10px;'>
<h4>Depth</h4>
<form>
<label>1</label>
<input type="range" id="depth" value="5" min="1" max="10" step="1">
<label>10</label>
</form>
<br>
</div>
<div id="slider" style='float:left; padding: 0px 10px;'>
<h4>Scaling</h4>
<form>
<label>0</label>
<input type="range" id="scale" value="0.75" min="0" max="1" step="0.01">
<label>1</label>
</form>
<br>
</div >
<div id="slider" style='float:left; padding: 0px 10px;'>
<h4>Angle</h4>
<form>
<label>0</label>
<input type="range" id="angle" value="0.3" min="0" max="3.14" step="0.01">
<label>360</label>
</form>
<br>
</div>
<div id="slider" style='float:left; padding: 0px 10px;'>
<h4>Left Offset</h4>
<form>
<label>0</label>
<input type="range" id="lSet" value="1" min="0" max="1" step="0.01">
<label>1</label>
</form>
<br>
</div>
<div id="slider" style='float:left; padding: 0px 10px;'>
<h4>Right Offset</h4>
<form>
<label>0</label>
<input type="range" id="rSet" value="1" min="0" max="1" step="0.01">
<label>1</label>
</form>
<br>
</div>
<div id="details" style='float:left; padding: 0px 10px; width:300px;'>
<p>A large, complex structure can arise from a few simple rules followed locally. In life, cells all follow the same set of rules called DNA. </p>
<a href="https://github.com/RemiTorracinta/GenTree/">Source Code!</a>
</div>
<div id="main" class="main">
<canvas id="canvas" width="1200" height="600" ></canvas>
</div>
<script type="text/javascript" >
init = function() {
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth - 50; //document.width is obsolete
canvas.height = window.innerHeight - 150; //document.height is obsolete
canvasW = canvas.width;
canvasH = canvas.height;
var x1 = canvasW/2;
var y1 = canvasH-120;
var x2 = canvasW/2;
var y2 = canvasH-220;
var angle = document.getElementById('angle').value; //Branching angle
var depth = document.getElementById('depth').value; //Number of branches
var scale = document.getElementById('scale').value; //Size of next branch relative to current
var lSet = document.getElementById('lSet').value; //Starting point of left branch along current
var rSet = document.getElementById('rSet').value; //Starting point of right branch along current
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet);
document.getElementById('depth').oninput = function() {
depth = document.getElementById('depth').value;
clearTree();
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet );
};
document.getElementById('angle').oninput = function() {
angle = document.getElementById('angle').value;
clearTree();
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet );
};
document.getElementById('scale').oninput = function() {
scale = document.getElementById('scale').value;
clearTree();
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet );
};
document.getElementById('lSet').oninput = function() {
lSet = document.getElementById('lSet').value;
clearTree();
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet );
};
document.getElementById('rSet').oninput = function() {
rSet = document.getElementById('rSet').value;
clearTree();
drawTree( x1, y1, x2, y2, angle, depth, scale, lSet, rSet );
};
}
clearTree = function () {
var context = document.getElementById('canvas').getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
}
drawTree = function( x1, y1, x2, y2, angle, depth, scale, lSet, rSet){
var context = document.getElementById('canvas').getContext('2d');
context.strokeStyle = 'rgb( 0, 0, 0 )';
context.lineWidth = depth;
context.beginPath();
context.moveTo( x1, y1 );
context.lineTo( x2, y2 );
context.stroke();
if( depth > 0 ){
var x = x2 - x1;
var y = y2 - y1;
x *= scale;
y *= scale;
var lxOffset = x*(1-lSet);
var lyOffset =y*(1-lSet);
var rxOffset = x*(1-rSet);
var ryOffset =y*(1-rSet);
var xLeft = x * Math.cos( -angle ) - y * Math.sin( -angle );
var yLeft = x * Math.sin( -angle ) + y * Math.cos( -angle );
var xRight = x * Math.cos( +angle ) - y * Math.sin( +angle );
var yRight = x * Math.sin( +angle ) + y * Math.cos( +angle );
xLeft += x2-lxOffset;
yLeft += y2-lyOffset;
xRight += x2-rxOffset;
yRight += y2-ryOffset;
drawTree( x2-lxOffset, y2-lyOffset, xLeft, yLeft, angle, depth - 1, scale , lSet, rSet);
drawTree( x2-rxOffset, y2-ryOffset, xRight, yRight, angle, depth - 1, scale , lSet, rSet);
}
}
</script>
</body>
</html>