-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
266 lines (226 loc) · 7.74 KB
/
script.js
File metadata and controls
266 lines (226 loc) · 7.74 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
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
const viewportRatio = viewportHeight / viewportWidth;
const viewportWidthInGridWidths = viewportRatio > 1 ? 21 : 59;
const gridSize = viewportWidth / viewportWidthInGridWidths;
const maxXCoord = 59;
const maxYCoord = 37;
const gridWidth = (maxXCoord + 1) * gridSize;
const gridHeight = (maxYCoord + 1) * gridSize;
const xOffset = (viewportWidthInGridWidths / 2 - 30) * gridSize;
const CSV_URL = 'https://docs.google.com/spreadsheets/d/16CjyorSwrzVsMXtdHecuu-C6HWVYqjJbgwG0p3ZFlWg/export?format=csv&gid=1371825706&single=true&output=csv'
function grid() {
const gridGroup = d3.select('#gridGroup');
const gridVisible = gridGroup.style('display') !== 'none';
gridGroup.style('display', gridVisible ? 'none' : '');
}
function updateInfoBox(data) {
const infoBoxRow = data.find(row => row.LongLabel === 'Infobox');
if (infoBoxRow && infoBoxRow.Description) {
d3.select('#info-box').html(infoBoxRow.Description);
}
}
(async () => {
const zoom = d3.zoom()
.scaleExtent([0.8, 5])
.on('zoom', zoomed);
const svg = d3.select('#grid')
.append('svg')
.attr('width', viewportWidth)
.attr('height', viewportHeight)
.call(zoom)
.append('g');
// Add blurred background image
const avifIsSupported = await isImageTypeSupported('image/avif');
const backgroundImageUrl = await (async () => {
if (avifIsSupported) return 'background.avif';
else if (await isImageTypeSupported('image/webp')) return 'background.webp';
else return '/background.jpg';
})();
const backgroundWidth = 60 * gridSize;
const backgroundHeight = (1950 / 3104) * 60 * gridSize;
const secondBackgroundScale = 1.5;
// Blurred background image
svg.append('image')
.attr('xlink:href', backgroundImageUrl)
.attr('x', 19*xOffset)
.attr('y', -backgroundHeight * (secondBackgroundScale-1)*0.8)
.attr('width', backgroundWidth * secondBackgroundScale)
.attr('height', backgroundHeight * secondBackgroundScale)
.attr('class', 'blurred-background');
// First blurred background
svg.append('image')
.attr('xlink:href', backgroundImageUrl)
.attr('x', xOffset)
.attr('y', 0)
.attr('width', backgroundWidth)
.attr('height', backgroundHeight)
.attr('class', 'first-blurred-background');
svg.append('image')
.attr('xlink:href', backgroundImageUrl)
.attr('x', xOffset)
.attr('y', 0)
.attr('width', backgroundWidth)
.attr('height', backgroundHeight)
.attr('class', 'first-blurred-background');
svg.append('image')
.attr('xlink:href', backgroundImageUrl)
.attr('x', xOffset)
.attr('y', 0)
.attr('width', backgroundWidth)
.attr('height', backgroundHeight)
.attr('class', 'first-blurred-background');
// Main background image
svg.append('image')
.attr('xlink:href', backgroundImageUrl)
.attr('x', xOffset)
.attr('y', 0)
.attr('width', backgroundWidth)
.attr('height', backgroundHeight);
const gridGroup = svg.append('g')
.attr('transform', `translate(${xOffset}, 0)`)
.attr('id', 'gridGroup');
function drawGrid() {
const rangeX = d3.range(0, gridWidth, gridSize);
const rangeY = d3.range(0, gridHeight, gridSize);
rangeX.forEach(x => {
gridGroup.append('line')
.attr('x1', x)
.attr('y1', 0)
.attr('x2', x)
.attr('y2', gridHeight)
.attr('class', 'grid-line');
});
rangeY.forEach(y => {
gridGroup.append('line')
.attr('x1', 0)
.attr('y1', y)
.attr('x2', gridWidth)
.attr('y2', y)
.attr('class', 'grid-line');
});
rangeX.forEach((x, i) => {
rangeY.forEach((y, j) => {
gridGroup.append('text')
.attr('x', x + 2)
.attr('y', y + 12)
.attr('class', 'grid-text')
.text(`${i},${j}`);
});
});
}
function zoomed(event) {
svg.attr('transform', event.transform);
}
drawGrid();
grid();
// Load processed image formats
const processedImageFormats = await (await fetch('logos/processed-formats.json')).json()
// Read CSV data and place a red square at the grid coordinates
d3.csv(CSV_URL)
.then(data => {
updateInfoBox(data); // Add this line
data.forEach(drawSquare);
setMapItemOrigins();
});
// Add the tooltip
const tooltip = d3.select('#tooltip');
function drawSquare(row) {
// Discard rows with 'x' in the 'hide' column
if (row.hide === 'x') return;
const x = +row.x;
const y = +row.y;
const scale = (+row.scale || 1) * 0.60;
let logo = row.logo;
const label = row.Label;
const link = row.Link;
const longLabel = row.LongLabel;
const description = row.Description;
// Calculate the position in pixels
const xPos = x * gridSize + xOffset;
const yPos = y * gridSize;
// Create a group for the logo, label, and link
const itemGroup = svg.append('g')
.attr('transform', `translate(${xPos}, ${yPos})`);
// Create a link
const itemLink = itemGroup.append('a')
.attr('href', link)
.attr('target', '_blank');
// Create a group for the logo and label, and apply hover effect to it
const contentGroup = itemLink.append('g')
.attr('class', 'mapItem')
.on('mouseover', function (event, row) {
tooltip
.style('left', (event.pageX + 10) + 'px')
.style('top', (event.pageY + 10) + 'px')
.html(`<strong>${longLabel}</strong><br>${description}`)
.classed('hidden', false);
})
.on('mouseout', function () {
tooltip.classed('hidden', true);
});
// Replace logo URL with processed version
const PREFIX = 'logos/'
const extension = '.' + logo.split('.').pop()
if (avifIsSupported && logo.startsWith(PREFIX) && processedImageFormats.includes(extension)) {
const name = logo.substring(PREFIX.length, logo.length - extension.length)
logo = `${PREFIX}avif/${name}.avif`
}
// Add the label
const labelText = label;
const labelMaxWidth = gridSize * scale * 2;
const fontsize = gridSize * scale * 0.3;
const itemContainer = contentGroup.append('foreignObject')
.attr('x', gridSize * scale / 2 - labelMaxWidth / 2)
.attr('y', 0)
.attr('width', labelMaxWidth)
.attr('font-size', fontsize)
.attr('height', gridSize * scale + 5 + 30);
const outerDiv = itemContainer.append('xhtml:div')
.attr('class', 'labelContainer')
.style('align-items', 'center');
outerDiv.append('xhtml:img')
.attr('alt', 'Image description')
.attr('src', logo)
.attr('style', `max-width: ${labelMaxWidth*0.9}px; max-height: ${labelMaxWidth*0.5}px`);
outerDiv.append('xhtml:div')
.text(labelText);
}
function setMapItemOrigins() {
// Set transform-origin dynamically for each square
d3.selectAll('.mapItem')
.each(function () {
const xPos = d3.select(this).attr('data-x');
const yPos = d3.select(this).attr('data-y');
d3.select(this).style('transform-origin', `${xPos}px ${yPos}px`);
});
}
})();
/**
*
* @param {string} mimeType
* @returns {Promise<boolean>}
*/
async function isImageTypeSupported(mimeType) {
// Create this:
//
// <picture>
// <source srcset="data:,x" type="{type}" />
// <img />
// </picture>
const img = document.createElement('img');
document.createElement('picture').append(
Object.assign(document.createElement('source'), {
srcset: 'data:,x', // Minimal valid URL
type: mimeType
}),
img
);
await 0; // Wait for img.currentSrc to be populated if format is supported
return !!img.currentSrc;
}
window.addEventListener('resize', () => {
d3.select('#grid svg')
.attr('width', window.innerWidth)
.attr('height', window.innerHeight);
});