forked from rustbridge/a-very-brief-intro-to-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globe.html
205 lines (170 loc) · 4.4 KB
/
globe.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
---
permalink: /globe/
---
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="utf-8">
<style>
.country {
fill: #b8b8b8;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
}
.graticule {
fill: none;
stroke: #000;
stroke-opacity: .3;
stroke-width: .5px;
}
.graticule-outline {
fill: none;
stroke: #333;
stroke-width: 1.5px;
}
text {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 18px;
font-weight: bold;
text-anchor: middle;
}
svg {
margin-left: -125px;
}
</style>
</head><body>
<script src="/js/d3.js"></script>
<script src="/js/topojson.js"></script>
<script>
// Each entry: [name, lat, lon]
var city_data = [
// 2016
["Berlin", "52.52", "13.41"],
["Pittsburgh", "40.44", "-79.99"],
// 2017
["Kyiv", "50.45", "30.52"],
["Mexico City", "19.43", "-99.13"],
["Portland", "45.52", "-122.67"],
["Zurich", "47.36", "8.55"],
["Columbus", "39.98", "-82.98"],
// 2018
["Nairobi", "-01.29", "36.82"],
["Paris", "48.85", "02.35"],
["Rome", "41.90", "12.49"],
// 2019
["Beijing", "39.90", "116.40"],
["Lima", "-12.02", "-77.26"],
];
var width = 960,
height = 500;
var centroid = d3.geo.path()
.projection(function(d) { return d; })
.centroid;
var projection = d3.geo.orthographic()
.scale(248)
.clipAngle(90);
var path = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule()
.extent([[-180, -90], [180 - .1, 90 - .1]]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
var line = svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
svg.append("circle")
.attr("class", "graticule-outline")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", projection.scale());
var locations = svg.append('svg:g').attr('id', 'locations');
var rotate = d3_geo_greatArcInterpolator();
d3.json("/js/index.json", function(error, world) {
var countries = topojson.object(world, world.objects.countries).geometries,
i = -1,
n = countries.length;
var country = svg.selectAll(".country")
.data(countries)
.enter().insert("path", ".graticule")
.attr("class", "country")
.attr("d", path);
var cities = locations.selectAll('path')
.data(city_data)
.enter()
.append('svg:path')
.datum(function(d) {
return {
type: "Point",
// d3 wants longitude THEN latitude!!!
coordinates: [d[2], d[1]]
};
})
.attr('class', 'geo-node')
.attr("d", path.pointRadius(5))
.attr('d', path)
.style("fill", "red");
globe_rotate();
function globe_rotate() {
d3.transition()
.duration(10)
.tween("rotate", function() {
var orig = projection.rotate();
rotate.source(projection.rotate()).target([orig[0] + 1, 0]).distance();
return function(t) {
projection.rotate(rotate(t));
country.attr("d", path);
line.attr("d", path);
cities.attr("d", path);
};
})
.transition()
.each("end", globe_rotate);
}
});
var d3_radians = Math.PI / 180;
function d3_geo_greatArcInterpolator() {
var x0, y0, cy0, sy0, kx0, ky0,
x1, y1, cy1, sy1, kx1, ky1,
d,
k;
function interpolate(t) {
var B = Math.sin(t *= d) * k,
A = Math.sin(d - t) * k,
x = A * kx0 + B * kx1,
y = A * ky0 + B * ky1,
z = A * sy0 + B * sy1;
return [
Math.atan2(y, x) / d3_radians,
Math.atan2(z, Math.sqrt(x * x + y * y)) / d3_radians
];
}
interpolate.distance = function() {
if (d == null) k = 1 / Math.sin(d = Math.acos(Math.max(-1, Math.min(1, sy0 * sy1 + cy0 * cy1 * Math.cos(x1 - x0)))));
return d;
};
interpolate.source = function(_) {
var cx0 = Math.cos(x0 = _[0] * d3_radians),
sx0 = Math.sin(x0);
cy0 = Math.cos(y0 = _[1] * d3_radians);
sy0 = Math.sin(y0);
kx0 = cy0 * cx0;
ky0 = cy0 * sx0;
d = null;
return interpolate;
};
interpolate.target = function(_) {
var cx1 = Math.cos(x1 = _[0] * d3_radians),
sx1 = Math.sin(x1);
cy1 = Math.cos(y1 = _[1] * d3_radians);
sy1 = Math.sin(y1);
kx1 = cy1 * cx1;
ky1 = cy1 * sx1;
d = null;
return interpolate;
};
return interpolate;
}
</script>
</body></html>