-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
341 lines (305 loc) · 8.15 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
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Flows</title>
<style>
@import url(maps.css);
body {
background: #fff;
}
#map {
width: 1920px;
height: 960px;
margin: 100 auto;
position: relative;
}
#heading {
position: absolute;
top: 30px;
left: 350px;
width: 420px;
}
#heading h1 {
font-size: 3.5em;
margin: 0;
}
h2 {
clear: left;
}
.author a, .meta a {
color: #000;
}
.author, .meta {
color: #666;
font-style: italic;
font-size: small;
}
#map svg {
margin-top:20px;
}
.globe {
cursor: move;
}
.background {
stroke: none;
fill: #fff;
}
.outline {
stroke: #000;
stroke-width: 0px;
fill: none;
}
.graticule {
stroke: #000;
stroke-width: 0px;
stroke-opacity: .5;
fill: none;
}
.land {
stroke: #000;
stroke-width: 0px;
fill: #595f62;
}
.boundary {
stroke: #000;
stroke-width: 0px;
stroke-opacity: .7;
fill: none;
}
.face {
stroke: #000;
stroke-width: 0px;
stroke-dasharray: 8,3;
fill: none;
}
</style>
<div id="map">
<div id="heading">
<h1>Flows</h1>
</div>
<div id="inset">
</div>
</div>
<script src="d3.js"></script>
<script src="topojson.min.js"></script>
<script src="d3.geo.projection.min.js"></script>
<script src="d3.geo.polyhedron.js"></script>
<script>
//var rotate = [99.8, -241, -85],
var rotate = [99.8, -241, -85],
rotation = d3.geo.rotation(rotate),
ε = 1e-6,
π = Math.PI,
radians = π / 180,
degrees = 180 / π,
θ = Math.atan(.5) * degrees,
width = 1920,
height = 960;
var vertices = [
[0, 90],
[0, -90]
].concat(d3.range(10).map(function(i) {
return [(i * 36 + 180) % 360 - 180, i & 1 ? θ : -θ];
}));
var polyhedron = d3.range(5).map(function(i) { return [0, 2 + (3 + i * 2) % 10, 3 + i * 2]; }) // North
.concat(d3.range(10).map(function(i) { return [2 + i, 2 + (i < 1 ? 10 + i - 1 : i - 1), 2 + (1 + i) % 10]; })) // Equator
.concat(d3.range(5).map(function(i) { return [1, 2 + i * 2, 2 + (2 + i * 2) % 10]; })) // South
.map(function(d) {
return d.map(function(i) { return vertices[i]; });
});
console.log(polyhedron);
(function() {
polyhedron.forEach(function(face) {
face.centroid = d3.geo.centroid({type: "MultiPoint", coordinates: face});
});
// Split face 3 at centroid.
var face = polyhedron[3],
tmp = face.slice();
centroid = face.centroid;
face[0] = centroid;
// Uncomment to join extra face.
//face[1] = centroid;
//face.push(tmp[1]);
polyhedron.push(face = [tmp[0], centroid, tmp[2]]);
face.centroid = centroid;
// Comment out to join extra face.
polyhedron.push(face = [tmp[0], tmp[1], centroid]);
face.centroid = centroid;
// Split face 5 at edge.
face = polyhedron[5];
tmp = face.slice();
centroid = d3.geo.interpolate(face[1], face[2])(.5);
face[1] = centroid;
polyhedron.push(face = [tmp[0], tmp[1], centroid]);
face.centroid = polyhedron[5].centroid;
polyhedron[4].splice(2, 0, centroid);
})();
var centroids = [], // centroids for initial nearest-centroid search
extraCentroids = []; // optional secondary centroids for split faces
var faces = polyhedron.map(function(face) {
var centroid = face.centroid;
centroids.push(cartesian(centroid));
extraCentroids.push(cartesian(d3.geo.centroid({type: "MultiPoint", coordinates: face})));
// TODO shouldn't really need to reverse faces.
var tmp = face.slice();
tmp.push(tmp[0]);
if (d3.geo.area({type: "Polygon", coordinates: [tmp]}) > 2 * π) face.reverse();
return {
face: face,
project: d3.geo.gnomonic().scale(1).translate([0, 0]).rotate([-centroid[0], -centroid[1]])
};
});
// Connect each face to a parent face.
[
// N
-1, // 0
0, // 1
11, // 2
13, // 3
21, // 4 (change to 3 to join extra face)
// Eq
6, // 5
7, // 6
8, // 7
9, // 8
1, // 9
9, // 10
10, // 11
11, // 12
12, // 13
13, // 14
// S
6, // 15
8, // 16
18, // 17
12, // 18
18, // 19
2, // 20 - extra
3, // 21 - extra
4 // 22 - extra
].forEach(function(d, i) {
var node = faces[d];
node && (node.children || (node.children = [])).push(faces[i]);
});
// Flows: These parameters control the overall projection and can be used to translate and rotate all components.
var projection = d3.geo.polyhedron(faces[0], face, π / 6 - π / 2)
.rotate(rotate)
.scale(160)
.translate([width / 2 + 150, height / 2 - 200])
.center([0, 0])
.precision(1);
var mesh = {type: "MultiLineString", coordinates: projection.mesh.map(function(segment) {
return segment.map(rotation.invert);
}).filter(function(segment) {
// Don't show unwanted segment.
var d = d3.geo.distance.apply(null, segment);
return d > 1 || d < .6;
})};
var zoom = d3.behavior.zoom()
.scale(projection.scale())
.translate([0,0]) // not linked directly to projection
.on("zoom", redraw);
var path = d3.geo.path().projection(projection);
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height)
.call(zoom);
svg.append("path")
.datum({type: "Sphere"})
.attr("class", "background")
.attr("d", path);
svg.append("path")
.datum(d3.geo.graticule())
.attr("class", "graticule")
.attr("d", path);
svg.append("path")
.datum({type: "Sphere"})
.attr("class", "outline")
.attr("d", path);
svg.append("path")
.datum(mesh)
.attr("class", "face")
.attr("d", path);
d3.json("world-50m.json", function(error, world) {
svg.insert("path", ".graticule")
.datum(topojson.feature(world, world.objects.land))
.attr("class", "land")
.attr("d", path);
svg.insert("path", ".graticule")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("d", path);
});
// track last translation and scale event we processed
var tlast = [0,0],
slast = null;
function redraw() {
if (d3.event) {
var scale = d3.event.scale,
t = d3.event.translate;
console.log(t)
// if scaling changes, ignore translation (otherwise touch zooms are weird)
if (scale != slast) {
projection.scale(scale);
} else {
var dx = t[0]-tlast[0],
dy = t[1]-tlast[1],
yaw = projection.rotate()[0],
tp = projection.translate();
// use x translation to rotate based on current scale
//projection.rotate([yaw+360.*dx/width*1/scale, 0, 0]);
// use y translation to translate projection, clamped by min/max
projection.translate([tp[0]+dx,tp[1]+dy]);
}
// save last values. resetting zoom.translate() and scale() would
// seem equivalent but doesn't seem to work reliably?
slast = scale;
tlast = t;
}
svg.selectAll('path') // re-project path data
.attr('d', path);
}
function face(λ, φ) {
//Much slower!
//var point = [λ, φ],
// face = null,
// inside = 0;
//for (var i = 0, n = faces.length; i < n; ++i) {
// face = faces[i];
// if (d3.geo.pointInPolygon(point, face.polygon)) return face;
//}
var cosφ = Math.cos(φ),
p = [cosφ * Math.cos(λ), cosφ * Math.sin(λ), Math.sin(φ)],
c,
d,
min = Infinity,
best;
for (var i = 0, n = centroids.length - 3; i < n; ++i) {
c = centroids[i];
d = (d = (c[0] - p[0])) * d + (d = (c[1] - p[1])) * d + (d = (c[2] - p[2])) * d;
if (d < min) min = d, best = i;
}
if (best != null) {
if (best !== 5 && best !== 3) return faces[best];
// Secondary nearest-centroid search.
var candidates = best === 3 ? [3, 20, 21] : [5, 22];
best = null, min = Infinity;
for (var i = 0; i < candidates.length; ++i) {
c = extraCentroids[candidates[i]];
d = (d = (c[0] - p[0])) * d + (d = (c[1] - p[1])) * d + (d = (c[2] - p[2])) * d;
if (d < min) min = d, best = candidates[i];
}
return best != null && faces[best];
}
}
// Converts spherical coordinates (degrees) to 3D Cartesian.
function cartesian(coordinates) {
var λ = coordinates[0] * radians,
φ = coordinates[1] * radians,
cosφ = Math.cos(φ);
return [
cosφ * Math.cos(λ),
cosφ * Math.sin(λ),
Math.sin(φ)
];
}
</script>