-
Notifications
You must be signed in to change notification settings - Fork 0
/
manipulate-svg.js
235 lines (188 loc) · 8.13 KB
/
manipulate-svg.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
document.addEventListener('DOMContentLoaded', () => {
const svgContainer = document.getElementById('svg-container');
// Load the SVG file
fetch('RegionMap.svg')
.then(response => response.text())
.then(svgText => {
// Insert the SVG into the container
svgContainer.innerHTML = svgText;
// After loading SVG, fetch coordinates and add circles
fetchCoordinatesAndAddCircles(svgContainer); // Pass svgContainer as an argument
})
.catch(error => console.error('Error fetching the SVG:', error));
});
async function fetchCoordinates(cmdr_system) {
try {
// Make a request to the API
const response = await fetch(`https://us-central1-canonn-api-236217.cloudfunctions.net/query/typeahead?q=${cmdr_system}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Parse the response as JSON
const data = await response.json();
// Find the entry with the matching name in the min_max array
const entry = data.min_max.find(item => item.name === cmdr_system);
// If entry is found, return the coordinates as an array
if (entry) {
return [entry.x, entry.y, entry.z];
} else {
return null;
}
} catch (error) {
console.error('Error fetching data:', error);
return null;
}
}
async function fetchCoordinatesAndAddCircles(svgContainer) {
url = new URL(window.location.href);
// Extract the query parameters
params = new URLSearchParams(url.search);
// Get the value of the 'cmdr' parameter
const cmdr = params.get('cmdr');
if (!cmdr) {
console.error('No cmdr parameter found in the URL');
return;
}
const apiUrl = `https://us-central1-canonn-api-236217.cloudfunctions.net/query/missing/codex?cmdr=${cmdr}`;
try {
const apiResponse = await fetch(apiUrl);
if (!apiResponse.ok) {
throw new Error(`API request failed with status ${apiResponse.status}`);
}
const data = await apiResponse.json();
// we could filter some but no need
const filteredItems = Object.values(data)
// Process each item asynchronously
for (const item of filteredItems) {
fetchAndRenderItem(svgContainer, item);
}
} catch (error) {
console.error('Error fetching data from API:', error);
}
}
async function fetchAndRenderItem(svgContainer, item) {
const { entryid, hud_category } = item;
const coordinatesUrl = `https://storage.googleapis.com/canonn-downloads/dumpr/${hud_category}/${entryid}.csv`;
try {
const response = await fetch(coordinatesUrl);
if (!response.ok) {
throw new Error(`CSV request failed with status ${response.status}`);
}
const csvText = await response.text();
await renderData(svgContainer, item, csvText);
} catch (error) {
console.error(`Error fetching CSV for entryid ${entryid} and hud_category ${hud_category}:`, error);
}
}
async function renderData(svgContainer, item, csvText) {
const svgElement = svgContainer.querySelector('svg');
if (!svgElement) return;
const { entryid, hud_category, english_name } = item;
// Split CSV text into lines
const lines = csvText.trim().split('\n');
const circleGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
circleGroup.setAttribute('id', 'circleGroup');
svgElement.append(circleGroup); // Add the group as the first child of the SVG
// Process each line (assuming no header and x, y are in fields 2 and 3)
let index = 0;
const batchSize = 1000; // Number of circles to add in each batch
const intervalId = setInterval(() => {
for (let i = 0; i < batchSize && index < lines.length; i++) {
const line = lines[index];
const fields = line.split(',');
if (fields.length >= 3) {
const x = parseFloat(fields[1]);
const y = parseFloat(fields[3]);
const title = fields[0] + '<br>' + english_name;
const cRegion = fields[6];
const bRegion = fields[7];
var regionElement = svgElement.getElementById("Region_" + cRegion.padStart(2, '0'));
// Check if the element exists
if (regionElement) {
// Set the fill-opacity to 50%
regionElement.style.fillOpacity = "0.5";
}
if (!isNaN(x) && !isNaN(y)) {
const tx = ((x + 49985) * 83 / 4096);
const ty = ((y + 24105) * 83 / 4096);
addCircleToSVG(svgElement, circleGroup, tx, 2048 - ty, title, fields[0], entryid, hud_category);
}
}
index++;
}
if (index >= lines.length) {
clearInterval(intervalId); // Stop interval when all circles are added
}
}, 0); // Interval of 0ms (no delay)
var paths = svgElement.querySelectorAll('.borderlines');
paths.forEach(function (path) {
svgElement.appendChild(path); // Move each text element to the end of the SVG
path.style.pointerEvents = "none";
});
var texts = svgElement.querySelectorAll('text');
texts.forEach(function (text) {
svgElement.appendChild(text); // Move each text element to the end of the SVG
text.style.pointerEvents = "none";
});
}
function showTooltip(evt, text) {
let tooltip = document.getElementById("tooltip");
tooltip.innerHTML = text;
tooltip.style.display = "block";
tooltip.style.left = evt.pageX + 10 + 'px';
tooltip.style.top = evt.pageY + 10 + 'px';
}
function hideTooltip() {
var tooltip = document.getElementById("tooltip");
tooltip.style.display = "none";
}
function addCircleToSVG(parent, svgElement, x, y, hoverText, systemName, entryid, hud_category) {
const svgns = 'http://www.w3.org/2000/svg';
// Create circle element
const newCircle = document.createElementNS(svgns, 'circle');
newCircle.setAttribute('cx', x);
newCircle.setAttribute('cy', y);
newCircle.setAttribute('r', '7'); // Adjust radius as needed
newCircle.setAttribute('fill', 'red');
newCircle.setAttribute('fill-opacity', '0.5'); // 50% transparency
newCircle.setAttribute('class', `entry_${entryid}`); // Set the class to the entryid
// Add click event listener to copy the title to the clipboard
newCircle.addEventListener('click', () => {
navigator.clipboard.writeText(systemName).then(() => {
console.log(`Copied to clipboard: ${systemName}`);
}).catch(err => {
console.error('Could not copy text: ', err);
});
});
newCircle.addEventListener('contextmenu', (event) => {
// Prevent the default context menu from appearing
event.preventDefault();
// Handle the right-click event
window.open(`https://canonn-science.github.io/Codex-Regions/?entryid=${entryid}&hud_category=${hud_category}`, '_blank');
// You can perform other actions here, like showing a custom context menu
});
// Append circle element to SVG
svgElement.appendChild(newCircle);
// Event listener on SVG element to handle mouseover and mouseout events
parent.addEventListener('mouseover', function (event) {
if (event.target === newCircle) {
const circles = document.querySelectorAll(`.entry_${entryid}`);
circles.forEach(circle => {
circle.setAttribute('fill', 'yellow');
//now bring the element to the front
svgElement.appendChild(circle);
});
showTooltip(event, hoverText);
}
});
parent.addEventListener('mouseout', function (event) {
if (event.target === newCircle) {
const circles = document.querySelectorAll(`.entry_${entryid}`);
circles.forEach(circle => {
circle.setAttribute('fill', 'red');
});
hideTooltip();
}
});
//console.log(`Added circle at (${x}, ${y}) with hover text "${hoverText}"`);
}