-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
471 lines (407 loc) · 13.2 KB
/
Copy pathindex.html
File metadata and controls
471 lines (407 loc) · 13.2 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>World Map Color Highlighter</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<style>
body {
margin: 0;
background: #fff:
color: #111;
font-family: sans-serif;
}
nav {
background-color: #222;
color: white;
display: flex;
justify-content: space-between;
padding: 10px 20px;
align-items: center;
font-size: 18px;
box-shadow: 0 2px 4px rgba(0,0,0,0.5);
height: 60px;
box-sizing: border-box;
}
.logo {
font-weight: bold;
}
.tools {
position: relative;
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
button {
background-color: #444;
color: white;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #555;
}
#map-container {
position: relative; /* needed for overlay positioning */
width: 100vw;
height: calc(100vh - 60px);
overflow: hidden;
}
svg {
position: absolute; /* sit under notes overlay */
width: 100%;
height: 100%;
z-index: 1;
}
#autocomplete-list {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: white;
list-style: none;
margin: 0;
padding: 0;
max-height: 200px;
overflow-y: auto;
z-index: 1000;
border: none; /* remove outer border */
box-shadow: 0 2px 6px rgba(0,0,0,0.2); /* optional: soft shadow */
}
#autocomplete-list li {
padding: 8px;
cursor: pointer;
background: #fff;
color: #111;
border-bottom: none; /* remove lines between items */
}
#autocomplete-list li:hover {
background-color: #f0f0f0;
}
.chat-box {
display: flex;
align-items: center;
gap: 6px;
margin-left: 12px;
}
#chat-input {
padding: 6px 10px;
border-radius: 6px;
border: 1px solid #ccc;
font-size: 14px;
width: 200px;
}
#chat-send {
background-color: #444;
border: none;
color: white;
padding: 8px 12px;
border-radius: 4px;
font-size: 14px;
transition: background 0.3s;
}
#chat-send:hover {
background-color: #555; /* match other buttons */
}
/* Notes overlay sits above the SVG, but allows dragging */
#notes-overlay {
position: absolute;
inset: 0; /* top:0; right:0; bottom:0; left:0 */
z-index: 5;
pointer-events: none; /* so map stays interactive below */
}
.note {
padding: 8px 12px;
border: 0px solid #ccc;
border-radius: 6px;
cursor: move;
position: absolute;
font-size: 14px;
max-width: 860px;
pointer-events: auto;
user-select: none;
/* remove box-shadow */
}
.note .close {
position: absolute;
top: 4px;
right: 6px;
font-weight: bold;
cursor: pointer;
opacity: 0; /* initially hidden */
transition: opacity 0.3s ease;
pointer-events: none; /* don’t block dragging */
}
/* Show the close button after hovering for 3 seconds */
.note:hover .close {
pointer-events: auto;
animation: showClose 0s forwards 1.5s; /* delay 3s before showing */
}
@keyframes showClose {
to {
opacity: 1;
}
}
#country-search {
padding: 6px 10px; /* same padding as chat input */
border-radius: 6px; /* same rounding */
border: 1px solid #ccc; /* match chat input */
font-size: 14px; /* same font */
width: 200px; /* match chat input width */
box-sizing: border-box; /* ensures padding doesn’t make it larger */
}
</style>
</head>
<body>
<nav>
<div class="logo">World Map Illustrator</div>
<div class="tools">
<input type="color" id="colorPicker" value="#ff0000" />
<button id="refresh-map">Refresh Map</button>
<div style="position: relative;">
<input type="text" id="country-search" placeholder="Search country..." autocomplete="off" />
<ul id="autocomplete-list" style="position:absolute; top:100%; left:0; right:0; background:white; list-style:none; margin:0; padding:0; max-height:200px; overflow-y:auto; z-index:1000;"></ul>
</div>
<!-- Chat Box -->
<div class="chat-box">
<input type="text" id="chat-input" placeholder="Add a fact…" />
<button id="chat-send">Add</button>
</div>
<button id="download-svg">Download SVG</button>
</div>
</nav>
<div id="map-container">
<div id="notes-overlay"></div>
</div>
<script>
const width = 1000;
const height = 600;
// Build SVG map
const svg = d3.select("#map-container")
.append("svg")
.attr("width", width)
.attr("height", height);
const mapGroup = svg.append("g");
const projection = d3.geoNaturalEarth1()
.scale(160)
.translate([width / 2, height / 2]);
const path = d3.geoPath().projection(projection);
const colorPicker = document.getElementById("colorPicker");
let selectedColor = colorPicker.value;
colorPicker.addEventListener("input", () => {
selectedColor = colorPicker.value;
});
const tooltip = d3.select("body")
.append("div")
.attr("id", "tooltip")
.style("position", "absolute")
.style("padding", "6px 10px")
.style("background", "rgba(0, 0, 0, 0.4)") // semi-transparent black
.style("color", "#fff")
.style("border-radius", "4px")
.style("pointer-events", "none")
.style("font-size", "14px")
.style("z-index", "9999") // ensure it's above the map
.style("box-shadow", "0 2px 6px rgba(0,0,0,0.3)") // optional for depth
.style("display", "none");
let hoverTimeout;
const countryInput = document.getElementById("country-search");
const autocompleteList = document.getElementById("autocomplete-list");
const countryPaths = {};
const countryNames = [];
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json").then(worldData => {
const countries = topojson.feature(worldData, worldData.objects.countries).features;
mapGroup.selectAll("path")
.data(countries)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "#2c3e50")
.attr("stroke", "#ffffff")
.attr("stroke-width", 0.5)
.attr("data-name", d => d.properties.name || "")
.each(function (d) {
const name = d.properties.name?.trim();
if (name) {
const key = name.toLowerCase();
countryPaths[key] = this;
countryNames.push(name);
}
})
.on("click", function () {
d3.select(this).attr("fill", selectedColor);
})
.on("click", function () {
d3.select(this).attr("fill", selectedColor);
})
.on("mouseover", function (event, d) {
const name = d.properties.name || "";
hoverTimeout = setTimeout(() => {
tooltip
.style("left", `${event.pageX + 10}px`)
.style("top", `${event.pageY + 10}px`)
.style("display", "block")
.html(name);
}, 2000); // Show after 2 seconds
})
.on("mousemove", function (event) {
tooltip
.style("left", `${event.pageX + 10}px`)
.style("top", `${event.pageY + 10}px`);
})
.on("mouseout", function () {
clearTimeout(hoverTimeout);
tooltip.style("display", "none");
});
// Autocomplete logic
countryInput.addEventListener("input", function () {
const query = this.value.toLowerCase();
autocompleteList.innerHTML = "";
if (!query) return;
const filtered = countryNames.filter(name =>
name.toLowerCase().includes(query)
);
filtered.forEach(name => {
const item = document.createElement("li");
item.textContent = name;
item.addEventListener("click", () => {
countryInput.value = name;
autocompleteList.innerHTML = "";
const key = name.toLowerCase();
if (countryPaths[key]) {
d3.select(countryPaths[key]).attr("fill", selectedColor);
}
});
autocompleteList.appendChild(item);
});
});
// On Enter key
countryInput.addEventListener("keydown", function (e) {
if (e.key === "Enter") {
const name = countryInput.value.trim().toLowerCase();
autocompleteList.innerHTML = "";
if (countryPaths[name]) {
d3.select(countryPaths[name]).attr("fill", selectedColor);
}
}
});
// Close suggestions when clicking outside
document.addEventListener("click", (e) => {
if (!countryInput.contains(e.target) && !autocompleteList.contains(e.target)) {
autocompleteList.innerHTML = "";
}
});
});
// Zoom behavior
const zoom = d3.zoom()
.scaleExtent([1, 8])
.on("zoom", (event) => {
mapGroup.attr("transform", event.transform);
});
svg.call(zoom);
// Refresh Button (no reload)
document.getElementById("refresh-map").addEventListener("click", () => {
d3.selectAll("path").attr("fill", "#2c3e50");
countryInput.value = "";
autocompleteList.innerHTML = "";
d3.select("#tooltip").style("display", "none");
svg.transition().duration(500).call(zoom.transform, d3.zoomIdentity);
});
// Download the rendered SVG
document.getElementById("download-svg").addEventListener("click", () => {
const svgNode = document.querySelector("#map-container svg"); // ensure correct SVG
const clone = svgNode.cloneNode(true);
clone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
const svgData = new XMLSerializer().serializeToString(clone);
const blob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = "highlighted-world-map.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
const notesOverlay = document.getElementById("notes-overlay");
// Track last mouse position over the map container
let lastMouse = { x: 120, y: 120 };
document.getElementById("map-container").addEventListener("mousemove", (e) => {
const rect = e.currentTarget.getBoundingClientRect();
lastMouse.x = e.clientX - rect.left;
lastMouse.y = e.clientY - rect.top;
});
// Single handler: create a draggable note at last mouse position
document.getElementById("chat-send").addEventListener("click", () => {
const input = document.getElementById("chat-input");
const message = input.value.trim();
if (!message) return;
createNote(message, lastMouse.x, lastMouse.y);
input.value = "";
});
document.getElementById("chat-input").addEventListener("keydown", (e) => {
if (e.key === "Enter") {
document.getElementById("chat-send").click();
}
});
function createNote(text, x, y) {
const note = document.createElement("div");
note.className = "note";
note.style.left = x + "px";
note.style.top = y + "px";
note.innerHTML = `
<span class="close" title="Delete">×</span>
<div class="content" contenteditable="true"></div>
`;
const contentDiv = note.querySelector(".content");
contentDiv.textContent = text;
// Default font and size
contentDiv.style.fontFamily = "Arial, sans-serif";
contentDiv.style.fontSize = "36px";
contentDiv.style.color = "#1f2b38"; // optional default color
contentDiv.style.fontWeight = "bold";
notesOverlay.appendChild(note);
makeDraggable(note);
// delete button
note.querySelector(".close").addEventListener("click", () => {
notesOverlay.removeChild(note);
});
}
function makeDraggable(element) {
let offsetX = 0, offsetY = 0, isDown = false;
element.addEventListener("mousedown", (e) => {
if ((e.target).classList.contains("close")) return; // allow delete without drag
isDown = true;
element.style.zIndex = 1000; // top while dragging
const rect = element.getBoundingClientRect();
const parentRect = notesOverlay.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
// Move with mouse
const onMove = (ev) => {
if (!isDown) return;
let left = ev.clientX - parentRect.left - offsetX;
let top = ev.clientY - parentRect.top - offsetY;
// clamp inside overlay
left = Math.max(0, Math.min(left, parentRect.width - rect.width));
top = Math.max(0, Math.min(top, parentRect.height - rect.height));
element.style.left = left + "px";
element.style.top = top + "px";
};
const onUp = () => {
isDown = false;
element.style.zIndex = 10;
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
};
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
});
}
//mouseover hover event handler//
</script>
</body>
</html>