|
| 1 | +/* ========================= |
| 2 | + MEDIA |
| 3 | +========================= */ |
| 4 | +const overlay = document.getElementById("overlay"); |
| 5 | +const audio = document.getElementById("background-audio"); |
| 6 | +const volumeIcon = document.getElementById("volume-icon"); |
| 7 | +const volumeSlider = document.getElementById("volume"); |
| 8 | + |
| 9 | +function startMedia() { |
| 10 | + overlay.classList.add("hidden"); |
| 11 | + audio?.play?.(); |
| 12 | +} |
| 13 | +function setVolume(v) { |
| 14 | + v = Math.max(0, Math.min(1, Number(v))); |
| 15 | + if (audio) audio.volume = v; |
| 16 | + if (volumeIcon) |
| 17 | + volumeIcon.src = v > 0 ? "assets/vol_on.png" : "assets/vol_off.png"; |
| 18 | +} |
| 19 | +function toggleMute() { |
| 20 | + setVolume((audio?.volume || 0) > 0 ? 0 : 1); |
| 21 | + if (volumeSlider) volumeSlider.value = audio?.volume || 0; |
| 22 | +} |
| 23 | +window.addEventListener("load", () => { |
| 24 | + const v = Number(localStorage.getItem("vol") || 0.5); |
| 25 | + setVolume(v); |
| 26 | + if (volumeSlider) volumeSlider.value = v; |
| 27 | +}); |
| 28 | +if (volumeSlider) |
| 29 | + volumeSlider.addEventListener("input", (e) => { |
| 30 | + setVolume(e.target.value); |
| 31 | + localStorage.setItem("vol", e.target.value); |
| 32 | + }); |
| 33 | + |
| 34 | +// Gestion du volume sur mobile |
| 35 | +const volumeControl = document.querySelector('.volume-control'); |
| 36 | +if (volumeControl && 'ontouchstart' in window) { |
| 37 | + volumeControl.addEventListener('click', (e) => { |
| 38 | + if (e.target === volumeControl || e.target === volumeIcon) { |
| 39 | + volumeControl.classList.toggle('active'); |
| 40 | + } |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/* ========================= |
| 45 | + MAP / ZOOM |
| 46 | +========================= */ |
| 47 | +const svg = document.getElementById("got-map"); |
| 48 | +const panzoom = document.getElementById("panzoom"); |
| 49 | +const backBtn = document.getElementById("back-btn"); |
| 50 | + |
| 51 | +let activeRegion = null; |
| 52 | + |
| 53 | +function computeZoomToBBox(bbox, factor = 0.82) { |
| 54 | + const viewW = svg.clientWidth; |
| 55 | + const viewH = svg.clientHeight; |
| 56 | + const scale = Math.min( |
| 57 | + (viewW * factor) / bbox.width, |
| 58 | + (viewH * factor) / bbox.height |
| 59 | + ); |
| 60 | + const cx = bbox.x + bbox.width / 2; |
| 61 | + const cy = bbox.y + bbox.height / 2; |
| 62 | + const tx = viewW / 2 - cx * scale; |
| 63 | + const ty = viewH / 2 - cy * scale; |
| 64 | + return { scale, tx, ty }; |
| 65 | +} |
| 66 | + |
| 67 | +function zoomToRegion(regionEl) { |
| 68 | + const bbox = regionEl.getBBox(); |
| 69 | + const { scale, tx, ty } = computeZoomToBBox(bbox); |
| 70 | + panzoom.style.transform = `translate(${tx}px, ${ty}px) scale(${scale})`; |
| 71 | + setTimeout(() => { |
| 72 | + regionEl.classList.add("active"); |
| 73 | + activeRegion = regionEl; |
| 74 | + backBtn.classList.add("show"); |
| 75 | + }, 650); |
| 76 | +} |
| 77 | + |
| 78 | +function resetZoom() { |
| 79 | + panzoom.style.transform = `translate(0px, 0px) scale(1)`; |
| 80 | + if (activeRegion) activeRegion.classList.remove("active"); |
| 81 | + activeRegion = null; |
| 82 | + backBtn.classList.remove("show"); |
| 83 | + closeInfoPanel(); |
| 84 | +} |
| 85 | + |
| 86 | +const infoPanel = document.getElementById("info-panel"); |
| 87 | +const panelTitle = document.getElementById("panel-title"); |
| 88 | +const panelClose = document.getElementById("panel-close"); |
| 89 | +const aboutContent = document.getElementById("about-content"); |
| 90 | +const contactContent = document.getElementById("contact-content"); |
| 91 | + |
| 92 | +function openInfoPanel(regionKey, title) { |
| 93 | + panelTitle.textContent = title; |
| 94 | + |
| 95 | + aboutContent.style.display = "none"; |
| 96 | + contactContent.style.display = "none"; |
| 97 | + |
| 98 | + if (regionKey === "about") { |
| 99 | + aboutContent.style.display = "block"; |
| 100 | + } else if (regionKey === "contact") { |
| 101 | + contactContent.style.display = "block"; |
| 102 | + } |
| 103 | + |
| 104 | + infoPanel.classList.add("open"); |
| 105 | +} |
| 106 | + |
| 107 | +function closeInfoPanel() { |
| 108 | + infoPanel.classList.remove("open"); |
| 109 | +} |
| 110 | + |
| 111 | +panelClose.addEventListener("click", closeInfoPanel); |
| 112 | + |
| 113 | +document.querySelectorAll(".region").forEach((region) => { |
| 114 | + region.addEventListener("click", () => { |
| 115 | + if (activeRegion === region) return; |
| 116 | + if (activeRegion) { |
| 117 | + activeRegion.classList.remove("active"); |
| 118 | + } |
| 119 | + |
| 120 | + closeInfoPanel(); |
| 121 | + closeModal(); |
| 122 | + |
| 123 | + zoomToRegion(region); |
| 124 | + |
| 125 | + if (region.classList.contains("panel-region")) { |
| 126 | + setTimeout(() => { |
| 127 | + openInfoPanel(region.dataset.key, region.dataset.title); |
| 128 | + }, 700); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +const modal = document.getElementById("modal"); |
| 134 | +const modalTitle = document.getElementById("modal-title"); |
| 135 | +const modalBody = document.getElementById("modal-body"); |
| 136 | +const modalClose = document.getElementById("modal-close"); |
| 137 | + |
| 138 | +function openModal(title, content) { |
| 139 | + modalTitle.textContent = title; |
| 140 | + modalBody.innerHTML = content; |
| 141 | + modal.classList.add("open"); |
| 142 | +} |
| 143 | +function closeModal() { |
| 144 | + modal.classList.remove("open"); |
| 145 | +} |
| 146 | + |
| 147 | +document.querySelectorAll(".city").forEach((city) => { |
| 148 | + city.addEventListener("click", (e) => { |
| 149 | + e.stopPropagation(); |
| 150 | + if (!activeRegion || activeRegion.dataset.key !== "projects") return; |
| 151 | + openModal(city.dataset.title, city.dataset.content); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +modalClose.addEventListener("click", closeModal); |
| 156 | + |
| 157 | +backBtn.addEventListener("click", () => { |
| 158 | + closeModal(); |
| 159 | + resetZoom(); |
| 160 | +}); |
| 161 | +window.addEventListener("keydown", (e) => { |
| 162 | + if (e.key === "Escape") { |
| 163 | + closeModal(); |
| 164 | + resetZoom(); |
| 165 | + } |
| 166 | +}); |
| 167 | +window.addEventListener("resize", () => { |
| 168 | + if (activeRegion) { |
| 169 | + const b = activeRegion.getBBox(); |
| 170 | + const { scale, tx, ty } = computeZoomToBBox(b); |
| 171 | + panzoom.style.transform = `translate(${tx}px, ${ty}px) scale(${scale})`; |
| 172 | + } |
| 173 | +}); |
0 commit comments