forked from cs480x-22c/final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
280 lines (243 loc) · 8.84 KB
/
script.js
File metadata and controls
280 lines (243 loc) · 8.84 KB
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
const svgSize = 550;
const datatipWidth = 250;
let activeConstellation = null;
let constellationInfo;
let fullPaths;
let fullStars;
//for easy access
const COLORS =
{
begin: 'black',
isActive: '#ee5533',
justActive: '#ddaa11',
inActive: '#74c3c4'
}
//load files
Promise.all([
d3.json('datasets/paths.geojson'),
d3.csv('datasets/stars.csv'),
d3.json('datasets/paths_north.geojson'),
d3.csv('datasets/stars_north.csv'),
d3.json('datasets/paths_south.geojson'),
d3.csv('datasets/stars_south.csv'),
d3.csv('datasets/StarDescriptions.csv')
]).then(([paths, stars, pathsN, starsN, pathsS, starsS, desc]) => {
createMap(pathsN, starsN, [0, -90], "#north"); //north hemisphere
createMap(pathsS, starsS, [180, 90], "#south"); //south hemisphere
appendConstellations(desc); //search
//store info in global variables for easy access
constellationInfo = desc;
fullPaths = paths;
fullStars = stars;
});
//draw stars in specified hemisphere
function createMap(data, stars, rotation, svgID) {
let proj = d3.geoAzimuthalEqualArea()
.rotate(rotation)
.fitExtent([[5, 5], [svgSize - 5, svgSize - 5]], data);
let gpath = d3.geoPath().projection(proj);
const svg = d3.select(svgID);
drawConstellations(svg, data.features, stars, proj, gpath, COLORS.begin, true);
//draw border
svg.append("circle")
.attr("cx", svgSize / 2)
.attr("cy", svgSize / 2)
.attr("r", ((svgSize - 10) / 2))
.attr("stroke-width", 2)
.attr("stroke", "white")
.attr("fill", "none");
}
//draw constellations based on projection, paths, and stars
function drawConstellations(svg, features, stars, proj, gpath, pathColor, attachEvent) {
//scale star radius based on brightness
sizeScale = d3.scaleLinear()
.domain([6.07, -1.46]) // unit: magnitude
.range([1, 5]) // unit: pixels
// draw constellation
svg.selectAll('path')
.data(features)
.enter()
.append('path')
.attr('d', function (d) { return gpath(d); })
.attr('stroke-width', 2)
.attr('stroke', pathColor)
.on("mouseover", e => {
if (attachEvent) //only call animations if path in map (not datatip)
mouseOverConstellation(e);
})
.on("mouseout", e => {
if (attachEvent)
mouseOffConstellation(e);
});
if (attachEvent)
svg.selectAll("path")
.attr('id', d => d.properties.name.replace(" ", "_"));
// draw stars
svg.selectAll('circle')
.data(stars)
.enter()
.append('circle')
.attr('cx', d => proj([d.Lon, d.Lat])[0])
.attr('cy', d => proj([d.Lon, d.Lat])[1])
.attr("r", d => sizeScale(d.Mag))
.attr('class', d => d.Constellation.replace(" ", "_"))
.attr("fill", "#aaaaaa")
.on("mouseover", e => {
if (attachEvent)
mouseOverStar(e);
})
.on("mouseout", e => {
if (attachEvent)
mouseOffStar(e);
});
addBlur(svg);
}
//add blurs on paths
function addBlur(svg) {
//TODO: find source
//Container for the gradients
let defs = svg.append("defs");
//Filter for the outside glow
let filter = defs.append("filter")
.attr("id", "glow");
filter.append("feGaussianBlur")
.attr("stdDeviation", "3.5")
.attr("result", "coloredBlur");
let feMerge = filter.append("feMerge");
feMerge.append("feMergeNode")
.attr("in", "coloredBlur");
feMerge.append("feMergeNode")
.attr("in", "SourceGraphic");
d3.selectAll("path")
.style("filter", "url(#glow)");
//end glow
}
//functions for animations
function mouseOverStar(e) {
activateConstellation('#' + e.target.classList[0], true)
}
function mouseOffStar(e) {
//activateConstellation('#' + e.target.classList[0], false)
}
function mouseOverConstellation(e) {
activateConstellation('#' + e.target.id, true)
}
function mouseOffConstellation(e) {
//activateConstellation('#' + e.target.id, false)
}
function activateConstellation(path, active) {
if (active) {
//unactivate current constellation
activeConstellation ? activateConstellation(activeConstellation, false) : null
activeConstellation = path;
showDatatip(path);
d3.selectAll(path)
.transition().duration(200)
.style('stroke-width', '5')
.style('stroke', COLORS.isActive);
}
else {
activeConstellation = null
//hideDatatip();
d3.selectAll(path)
.transition().duration(200)
.style('stroke-width', '2')
.style('stroke', COLORS.justActive)
.transition().duration(5000)
.style('stroke', COLORS.inActive);
}
}
//add options to search dropdown
function appendConstellations(data) {
var divTag = document.getElementById("constellationDropdown");
for (var i = 0; i < data.length; i++) {
var option = document.createElement("option");
option.value = data[i]["Star Name"];
option.innerHTML = data[i]["Star Name"];
divTag.appendChild(option);
option.addEventListener("click", e => {
activateConstellation(('#' + e.target.value.replace(" ", "_")), true);
});
}
}
//search filter function
function filter() {
let searchField = document.getElementById("searchInput").value.toUpperCase().trim();
const div = document.getElementById("constellationDropdown");
const options = Array.from(div.getElementsByTagName("option"));
//show options that match search field
options.filter((option) => {
return option.value.toUpperCase().includes(searchField);
}).forEach(option => {
option.classList.remove("hidden");
});
//filter out options that don't match search field
options.filter((option) => {
return !option.value.toUpperCase().includes(searchField);
}).forEach(option => {
option.classList.add("hidden");
});
}
//draw datatips in side panel
function showDatatip(constellation) {
//hide search
d3.select("#dropdown")
.classed("hidden", true);
constellation = constellation.substring(1);
constellation = constellation.replace("_", " ");
info = constellationInfo.filter(item => item["Star Name"] == constellation)[0];
let desc = info.Description;
let history = info.History;
let html = `<button id="returnToSearch" onclick="hideDatatip()"><i class="arrow left"></i> Return to Search</button>
<p class="title">${constellation}</p>
<p class="desc">The ${desc}</p>
<div><svg id="datatipImg" width=${datatipWidth - 10} height=${datatipWidth - 10}></svg></div>
<p class="history">${history}</p>
<a class="link" href="http://www.seasky.org/constellations/constellations.html">Source: Sea and Sky</a>`;
d3.select("#side-panel")
.html(html);
//draw constellation in svg
//filter paths and stars for selected constellation
let path = fullPaths.features.filter(feature => feature.properties.name == constellation)[0];
let geojson = { "type": "FeatureCollection", "features": [path] };
let stars = fullStars.filter(star => star["Constellation"] == constellation);
//figure out what rotation to use (average lat and lon in path, removing duplicates)
lon1 = path.geometry.coordinates.map(x => x[0][0]);
lon2 = path.geometry.coordinates.map(x => x[1][0]);
lon = lon1.concat(lon2);
lon = [...new Set(lon)];
//if constellation crosses over longitude 180, need transformation
if (Math.max.apply(Math, lon) - Math.min.apply(Math, lon) > 180) {
lon = lon.map(x => {
if (x < 0) return 180 + (180 - Math.abs(x));
else return x;
})
avgLon = (lon.reduce((x, y) => x + y, 0) / lon.length);
}
else {
lon = lon.map(x => x + 180);
avgLon = (lon.reduce((x, y) => x + y, 0) / lon.length) - 180;
}
lat1 = path.geometry.coordinates.map(x => x[0][1])
lat2 = path.geometry.coordinates.map(x => x[1][1])
lat = lat1.concat(lat2);
lat = [...new Set(lat)];
avgLat = lat.reduce((x, y) => x + y, 0) / lat.length;
let imgPadding = 20;
let proj = d3.geoAzimuthalEqualArea()
.rotate([-avgLon, -avgLat])
.fitExtent([[imgPadding, imgPadding], [datatipWidth - imgPadding - 10, datatipWidth - imgPadding - 10]], geojson)
let gpath = d3.geoPath().projection(proj);
let svg = d3.select("#datatipImg");
drawConstellations(svg, geojson.features, stars, proj, gpath, COLORS.inActive, false);
}
function hideDatatip() {
//show search
d3.select("#dropdown")
.classed("hidden", false);
//clear datatip
d3.select("#side-panel")
.html("");
//unactivate constellation
activateConstellation(activeConstellation, false);
}