-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.js
152 lines (133 loc) Β· 4.82 KB
/
main.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
import { saveAs } from "file-saver";
import { findTiles, getTileTypes, initializeTiles } from "./algo";
import { fetchContributions, getConvertedContributions } from "./api";
import { INIT_CONTRIBUTIONS } from "./constants";
import {
changeShadowPreset,
clearScene,
convertSceneToStlBlob,
createScene,
renderBuilding,
renderGrass,
renderRoad,
} from "./scene";
// Get HTML elements
const autoRotateButton = document.getElementById("autorotate");
const downloadButton = document.getElementById("download");
const yearSelect = document.getElementById("yearSelect");
const usernameInput = document.getElementById("usernameInput");
const infoForm = document.getElementById("infoForm");
const selectionScreen = document.getElementById("selectionScreen");
const titleLink = document.getElementById("title");
const displayInfo = document.getElementById("displayInfo");
const errorMessage = document.getElementById("errorMessage");
const shadowPreset = document.getElementById("shadowPreset");
// Populate year select with years down to 2008
const currentYear = new Date().getFullYear();
for (let y = currentYear; y >= 2008; y--) {
const option = document.createElement("option");
option.value = y;
option.innerHTML = y;
yearSelect.appendChild(option);
}
// Create 3D environment
const { scene, controls } = createScene();
const renderShiftX = -26;
const renderShiftY = -4;
const renderShiftZ = 0.38;
let enteredInfo = false;
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("name") && urlParams.get("year")) {
// Generate city with url params
enteredInfo = true;
const name = urlParams.get("name");
const year = urlParams.get("year");
generateCityFromParams(name, year);
} else {
// Placeholder city
generateCity(INIT_CONTRIBUTIONS);
}
/* - - Input events - - */
// Set autorotations
autoRotateButton.onclick = (e) => {
e.preventDefault();
controls.autoRotate = !controls.autoRotate;
if (controls.autoRotate) autoRotateButton.classList.remove("inactive");
else autoRotateButton.classList.add("inactive");
};
// Submit form, get data and generate city
infoForm.onsubmit = async (e) => {
e.preventDefault();
const name = usernameInput.value.trim();
const year = yearSelect.value;
enteredInfo = true;
const tempArray = window.location.href.split("?");
const baseURL = tempArray[0];
const newUrl = `${baseURL}?name=${name}&year=${year}`;
window.history.replaceState("", "", newUrl);
await generateCityFromParams(name, year);
};
// Open UI back, start autorotation
titleLink.onclick = (e) => {
e.preventDefault();
if (!selectionScreen.classList.contains("hidden") && enteredInfo) {
selectionScreen.classList.add("hidden");
} else {
selectionScreen.classList.remove("hidden");
controls.autoRotate = true;
infoForm.style.display = "";
errorMessage.style.display = "none";
}
if (controls.autoRotate) autoRotateButton.classList.remove("inactive");
else autoRotateButton.classList.add("inactive");
};
shadowPreset.onchange = () => {
changeShadowPreset(scene, shadowPreset.value);
};
async function generateCityFromParams(name, year) {
// Get data from API
const apiContribs = await fetchContributions(name, year);
if (apiContribs == null) {
errorMessage.style.display = "block";
return;
}
// Convert data to 2D array
const contribs = getConvertedContributions(apiContribs);
selectionScreen.classList.add("hidden");
infoForm.style.display = "none";
displayInfo.innerHTML = `<span>${name}</span> <span>${year}</span>`;
// Render data
generateCity(contribs);
downloadButton.onclick = (e) => {
e.preventDefault();
const blob = convertSceneToStlBlob(scene);
saveAs(blob, `${name}-${year}-city.stl`);
};
}
// Get the 2D array containing contributions and make stuff happen
function generateCity(contribs) {
// Reset city
clearScene(scene);
// Initialize city layout for initial view
initializeTiles(contribs);
findTiles();
const tileTypes = getTileTypes();
// Render city
for (let i = 0; i < tileTypes.length; i++) {
for (let j = 0; j < tileTypes[0].length; j++) {
const tileType = tileTypes[i][j];
const x = 2 * (j + renderShiftX) * 1.1;
const z = 2 * (i + renderShiftY) * 1.1;
if (tileType.tile === 0) {
// Render grass tiles
renderGrass(x, 0, z, scene);
} else if (tileType.tile === 1) {
// Render road tiles
renderRoad(x, -0.015, z, tileTypes[i][j], scene);
} else if (tileType.tile === 2) {
// Render building tiles
renderBuilding(x, 2 * renderShiftZ, z, tileTypes[i][j], scene);
}
}
}
}