-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-grid.js
More file actions
136 lines (114 loc) · 4.14 KB
/
image-grid.js
File metadata and controls
136 lines (114 loc) · 4.14 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
const gridSize = 15;
const coordSeparator = ',';
function getCenter(item, containerRect) {
const rect = item.getBoundingClientRect();
return {
x: rect.left + rect.width / 2 - containerRect.left,
y: rect.top + rect.height / 2 - containerRect.top,
};
}
function createTunnelLine(point1, point2, stroke, strokeWidth) {
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', point1.x);
line.setAttribute('y1', point1.y);
line.setAttribute('x2', point2.x);
line.setAttribute('y2', point2.y);
line.setAttribute('stroke', stroke);
line.setAttribute('stroke-width', strokeWidth);
return line;
}
function initTunnels(gridContainer) {
const showTunnelsChk = document.getElementById('showTunnels');
const tunnelOverlay = gridContainer.querySelector('.tunnel-overlay');
if (!tunnelOverlay) {
return;
}
const svg = tunnelOverlay.querySelector('svg');
if (!svg) {
return;
}
svg.setAttribute('width', gridContainer.clientWidth);
svg.setAttribute('height', gridContainer.clientHeight);
svg.setAttribute('viewBox', `0 0 ${gridContainer.clientWidth} ${gridContainer.clientHeight}`);
tunnelOverlay.style.display = 'none';
if (showTunnelsChk) {
showTunnelsChk.addEventListener('change', () => {
tunnelOverlay.style.display = showTunnelsChk.checked ? 'block' : 'none';
});
}
const tunnelsData = window.TUNNELS || {};
const containerRect = gridContainer.getBoundingClientRect();
Object.entries(tunnelsData).forEach(([from, to]) => {
const [fromRow, fromCol] = from.split(coordSeparator);
const [toRow, toCol] = to.split(coordSeparator);
const item1 = gridContainer.querySelector(`[data-coord="${fromRow}_${fromCol}"]`);
if (!item1) {
console.warn(`[tunnel] Could not find grid element ${fromRow}_${fromCol}`);
return;
}
const item2 = gridContainer.querySelector(`[data-coord="${toRow}_${toCol}"]`);
if (!item2) {
console.warn(`[tunnel] Could not find grid element ${toRow}_${toCol}`);
return;
}
const center1 = getCenter(item1, containerRect);
const center2 = getCenter(item2, containerRect);
svg.appendChild(createTunnelLine(center1, center2, 'black', '6'));
svg.appendChild(createTunnelLine(center1, center2, 'yellow', '2'));
});
}
function initGrid() {
if (typeof document === 'undefined') {
return;
}
const gridContainer = document.querySelector('.grid-container');
if (!gridContainer) {
return;
}
const notesData = window.COORD_NOTES || {};
for (let row = 1; row <= gridSize; row++) {
for (let colCode = 65; colCode < 65 + gridSize; colCode++) {
const col = String.fromCharCode(colCode);
const coordinate = `${row}${coordSeparator}${col}`;
const gridItem = document.createElement('div');
gridItem.className = 'grid-item';
gridItem.setAttribute('data-coord', `${row}_${col}`);
const img = document.createElement('img');
img.src = `imgs/${row}_${col}.png`;
img.loading = 'lazy';
gridItem.appendChild(img);
const badge = document.createElement('span');
badge.className = 'coordinate-badge';
badge.tabIndex = 0;
badge.textContent = coordinate;
const noteText = notesData[coordinate];
if (noteText) {
const tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.textContent = `${coordinate}: ${noteText}`;
badge.appendChild(tooltip);
}
gridItem.appendChild(badge);
// Insert grid item without referencing tunnelOverlay
gridContainer.appendChild(gridItem);
}
}
// Initialize tunnels after grid is set up
initTunnels(gridContainer);
}
if (typeof window !== 'undefined') {
window.initGrid = initGrid;
window.initTunnels = initTunnels;
window.getCenter = getCenter;
window.createTunnelLine = createTunnelLine;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = { initGrid, initTunnels, getCenter, createTunnelLine };
}
if (typeof document !== 'undefined') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initGrid);
} else {
initGrid();
}
}