-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
224 lines (198 loc) · 7.09 KB
/
index.html
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<!--
Radial Family Trees on HTML5 Canvas
Copyright (C) 2013, 2017 tobyp
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Family Tree</title>
<script src="jquery.mousewheel.min.js"></script>
<script src="jquery.min.js"></script>
<style type="text/css">
* {
border: 0;
padding: 0;
margin: 0;
}
#family_tree {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
span#copy {
display: block;
background: white;
position: absolute;
padding: 0.2em;
bottom: 0.2em;
right: 0.2em;
}
</style>
<script type="text/javascript" src="family_tree.js"></script>
<script type="text/javascript">
var ctx;
var SIZE = [1200, 1200];
var MIN_RADIAL_DISTANCE = 150;
var FONT = '12px monospace';
var CIRCLE_STYLE = "#bbb";
var LINE_STYLE = "#999";
var TEXT_STYLE = "#000";
var TAG_STYLE = "#fff";
var ANGLE = 0;
var DISTANCE = 1;
var X = 0;
var Y = 1;
function max(list) { //used for depth
var m = list[0];
for (var i in list) {
if (list[i] > m) m = list[i];
}
return m;
}
function translate(reference, location) { //translate relative polar coords to absolute x/y
var rad = (location[ANGLE] * 2 * Math.PI) / 360.0;
return [reference[X] + location[DISTANCE] * Math.sin(rad), reference[Y] + location[DISTANCE] * Math.cos(rad)];
}
function line(reference, location0, location1) { //draw a line using rel. polar coords
ctx.strokeStyle = LINE_STYLE;
location0 = translate(reference, location0);
location1 = translate(reference, location1);
ctx.beginPath()
ctx.moveTo(location0[X], location0[Y]);
ctx.lineTo(location1[X], location1[Y]);
ctx.closePath()
ctx.stroke();
}
function text(reference, location, string) { //draw centered text using rel. polar coords
var l = ctx.measureText(string).width / 2;
location = translate(reference, location)
ctx.fillStyle = TAG_STYLE;
ctx.fillRect(location[X] - l - 2, location[Y] - 13, 2 * l + 4, 24);
ctx.fillStyle = TEXT_STYLE;
ctx.textBaseline = 'middle';
ctx.font = FONT;
ctx.fillText(string, location[X] - l, location[Y]);
}
function circle(reference, center, radius) { //draw a circle using rel. polar coords
center = translate(reference, center);
ctx.strokeStyle = CIRCLE_STYLE;
ctx.beginPath();
ctx.arc(center[X], center[Y], radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.stroke();
}
function display_line(reference, location, parent_location) { //draw line from child to parent
if (parent_location != null)
line(reference, location, parent_location);
}
function display_text(reference, location, node) { //draw text of a person - name and (optinally) spouse
var str = node['name']
if (typeof(node['spouse']) !== "undefined") {
str += " ∞ " + node['spouse'];
}
text(reference, location, str);
}
function count_segments(branch, depth) { //count how wide and deep the tree will be. width is max(1, sum of childrens' width), depth is max(1, max(all of childrens' depth))
branch['width'] = 0;
branch['depth'] = depth;
for (var c in branch['children']) {
count_segments(branch['children'][c], depth+1);
branch['width'] += branch['children'][c]['width'];
branch['depth'] = max([branch['depth'], branch['children'][c]['depth']])
}
branch['width'] = Math.max(1, branch['width']);
}
function draw_segment(reference, data, angle, distance, parent_loc, delta) { //draw a node and its children recursively
var display_loc = [angle + (delta[ANGLE] * data['width']) / 2.5, distance]; //my display: in the angle-middle of all my children.
display_line(reference, display_loc, parent_loc);
for (var c_ in data['children']) {
var c = data['children'][c_];
draw_segment(reference, c, angle, distance+delta[DISTANCE], display_loc, delta)
angle += c['width']*delta[ANGLE];
}
display_text(reference, display_loc, data);
}
function draw(origin, family_tree) { //draw a family tree relative to origin
var delta = [360.0 / family_tree['width'], max([MIN_RADIAL_DISTANCE, family_tree['width'] * 2.0])];
var i;
for (i=1; i<=family_tree['depth']; i++) {
circle(origin, [0.0, 0.0], i*delta[DISTANCE]);
}
draw_segment(origin, family_tree, 0.0, 0.0, null, delta);
}
var dx = 0, dy = 0, scale = 1;
var vx = 0, vy = 0;
var last_frame = null;
var cnv, cnv, cnv;
function render(timestamp) {
var delta = 0;
if (last_frame != null) { delta = timestamp - last_frame; }
last_frame = timestamp;
ctx.clearRect(0, 0, ctx.width, ctx.height);
ctx.save();
ctx.scale(scale, scale);
ctx.translate(dx, dy);
draw([SIZE[0] / 2, SIZE[1] / 2], family_tree);
ctx.restore();
dx += vx * delta;
dy += vy * delta;
window.requestAnimationFrame(render);
}
$(document).ready(function() {
var cnv = $("#family_tree")[0];
ctx = cnv.getContext('2d');
$(window).resize(function() {
cnv.width = window.innerWidth;
cnv.height = window.innerHeight;
ctx.width = window.innerWidth;
ctx.height = window.innerHeight;
}).resize();
count_segments(family_tree, 0);
var downs = {};
$(window).mousewheel(function(e){
scale *= Math.pow(1.1, e.deltaY);
console.log("scale ", scale);
}).keydown(function(e) {
//"LEFT": 37, "UP": 38, "RIGHT": 39, "DOWN": 40
if (downs[e.which]) return;
if (e.which == 37) vx += 1;
else if (e.which == 38) vy += 1;
else if (e.which == 39) vx -= 1;
else if (e.which == 40) vy -= 1;
downs[e.which] = true;
console.log({'vx':vx, 'vy':vy, 'dx':dx, 'dy':dy});
}).keyup(function(e) {
if (e.which == 37) vx -= 1;
else if (e.which == 38) vy -= 1;
else if (e.which == 39) vx += 1;
else if (e.which == 40) vy += 1;
downs[e.which] = false;
console.log({'vx':vx, 'vy':vy, 'dx':dx, 'dy':dy});
});
window.requestAnimationFrame(render);
});
</script>
</head>
<body>
<canvas id="family_tree">
Your web-browser doesn't support HTML5 Canvas drawing. You should get a recent web browser, like Google Chrome or Firefox.
</canvas>
<noscript>
Additionally, your browser doesn't support JavaScript. You should turn it on in the settings, or get a browser like Google Chrome or Firefox that support it.
</noscript>
<span id="copy">© 2013 tobyp | <a href="https://github.com/tobyp/family-tree">github</a></span>
</body>
</html>