-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
338 lines (308 loc) · 10.3 KB
/
map.js
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
/* First, define what constitutes a small screen.
This will affect the zoom parameter for each chapter. */
var smallMedia = window.matchMedia("(max-width: 600px)").matches;
/* Next, create two variables that will hold:
1. The different types of layers available to Mapbox and their
respective opacity attributes.
2. The possible alignments which could be applied to the vignettes.*/
var layerTypes = {
fill: ["fill-opacity"],
line: ["line-opacity"],
circle: ["circle-opacity", "circle-stroke-opacity"],
symbol: ["icon-opacity", "text-opacity"],
raster: ["raster-opacity"],
"fill-extrusion": ["fill-extrusion-opacity"],
heatmap: ["heatmap-opacity"],
};
var alignments = {
left: "lefty",
center: "centered",
right: "righty",
full: "fully",
};
/* The next two functions help turn on and off individual
layers through their opacity attributes: The first one gets
the type of layer and the second one adjusts the layer's opacity */
function getLayerPaintType(layer) {
var layerType = map.getLayer(layer).type;
return layerTypes[layerType];
}
function setLayerOpacity(layer) {
var paintProps = getLayerPaintType(layer.layer);
paintProps.forEach(function (prop) {
var options = {};
if (layer.duration) {
var transitionProp = prop + "-transition";
options = { duration: layer.duration };
map.setPaintProperty(layer.layer, transitionProp, options);
}
map.setPaintProperty(layer.layer, prop, layer.opacity, options);
});
}
/* Next, these variables and functions create the story and vignette html
elements, and populate them with the content from the config.js file.
They also assign a css class to certain elements, also based on the
config.js file */
// Main 'story', 'features' and 'header' elements
var story = document.getElementById("story");
var features = document.createElement("div");
var header = document.createElement("div");
features.setAttribute("id", "features");
// If the content exists, then assign it to the 'header' element
// Note how each one of these are assigning 'innerHTML'
if (config.topTitle) {
var topTitle = document.createElement("div");
topTitle.innerHTML = config.topTitle;
header.appendChild(topTitle);
}
if (config.title) {
var titleText = document.createElement("div");
titleText.innerHTML = config.title;
header.appendChild(titleText);
}
if (config.subtitle) {
var subtitleText = document.createElement("div");
subtitleText.innerHTML = config.subtitle;
header.appendChild(subtitleText);
}
if (config.byline) {
var bylineText = document.createElement("div");
bylineText.innerHTML = config.byline;
header.appendChild(bylineText);
}
if (config.description) {
var descriptionText = document.createElement("div");
descriptionText.innerHTML = config.description;
header.appendChild(descriptionText);
}
// If after this, the header has anything in it, it gets appended to the story
if (header.innerText.length > 0) {
header.classList.add(config.theme);
header.setAttribute("id", "header");
story.appendChild(header);
}
/* After building the elements and assigning content to the header these
functions will loop through the chapters in the config.js file,
create the vignette elements and assign them their respective content */
config.chapters.forEach((record, idx) => {
/* These first two variables will hold each vignette, the chapter
element will go in the container element */
var container = document.createElement("div");
var chapter = document.createElement("div");
// Adds a class to the vignette
chapter.classList.add("br3");
// Adds all the content to the vignette's div
chapter.innerHTML = record.chapterDiv;
// Sets the id for the vignette and adds the step css attribute
container.setAttribute("id", record.id);
container.classList.add("step");
// If the chapter is the first one, set it to active
if (idx === 0) {
container.classList.add("active");
}
// Creates the image credit for the vignette
if (record.imageCredit) {
var imageCredit = document.createElement('p');
imageCredit.classList.add('imageCredit');
imageCredit.innerHTML = 'Image credit: ' + record.imageCredit;
chapter.appendChild(imageCredit);
}
// Adds the overall theme to the chapter element
chapter.classList.add(config.theme);
/* Appends the chapter to the container element and the container
element to the features element */
container.appendChild(chapter);
container.classList.add(alignments[record.alignment] || "centered");
if (record.hidden) {
container.classList.add("hidden");
}
features.appendChild(container);
});
// Appends the features element (with the vignettes) to the story element
story.appendChild(features);
/* Next, this section creates the footer element and assigns it
its content based on the config.js file */
var footer = document.createElement("div");
// This assigns all the content to the footer element
if (config.footer) {
var footerText = document.createElement("p");
footerText.innerHTML = config.footer;
footer.appendChild(footerText);
}
// If the footer element contains any content, add it to the story
if (footer.innerText.length > 0) {
footer.classList.add(config.theme);
footer.setAttribute("id", "footer");
story.appendChild(footer);
}
// Adds the Mapbox access token
mapboxgl.accessToken = config.accessToken;
// Honestly, don't know what this does
const transformRequest = (url) => {
const hasQuery = url.indexOf("?") !== -1;
const suffix = hasQuery
? "&pluginName=scrollytellingV2"
: "?pluginName=scrollytellingV2";
return {
url: url + suffix,
};
};
// Creates a variable to hold the starting zoom value
var startingZoom;
// If the screen size is small, it uses the `zoomSmall` value
if (smallMedia) {
startingZoom = config.chapters[0].location.zoomSmall;
} else {
startingZoom = config.chapters[0].location.zoom;
}
/* This section creates the map element with the
attributes from the main section of the config.js file */
var map = new mapboxgl.Map({
container: "map",
style: config.style,
center: config.chapters[0].location.center,
zoom: startingZoom,
bearing: config.chapters[0].location.bearing,
pitch: config.chapters[0].location.pitch,
interactive: false,
transformRequest: transformRequest,
});
if (config.showMarkers) {
var marker = new mapboxgl.Marker({ color: config.markerColor });
marker.setLngLat(config.chapters[0].location.center).addTo(map);
}
// Instantiates the scrollama function
var scroller = scrollama();
/* Here we add the two extra layers we are using, just like in our previous
tutorial. At the end, however, we setup the functions that will tie the
scrolling to the chapters and move the map from one location to another
while changing the zoom level, pitch and bearing */
map.on("click", "timbulandki22", function(e) {
let city = e.features[0].properties.x2;
let year = e.features[0].properties.tahun;
let waste = e.features[0].properties.x4;
let more = e.features[0].properties.more;
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML('<p><b>City:</b>'+city+'<br>'+'<p><b>Year:</b>'+year+'<br>'+'<p><b>Waste generation/day:</b>'+waste)
.addTo(map);
});
map.on("load", function () {
// Add 3d terrain if necessary
if (config.use3dTerrain) {
map.addSource("mapbox-dem", {
type: "raster-dem",
url: "mapbox://mapbox.mapbox-terrain-dem-v1",
tileSize: 512,
maxzoom: 14,
});
// Add the DEM source as a terrain layer with exaggerated height
map.setTerrain({ source: "mapbox-dem", exaggeration: 1.5 });
// Add a sky layer that will show when the map is highly pitched
map.addLayer({
id: "sky",
type: "sky",
paint: {
"sky-type": "atmosphere",
"sky-atmosphere-sun": [0.0, 0.0],
"sky-atmosphere-sun-intensity": 15,
},
});
}
/* map.addLayer(
{
id: "timbulandki22",
type: "fill",
source: {
type: "geojson",
data: "data/timbulandki.geojson",
},
paint: {
"fill-color": [
"match",
["get", "DM"],
4,
"#69110a",
3,
"#d42c1f",
2,
"#f4ac3d",
1,
"#f6d38b",
0,
"#f9f89d",
"#ffffff"
],
"fill-opacity":0,
},
},
"waterway-label"
);
*/
/* map.on('mousemove', (event) => {
const states = map.queryRenderedFeatures(event.point, {
layers: ['timbulandki22']
});
document.getElementById('x4').innerHTML = states.length
? `<h3>${states[0].properties.name}</h3><p><strong><em>${states[0].properties.density}</strong> people per square mile</em></p>`
: `<p>Hover over a state!</p>`;
}); */
// Setup the instance, pass callback functions
scroller
.setup({
step: ".step",
offset: 0.75,
progress: true,
})
.onStepEnter((response) => {
var chapter = config.chapters.find(
(chap) => chap.id === response.element.id
);
response.element.classList.add("active");
let thisZoom;
if (smallMedia) {
thisZoom = chapter.location.zoomSmall;
} else {
thisZoom = chapter.location.zoom;
}
thisLocation = {
bearing: chapter.location.bearing,
center: chapter.location.center,
pitch: chapter.location.pitch,
zoom: thisZoom,
};
map[chapter.mapAnimation || "flyTo"](thisLocation);
if (config.showMarkers) {
marker.setLngLat(chapter.location.center);
}
if (chapter.onChapterEnter.length > 0) {
chapter.onChapterEnter.forEach(setLayerOpacity);
}
if (chapter.callback) {
window[chapter.callback]();
}
if (chapter.rotateAnimation) {
map.once("moveend", function () {
const rotateNumber = map.getBearing();
map.rotateTo(rotateNumber + 90, {
duration: 24000,
easing: function (t) {
return t;
},
});
});
}
})
.onStepExit((response) => {
var chapter = config.chapters.find(
(chap) => chap.id === response.element.id
);
response.element.classList.remove("active");
if (chapter.onChapterExit.length > 0) {
chapter.onChapterExit.forEach(setLayerOpacity);
}
});
});
/* Here we watch for any resizing of the screen to
adjust our scrolling setup */
window.addEventListener("resize", scroller.resize);