/
scale.html
474 lines (415 loc) · 17.9 KB
/
scale.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!DOCTYPE html>
<html>
<head>
<title>Progress Tree</title>
<meta charset="utf-8">
<link href="bootstrap-3.1.1-dist/css/bootstrap.css" rel="stylesheet">
</head>
<style type="text/css">
#content {
padding-left: 50px;
padding-top: 100px;
}
</style>
<body>
<div class="row">
<div class="col-md-4" id="content">
<h1>Make a change</h1>
<p>Save the Children has been working to fight hunger, prevent malnutrition and improve the lives of boys and girls in Africa since 1963. Whether we are working with orphaned children in sub-Saharan Africa or emergency relief for refugees in North Africa, Save the Children strives to meet the needs to vulnerable children and their families with lifesaving and life-changing programs. Charities can make a difference, with a hand-up, not a handout.</p>
<button class="btn btn-primary" id='donate' type="submit">Donate</button>
<button class="btn btn-success" id='animate' type="submit">Animate</button>
<button class="btn btn-danger" id='atonce' type="submit">At Once</button>
</div>
<div class="col-md-8" id ="svgArea">
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="186.32976"
height="170.42111"
id="svg1901"
sodipodi:version="0.32"
inkscape:version="0.46+pre4"
sodipodi:docname="leaf.svg"
sodipodi:docbase="C:\Important\svg"
inkscape:output_extension="org.inkscape.output.svg.inkscape"
version="1.0">
<defs>
<g
inkscape:label="Taso 1"
inkscape:groupmode="layer"
id="layer1"
transform="scale(0.1) rotate(132 355 210)">
<path
style="fill-rule:evenodd;stroke-width:3.00000024;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 422.27292,632.34394 C 432.85918,616.61295 448.34076,612.0402 448.34076,612.0402 C 448.34076,612.0402 442.54386,606.55398 439.22989,607.84521 C 426.2285,617.56064 419.44945,629.60334 419.44945,629.60334 C 266.27427,623.18356 285.91787,686.20064 265.011,767.7044 C 381.6304,796.72098 433.42735,737.61667 422.27292,632.34394 z"
id="path2159"
sodipodi:nodetypes="cccccc" />
<!-- <path
style="fill:#abc837;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1"
d="M 302.27759,741.21062 C 312.59094,737.71342 319.22122,728.61389 328.93943,724.10035 C 335.14269,714.75636 349.43533,712.00907 346.17152,729.80846 C 341.8614,737.76032 352.91408,760.81454 355.72913,743.55812 C 358.08954,731.20136 348.85485,717.1466 351.93186,705.46388 C 358.49071,696.04816 365.40665,685.64598 375.07218,679.76244 C 382.80036,689.48542 383.13328,707.52311 382.74251,719.46905 C 396.22556,732.1495 392.54206,703.058 389.47711,694.77056 C 382.451,679.12849 383.55459,664.84496 397.6377,660.1622 C 404.73986,658.06346 419.53919,640.77051 403.1361,642.87667 C 392.38688,648.00479 386.31989,661.9091 373.03691,661.89063 C 361.92009,659.56636 338.81043,639.82534 336.66956,655.76491 C 342.93053,664.24787 369.9026,660.24127 365.11729,674.35945 C 353.65845,678.84665 352.6711,701.25674 336.37406,695.49154 C 326.77131,692.78319 307.80361,677.82768 305.15926,691.25865 C 310.75635,700.18676 334.56641,695.04273 333.28119,708.74618 C 321.90598,718.12074 305.73983,723.82061 298.22278,736.6954 C 298.65539,738.6772 300.291,740.78 302.27759,741.21062 z"
id="path3147" /> -->
</g>
<!-- <g
inkscape:label="Taso 1"
inkscape:groupmode="layer"
id="layer1"
style="opacity:1"
transform="scale(0.1) rotate(132 355 210)">
<path
style="fill:#89a02c;fill-rule:evenodd;stroke:#4c5a17;stroke-width:3.00000024;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 422.27292,632.34394 C 432.85918,616.61295 448.34076,612.0402 448.34076,612.0402 C 448.34076,612.0402 442.54386,606.55398 439.22989,607.84521 C 426.2285,617.56064 419.44945,629.60334 419.44945,629.60334 C 266.27427,623.18356 285.91787,686.20064 265.011,767.7044 C 381.6304,796.72098 433.42735,737.61667 422.27292,632.34394 z"
id="path2159"
sodipodi:nodetypes="cccccc" />
<path
style="fill:#abc837;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1"
d="M 302.27759,741.21062 C 312.59094,737.71342 319.22122,728.61389 328.93943,724.10035 C 335.14269,714.75636 349.43533,712.00907 346.17152,729.80846 C 341.8614,737.76032 352.91408,760.81454 355.72913,743.55812 C 358.08954,731.20136 348.85485,717.1466 351.93186,705.46388 C 358.49071,696.04816 365.40665,685.64598 375.07218,679.76244 C 382.80036,689.48542 383.13328,707.52311 382.74251,719.46905 C 396.22556,732.1495 392.54206,703.058 389.47711,694.77056 C 382.451,679.12849 383.55459,664.84496 397.6377,660.1622 C 404.73986,658.06346 419.53919,640.77051 403.1361,642.87667 C 392.38688,648.00479 386.31989,661.9091 373.03691,661.89063 C 361.92009,659.56636 338.81043,639.82534 336.66956,655.76491 C 342.93053,664.24787 369.9026,660.24127 365.11729,674.35945 C 353.65845,678.84665 352.6711,701.25674 336.37406,695.49154 C 326.77131,692.78319 307.80361,677.82768 305.15926,691.25865 C 310.75635,700.18676 334.56641,695.04273 333.28119,708.74618 C 321.90598,718.12074 305.73983,723.82061 298.22278,736.6954 C 298.65539,738.6772 300.291,740.78 302.27759,741.21062 z"
id="path3147" />
</g> -->
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.7"
inkscape:cx="205.81395"
inkscape:cy="-68.376196"
inkscape:document-units="px"
inkscape:current-layer="layer1"
gridtolerance="10000"
inkscape:window-width="1280"
inkscape:window-height="938"
inkscape:window-x="-4"
inkscape:window-y="-4"
showguides="true"
inkscape:guide-bbox="true"
showgrid="false" />
<metadata
id="metadata1906">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
</svg>
</div>
</div>
<script src="D3/d3.js"></script>
<script src="bootstrap-3.1.1-dist/js/jquery-1.11.1.js"></script>
<script src="bootstrap-3.1.1-dist/js/bootstrap.min.js"></script>
<script>
var branches = [];
var leaves = [];
var seedWidth = 75;
var maxDepth = 9;
function degToRad(degree) {
return degree*Math.PI/180;
}
function radToDeg(radian) {
return radian*180/Math.PI
}
// transform angle with y axis as a basis. so 0 degree means vertical
function normalizeAngle(rad) {
return degToRad(90)+rad;
}
// transform angle with x axis as basis like normal polar coordinate
function denormalizeAngle(rad) {
return rad - degToRad(90);
}
// Tree creation functions
function branch(b) {
var end = endPt(b), daR, newB, da, dl, aRand, node, id;
branches.push(b);
//start from depth n+1 the length is 0.8*length
if (b.depth >= 0 && b.depth < 1) {
dl = 0.5;
} else {
dl = 0.75;
}
//dl = 0.75;
//start from depth n+1 the angle is 20
// if (b.depth >= 3) {
// da = degToRad(25);
// aRand = degToRad(10);
// } else if (b.depth >= 5) {
// da = degToRad(30);
// aRand = degToRad(10);
// } else {
// da = degToRad(20);
// aRand = degToRad(5);
// }
lRand = 0.15;
lRand = Math.random() * (lRand);
if (b.depth < 3) {
da = degToRad(30);
aRand = degToRad(10);
} else if (b.depth < 6) {
da = degToRad(20);
aRand = degToRad(10);
} else {
da = degToRad(20);
aRand = degToRad(5);
}
if (b.depth === maxDepth) {
b.visible = false;
leaves.push(b);
return;
}
// create node array, contains id of branch that has been traversed by the branch
node = [];
node.push(b.i);
node = node.concat(b.node);
// console.log(b.i);
// console.log(b.depth);
// console.log(b.dl);
// console.log('-----')
// Left branch
//daR = ar * Math.random() - ar * 0.5;
daR = Math.random() * (aRand + aRand) - aRand; //random angle between -aRand and aRand
newB = {
i: branches.length,
x: end.x,
y: end.y,
angle: b.angle + da + daR,
length: b.length * (dl + lRand),
depth: b.depth + 1,
parent: b.i,
node: node,
dl: dl,
da: radToDeg(da),
aRand: aRand
};
branch(newB);
// Right branch
//daR = ar * Math.random() - ar * 0.5;
daR = Math.random() * (aRand + aRand) - aRand; //random angle between -aRand and aRand
newB = {
i: branches.length,
x: end.x,
y: end.y,
angle: b.angle - da + daR,
length: b.length * (dl + lRand),
depth: b.depth + 1,
parent: b.i,
node: node,
dl: dl,
da: radToDeg(da),
aRand: aRand
};
branch(newB);
}
function endPt(b) {
// Return endpoint of branch
var x = b.x + b.length * Math.cos( normalizeAngle(b.angle) );
var y = b.y - b.length * Math.sin( normalizeAngle(b.angle) );
return {x: x, y: y};
}
// define svg for placeholder
// var margin = {top: 20, right: 10, bottom: 20, left: 30};
// var width = 800 - margin.left - margin.right;
// var height = 800 - margin.top - margin.bottom;
//var width = document.getElementById("svgArea").offsetWidth;
var width = 800;
var height = 800;
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height)
.attr('id', 'tree')
.attr('viewBox', '50 50 ' + width + ' ' + height);
// Tree configuration
var seed = {i: 0, x: 10, y: 20, angle: 0, length: 1, depth: 0, node: [], dl:0.5}; // a = angle, l = length, d = depth
//svg.style("background-color", "aliceblue");
function x1(d) {return xScale(d.x);}
function y1(d) {return yScale(d.y);}
function x2(d) {return xScale(endPt(d).x);}
function y2(d) {return yScale(endPt(d).y);}
function highlightParents(d) {
var colour = d3.event.type === 'mouseover' ? 'green' : '#470000';
var depth = d.depth;
for(var i = 0; i <= depth; i++) {
d3.select('#id-'+parseInt(d.i)).style('stroke', colour);
d = branches[d.parent];
}
}
function highlightParents2(d) {
console.log(d);
var colour = d3.event.type === 'mouseover' ? 'green' : '#470000';
var depth = d.depth;
for(var i = 0; i <= depth; i++) {
d3.select('#id-'+parseInt(d.i)).style('stroke', colour);
d = branches[d.parent];
}
}
function highlightLeaf(d,i) {
svg.selectAll('use')
.style('fill', 'grey');
svg.select('use#id-'+i)
.style('fill', 'green');
}
branch(seed);
var xMax = Math.max.apply(null, branches.map(function(element){return element.x}));
var xMin = Math.min.apply(null, branches.map(function(element){return element.x}));
var yMax = Math.max.apply(null, branches.map(function(element){return element.y}));
var yMin = Math.min.apply(null, branches.map(function(element){return element.y}));
// an ugly hack to leave room for leave to grow inside the svg container
// so that the leaf is not cut out of the svg
var leafWidth = 0.8;
var xScale = d3.scale.linear()
.domain([xMin-leafWidth, xMax+leafWidth])
.range([0, width]);
var yScale = d3.scale.linear()
.domain([yMin-leafWidth, yMax+leafWidth])
.range([0, height]);
svg.selectAll('line')
.data(branches)
.enter()
.append('line')
.attr('x1', x1)
.attr('y1', y1)
.attr('x2', x2)
.attr('y2', y2)
//.attr('stroke-width', 5)
.attr('stroke', '#470000')
// .style('stroke-width', function(d) {if (d.depth == 0) { return parseInt(seedWidth) + 'px'}
// else if (d.depth == 1) { return 55 + 'px'; }
// else if (d.depth == 2) { return 37 + 'px'; }
// else if (d.depth == 3) { return 21 + 'px'; }
// else if (d.depth == 4) { return 13 + 'px'; }
// else if (d.depth == 5) { return 9 + 'px'}
// else if (d.depth == 6) { return 6 + 'px'; }
// else if (d.depth == 7 ) { return 4 + 'px'; }
// else if (d.depth == 8 ) { return 3 + 'px'; }
// else if (d.depth == 9 ) { return 2 + 'px'; }
// else { return 1 + 'px'; }})
.style('stroke-width', function(d){
var x2 = xScale(endPt(d).x);
var y2 = yScale(endPt(d).y);
var x1 = xScale(d.x);
var y1 = yScale(d.y);
var length = Math.pow(Math.pow((x2-x1),2)+Math.pow((y2-y1),2),0.5);
var width = treeWidth(length,d.depth);
return width < 1 ? parseInt(1) + 'px' : parseInt(width) + 'px';
//return parseInt(treeWidth(length)) + 'px';
})
//.style('stroke-width', function(d) {return parseInt(seedWidth + 1 - d.depth) + 'px';})
.style('stroke-linecap', function(d){return d.depth === -1 ? 'butt' : 'round'})
.attr('id', function(d) {return 'id-'+d.i;})
.on('mouseover', highlightParents2)
.on('mouseout', highlightParents);
leafScale(1);
svg.selectAll('use')
.data(leaves)
.enter()
.append('use')
.attr('x', x2)
.attr('y', y2)
.attr('id', function(d,i){return 'id-' + i})
.attr('transform', function(d){ return 'rotate(' + radToDeg(-d.angle) + ' ' + xScale(endPt(d).x) + ' ' + yScale(endPt(d).y) + ')';})
.attr("xlink:href", "#layer1")
.attr('style', 'fill:green;stroke:black')
.attr('visibility', function(d){
return d.visible ? 'visible' : 'hidden'
})
.on('mouseover', highlightLeaf)
.on('mouseout', function(d){svg.selectAll('use').style('fill', 'green')});
// another hack to hide rounded line of first branch (the trunk)
var trunk = branches[0];
svg.append('rect')
.attr('height', getLength(trunk)*0.3)
.attr('width', width)
.attr('x', 0)
.attr('y', yScale(trunk.y))
.attr('fill', 'white');
// svg.append('rect')
// .attr('width', 75)
// .attr('height', 20)
// .attr('x', 400-75/2)
// .attr('y', 800-20)
// .attr('fill', 'white');
// svg.selectAll('ellipse')
// .data(leaves)
// .enter()
// .append('ellipse')
// .attr('cx', x2)
// .attr('cy', y2)
// .attr('rx', 4)
// .attr('ry', 10)
// .attr('id', function(d,i){return 'id-' + i})
// .attr('visibility', function(d){
// return d.visible ? 'visible' : 'hidden'
// })
// .attr('transform', function(d){
// // angle in making branch and rotate is different. 20 degree angle in branch eq to -20 degree angle in rotate
// return 'rotate(' + radToDeg(-d.angle) + ' ' + endPt(d).x + ' ' + endPt(d).y + ')';
// //return 'rotate(' + radToDeg(denormalizeAngle(d.angle)) + ' ' + endPt(d).x + ' ' + endPt(d).y + ')';
// })
// //.attr('transform', function(d){return "rotate(" + radToDeg(denormalizeAngle(d.angle)) + ")"})
// .attr('stroke', 'black')
// .attr('fill', "green")
// .on('mouseover', highlightLeaf)
// .on('mouseout', function(d){svg.selectAll('ellipse').style('fill', 'green')});
d3.select('#donate').on('click', update);
d3.select('#animate').on('click', animate);
d3.select('#atonce').on('click', atonce);
var numOfClick = 0;
function update() {
console.log('btn clicked')
numOfClick++;
leaves[numOfClick].visible = true;
svg
.selectAll('use')
.data(leaves)
.transition()
.attr('visibility', function(d){return d.visible ? 'visible' : 'hidden'});
}
function animate() {
for (i = 0; i < leaves.length; i++) {
setTimeout(function(i) {
leaves[i].visible = true;
svg
.selectAll('use#id-'+i)
.data(leaves)
.transition()
.attr('visibility', function(d){return d.visible ? 'visible' : 'hidden'});
}, 100, i);
}
}
function atonce() {
leaves.forEach(function(d){
d.visible = true;
})
svg
.selectAll('use')
.data(leaves)
.transition()
.attr('visibility', function(d){return d.visible ? 'visible' : 'hidden'});
}
function leafScale(scale) {
d3.select('g#layer1')
.attr('transform', 'scale(' + scale*0.1 + ') rotate(132 355 210)');
}
function treeWidth(length,depth) {
return depth === 0 ? (0.4059*length-8.1419)*0.73 : 0.4059*length-8.1419;
//return 0.4059*length-8.1419;
}
function getLength(branch) {
var x2 = xScale(endPt(branch).x);
var y2 = yScale(endPt(branch).y);
var x1 = xScale(branch.x);
var y1 = yScale(branch.y);
return Math.pow(Math.pow((x2-x1),2)+Math.pow((y2-y1),2),0.5);
}
</script>
</body>
</html>