-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (130 loc) · 4.76 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>AXONOMETRIC Paths</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
<script src="build/d3-axonometric.js"></script>
<style>
.spring{
fill:none;
stroke:black;
stroke-width: 1px;
}
.axis, #arrow{
fill: lightgrey;
stroke: lightgrey;
stroke-width: 1px;
}
.left-face{
fill:#999;
stroke:#000;
stroke-linejoin:round;
}
.right-face{
fill:#bbb;
stroke:#000;
stroke-linejoin:round;
}
.top-face{
fill:#FFF;
stroke:#000;
stroke-linejoin:round;
}
</style>
</head>
<body>
<h1>Axonometric projection: paths and lines</h1>
<svg class="slab" width="600" height="600"></svg>
<svg class="helix" width="600" height="600"></svg>
</body>
<script>
var projection = d3.axonometricProjection();
var pathGenerator = d3.axonometricPath()
.projection(projection);
var lineGenerator = d3.axonometricLine()
.projection(projection);
var getBounds = d3.axonometricBounds()
.projection(projection);
function makeSlab(slabWidth, slabDepth, slabHeight){
var topFace = [
[-slabWidth, slabDepth, -slabHeight],
[0, slabDepth, -slabHeight],
[0, slabDepth, 0],
[-slabWidth, slabDepth, 0] ];
var leftFace = [
[-slabWidth, slabDepth, 0],
[0, slabDepth, 0],
[0, 0, 0],
[-slabWidth, 0, 0] ];
var rightFace = [
[0, slabDepth, 0],
[0, slabDepth, -slabHeight],
[0, 0, -slabHeight],
[0, 0, 0] ];
return [
{'class':'left-face',shape:leftFace},
{'class':'right-face',shape:rightFace},
{'class':'top-face', shape:topFace}
];
}
(function draw(){
var data = makeSlab(Math.random()*200,Math.random()*200,Math.random()*200);
var points = data.reduce(function(points,current){
return points.concat(current.shape);
},[]);
var bounds = getBounds(points);
d3.select('svg.slab').attr('viewBox', [bounds.x, bounds.y, bounds.width, bounds.height])
d3.select('svg.slab')
.append('g')
.attr('class', 'origin')
.selectAll('path')
.data(data)
.enter()
.append('path')
.attr('d',function(d){
return pathGenerator(d.shape) + 'z';
})
.attr('class',function(d){ return d.class; });
})();
function change(){
var data = makeSlab(Math.random()*200,Math.random()*200,Math.random()*200);
var points = data.reduce(function(points,current){
return points.concat(current.shape);
},[]);
var bounds = getBounds(points);
d3.select('svg.slab').transition().attr('viewBox', [bounds.x, bounds.y, bounds.width, bounds.height]);
d3.selectAll('svg.slab path')
.data(data)
.transition()
.attr('d',function(d){
return pathGenerator(d.shape) + 'z';
})
.attr('class',function(d){ return d.class; });
}
var radius = 100;
var separationConstant = 10;
var data = [];
for(var t = 0; t<40; t+=0.2){ //generate the points in 3d space for the helix
data.push([ radius*Math.cos(t), separationConstant*t, radius*Math.sin(t), ]);
}
//.projection(projection);
var origin = d3.select('svg.helix')
.append('g')
.attr('transform','translate(300,500)');
origin.append('line')
.attr('class','axis')
.attrs(lineGenerator([-200,0,0],[200,0,0]));
origin.append('line')
.attr('class','axis')
.attrs(lineGenerator([0,0,0],[0,400,0]));
origin.append('line')
.attr('class','axis')
.attrs(lineGenerator([0,0,-200],[0,0,200]));
origin.append('path')
.attr('class','spring')
.attr('d',pathGenerator(data));
</script>
</html>