|
| 1 | +<template> |
| 2 | + <div id="map-page"> |
| 3 | + <div id="map"></div> |
| 4 | + <div id="steps"> |
| 5 | + <div class="step"></div> |
| 6 | + <div class="step"></div> |
| 7 | + </div> |
| 8 | + </div> |
| 9 | +</template> |
| 10 | + |
| 11 | +<script setup> |
| 12 | + import { onMounted, onUnmounted } from 'vue' |
| 13 | + import maplibregl from 'maplibre-gl' |
| 14 | + import scrollama from 'scrollama' |
| 15 | + |
| 16 | + onMounted(() => { |
| 17 | + const locParliament = [19.04556, 47.50704] |
| 18 | + const locCastle = [19.03945, 47.49621] |
| 19 | + const zoomStart = 15 |
| 20 | + const zoomEnd = 17 |
| 21 | + |
| 22 | + const map = new maplibregl.Map({ |
| 23 | + container: 'map', |
| 24 | + style: 'https://tiles.openfreemap.org/styles/liberty', |
| 25 | + center: locParliament, |
| 26 | + zoom: zoomStart |
| 27 | + }) |
| 28 | + |
| 29 | + // Disable map interactions in order not to interfere with scrollytelling |
| 30 | + map.scrollZoom.disable() |
| 31 | + map.boxZoom.disable() |
| 32 | + map.dragRotate.disable() |
| 33 | + map.dragPan.disable() |
| 34 | + map.keyboard.disable() |
| 35 | + map.doubleClickZoom.disable() |
| 36 | + map.touchZoomRotate.disable() |
| 37 | + |
| 38 | + map.once('idle', () => { |
| 39 | + map.setCenter(locParliament) |
| 40 | + map.setZoom(zoomStart) |
| 41 | + |
| 42 | + const card1 = document.createElement('div') |
| 43 | + card1.className = 'marker-card' |
| 44 | + card1.innerHTML = '<img src="" alt="Parliament"><p>Hungarian Parliament</p>' |
| 45 | + |
| 46 | + const card2 = document.createElement('div') |
| 47 | + card2.className = 'marker-card' |
| 48 | + card2.innerHTML = '<img src="" alt="Castle"><p>Buda Castle</p>' |
| 49 | + |
| 50 | + const marker1 = new maplibregl.Marker(card1, { offset: [0, -75] }) |
| 51 | + .setLngLat(locParliament) |
| 52 | + .addTo(map) |
| 53 | + const marker2 = new maplibregl.Marker(card2, { offset: [0, -75] }) |
| 54 | + .setLngLat(locCastle) |
| 55 | + .addTo(map) |
| 56 | + |
| 57 | + card2.style.opacity = 0 |
| 58 | + |
| 59 | + const scroller = scrollama() |
| 60 | + scroller |
| 61 | + .setup({ step: '.step', offset: 0.5, progress: true }) |
| 62 | + .onStepProgress(resp => { |
| 63 | + if (resp.index === 0) { |
| 64 | + const t = Math.max(0, Math.min(1, resp.progress)) |
| 65 | + const lng = locParliament[0] + (locCastle[0] - locParliament[0]) * t |
| 66 | + const lat = locParliament[1] + (locCastle[1] - locParliament[1]) * t |
| 67 | + const zoom = zoomStart + (zoomEnd - zoomStart) * t |
| 68 | + |
| 69 | + map.easeTo({ |
| 70 | + center: [lng, lat], |
| 71 | + zoom, |
| 72 | + duration: 100, |
| 73 | + easing: n => n |
| 74 | + }) |
| 75 | + |
| 76 | + card1.style.opacity = 1 - t |
| 77 | + card2.style.opacity = t |
| 78 | + } |
| 79 | + }) |
| 80 | + .onStepEnter(resp => { |
| 81 | + if (resp.index === 1) { |
| 82 | + map.easeTo({ |
| 83 | + center: locCastle, |
| 84 | + zoom: zoomEnd, |
| 85 | + duration: 800, |
| 86 | + easing: n => n |
| 87 | + }) |
| 88 | + card1.style.opacity = 0 |
| 89 | + card2.style.opacity = 1 |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + window.addEventListener('resize', scroller.resize) |
| 94 | + }) |
| 95 | + |
| 96 | + onUnmounted(() => { |
| 97 | + map.remove() |
| 98 | + }) |
| 99 | + }) |
| 100 | +</script> |
| 101 | + |
| 102 | +<style> |
| 103 | + #map-page { |
| 104 | + margin: 0; |
| 105 | + padding: 0; |
| 106 | + } |
| 107 | + #map { |
| 108 | + position: fixed; |
| 109 | + inset: 0; |
| 110 | + width: 100%; |
| 111 | + height: 100%; |
| 112 | + z-index: 0; |
| 113 | + } |
| 114 | + #steps { |
| 115 | + position: relative; |
| 116 | + z-index: 1; |
| 117 | + } |
| 118 | + .step { |
| 119 | + height: 100vh; |
| 120 | + } |
| 121 | + .marker-card { |
| 122 | + background: white; |
| 123 | + border: 1px solid #666; |
| 124 | + border-radius: 4px; |
| 125 | + padding: 8px; |
| 126 | + font-family: sans-serif; |
| 127 | + font-size: 14px; |
| 128 | + max-width: 200px; |
| 129 | + text-align: center; |
| 130 | + box-shadow: 0 2px 6px rgba(0,0,0,0.2); |
| 131 | + transition: opacity 0.4s ease; |
| 132 | + } |
| 133 | + .marker-card img { |
| 134 | + width: 100%; |
| 135 | + height: auto; |
| 136 | + display: block; |
| 137 | + margin-bottom: 5px; |
| 138 | + } |
| 139 | +</style> |
0 commit comments