|
5 | 5 | import { feature } from 'topojson-client'; |
6 | 6 | import { worldAtlasIdToIso3 } from '../data/worldIso3.js'; |
7 | 7 | import { filters, setSelectedCountry } from '../stores/filters.js'; |
8 | | -
|
9 | | - let { countryByDecade = [], step = 0 } = $props(); |
| 8 | + import { |
| 9 | + closestRotationStart, |
| 10 | + easeInOutCubic, |
| 11 | + buildIsoCounts, |
| 12 | + globeFillFor, |
| 13 | + IDLE_DEGREES_PER_MS, |
| 14 | + IDLE_RESUME_DELAY, |
| 15 | + TOP_THREE_HOLD_DELAY, |
| 16 | + TOP_THREE_ROTATION, |
| 17 | + } from '../utils/globeMotion.js'; |
| 18 | + import './globe.css'; |
| 19 | +
|
| 20 | + let { countryByDecade = [], step = 0, active = false } = $props(); |
10 | 21 |
|
11 | 22 | const width = 760; |
12 | 23 | const height = 760; |
13 | | - const topThree = new Set(['USA', 'FRA', 'DEU']); |
14 | 24 |
|
15 | 25 | let countries = $state([]); |
16 | 26 | let rotation = $state([-35, -20]); |
17 | 27 | let dragging = $state(false); |
| 28 | + let scripted = $state(false); |
18 | 29 | let dragStart = null; |
19 | 30 | let rotateStart = null; |
20 | 31 | let pointerDownCountryIndex = null; |
21 | 32 | let pointerMoved = false; |
22 | | - let frame = 0; |
23 | | - let reduceMotion = false; |
| 33 | + let motionFrame = 0; |
| 34 | + let idleFrame = 0; |
| 35 | + let idleLast = 0; |
| 36 | + let idlePausedUntil = 0; |
| 37 | + let lastFocusedStep = -1; |
| 38 | + let reduceMotion = $state(false); |
24 | 39 |
|
25 | 40 | let projection = $derived( |
26 | 41 | geoOrthographic() |
|
33 | 48 | let graticulePath = $derived(path(geoGraticule10())); |
34 | 49 | let range = $derived($filters.decadeRange); |
35 | 50 | let selectedCountry = $derived($filters.selectedCountry); |
36 | | - let countByIso = $derived(buildCounts(countryByDecade, range)); |
| 51 | + let countByIso = $derived(buildIsoCounts(countryByDecade, range)); |
37 | 52 | let maxCount = $derived(Math.max(1, ...Object.values(countByIso))); |
38 | 53 |
|
39 | | - function buildCounts(rows, decadeRange) { |
40 | | - const counts = {}; |
41 | | - for (const row of rows) { |
42 | | - if (row.decade === null || row.decade < decadeRange[0] || row.decade > decadeRange[1]) continue; |
43 | | - counts[row.iso3] = (counts[row.iso3] || 0) + row.n; |
44 | | - } |
45 | | - return counts; |
46 | | - } |
47 | | -
|
48 | | - function fillFor(iso3) { |
49 | | - const n = countByIso[iso3] || 0; |
50 | | - if (n === 0) return 'transparent'; |
51 | | - if (step >= 2 && topThree.has(iso3)) return 'var(--accent-primary)'; |
52 | | - if (step === 0) return 'var(--seq-0)'; |
53 | | - const bucket = Math.min(4, Math.max(0, Math.floor((Math.log10(n) / Math.log10(maxCount)) * 4))); |
54 | | - return `var(--seq-${bucket})`; |
55 | | - } |
56 | | -
|
57 | 54 | function showTooltip(event, country) { |
58 | 55 | const iso3 = worldAtlasIdToIso3[String(country.id).padStart(3, '0')]; |
59 | 56 | const count = countByIso[iso3] || 0; |
|
73 | 70 | window.dispatchEvent(new CustomEvent('darts:tooltip-hide')); |
74 | 71 | } |
75 | 72 |
|
76 | | - function ease(t) { |
77 | | - return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; |
| 73 | + function pauseIdle(duration = IDLE_RESUME_DELAY) { |
| 74 | + idlePausedUntil = performance.now() + duration; |
| 75 | + idleLast = 0; |
78 | 76 | } |
79 | 77 |
|
80 | | - function centerCountry(country) { |
81 | | - const iso3 = worldAtlasIdToIso3[String(country.id).padStart(3, '0')]; |
82 | | - if (!iso3 || !countByIso[iso3]) return; |
83 | | - setSelectedCountry(iso3); |
84 | | - const [lon, lat] = geoCentroid(country); |
85 | | - const target = [-lon, -lat]; |
| 78 | + function cancelMotion() { |
| 79 | + cancelAnimationFrame(motionFrame); |
| 80 | + motionFrame = 0; |
| 81 | + scripted = false; |
| 82 | + } |
| 83 | +
|
| 84 | + function animateRotationTo(target, duration = 900, holdDelay = 0) { |
| 85 | + pauseIdle(duration + holdDelay); |
| 86 | + cancelMotion(); |
86 | 87 | if (reduceMotion) { |
87 | 88 | rotation = target; |
88 | 89 | return; |
89 | 90 | } |
90 | | - const start = rotation; |
| 91 | + scripted = true; |
| 92 | + const start = closestRotationStart(rotation, target); |
91 | 93 | const interp = interpolate(start, target); |
92 | 94 | const started = performance.now(); |
93 | | - cancelAnimationFrame(frame); |
94 | 95 | const tick = (now) => { |
95 | | - const t = Math.min(1, (now - started) / 900); |
96 | | - rotation = interp(ease(t)); |
97 | | - if (t < 1) frame = requestAnimationFrame(tick); |
| 96 | + const t = Math.min(1, (now - started) / duration); |
| 97 | + rotation = interp(easeInOutCubic(t)); |
| 98 | + if (t < 1) { |
| 99 | + motionFrame = requestAnimationFrame(tick); |
| 100 | + } else { |
| 101 | + rotation = target; |
| 102 | + scripted = false; |
| 103 | + motionFrame = 0; |
| 104 | + } |
98 | 105 | }; |
99 | | - frame = requestAnimationFrame(tick); |
| 106 | + motionFrame = requestAnimationFrame(tick); |
| 107 | + } |
| 108 | +
|
| 109 | + function startIdleLoop() { |
| 110 | + if (idleFrame || reduceMotion) return; |
| 111 | + idleFrame = requestAnimationFrame(idleTick); |
| 112 | + } |
| 113 | +
|
| 114 | + function stopIdleLoop() { |
| 115 | + cancelAnimationFrame(idleFrame); |
| 116 | + idleFrame = 0; |
| 117 | + idleLast = 0; |
| 118 | + } |
| 119 | +
|
| 120 | + function idleTick(now) { |
| 121 | + idleFrame = requestAnimationFrame(idleTick); |
| 122 | + if (!active || reduceMotion) return; |
| 123 | + const elapsed = idleLast ? Math.min(40, now - idleLast) : 0; |
| 124 | + idleLast = now; |
| 125 | + if (!elapsed || dragging || scripted || selectedCountry || now < idlePausedUntil) return; |
| 126 | + rotation = [rotation[0] + elapsed * IDLE_DEGREES_PER_MS, rotation[1]]; |
| 127 | + } |
| 128 | +
|
| 129 | + function focusTopThreeCountries() { |
| 130 | + animateRotationTo(TOP_THREE_ROTATION, 900, TOP_THREE_HOLD_DELAY); |
| 131 | + } |
| 132 | +
|
| 133 | + function centerCountry(country) { |
| 134 | + const iso3 = worldAtlasIdToIso3[String(country.id).padStart(3, '0')]; |
| 135 | + if (!iso3 || !countByIso[iso3]) return; |
| 136 | + setSelectedCountry(iso3); |
| 137 | + const [lon, lat] = geoCentroid(country); |
| 138 | + const target = [-lon, -lat]; |
| 139 | + animateRotationTo(target, 900, IDLE_RESUME_DELAY); |
100 | 140 | } |
101 | 141 |
|
102 | 142 | function onPointerDown(event) { |
| 143 | + cancelMotion(); |
| 144 | + pauseIdle(); |
103 | 145 | dragging = true; |
104 | 146 | dragStart = [event.clientX, event.clientY]; |
105 | 147 | rotateStart = rotation; |
|
123 | 165 | pointerDownCountryIndex = null; |
124 | 166 | if (event.currentTarget.hasPointerCapture?.(event.pointerId)) |
125 | 167 | event.currentTarget.releasePointerCapture(event.pointerId); |
| 168 | + pauseIdle(); |
126 | 169 | if (shouldSelect) centerCountry(countries[Number(countryIndex)]); |
127 | 170 | } |
128 | 171 |
|
| 172 | + $effect(() => { |
| 173 | + if (active && !reduceMotion) { |
| 174 | + startIdleLoop(); |
| 175 | + } else { |
| 176 | + stopIdleLoop(); |
| 177 | + } |
| 178 | + }); |
| 179 | +
|
| 180 | + $effect(() => { |
| 181 | + if (active && step === 2 && lastFocusedStep !== 2 && !selectedCountry) focusTopThreeCountries(); |
| 182 | + lastFocusedStep = step; |
| 183 | + }); |
| 184 | +
|
129 | 185 | onMount(async () => { |
130 | 186 | reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
131 | 187 | const response = await fetch('./topology/world-110m.json'); |
132 | 188 | const topology = await response.json(); |
133 | 189 | countries = feature(topology, topology.objects.countries).features; |
134 | 190 | }); |
135 | 191 |
|
136 | | - onDestroy(() => cancelAnimationFrame(frame)); |
| 192 | + onDestroy(() => { |
| 193 | + cancelMotion(); |
| 194 | + stopIdleLoop(); |
| 195 | + }); |
137 | 196 | </script> |
138 | 197 |
|
139 | 198 | <figure class="globe-figure" aria-label="Orthographic globe showing credited works by artist country"> |
|
167 | 226 | d={path(country)} |
168 | 227 | data-country-index={index} |
169 | 228 | data-iso3={iso3} |
170 | | - fill={fillFor(iso3)} |
| 229 | + fill={globeFillFor(iso3, countByIso[iso3] || 0, maxCount, step)} |
171 | 230 | class:selected={selectedCountry === iso3} |
172 | 231 | class:clickable={Boolean(iso3 && countByIso[iso3])} |
173 | 232 | onpointermove={(event) => showTooltip(event, country)} |
|
187 | 246 | </g> |
188 | 247 | </svg> |
189 | 248 | </figure> |
190 | | -
|
191 | | -<style> |
192 | | - .globe-figure { |
193 | | - margin: 0; |
194 | | - width: min(90vh, 100%); |
195 | | - } |
196 | | -
|
197 | | - svg { |
198 | | - display: block; |
199 | | - width: 100%; |
200 | | - height: auto; |
201 | | - } |
202 | | -
|
203 | | - .sphere { |
204 | | - fill: var(--bg-dark); |
205 | | - stroke: var(--fg-on-dark-mute); |
206 | | - stroke-width: 0.5; |
207 | | - opacity: 0.9; |
208 | | - } |
209 | | -
|
210 | | - .graticule { |
211 | | - fill: none; |
212 | | - stroke: var(--fg-on-dark-mute); |
213 | | - stroke-width: 0.35; |
214 | | - opacity: 0.35; |
215 | | - pointer-events: none; |
216 | | - } |
217 | | -
|
218 | | - .countries { |
219 | | - cursor: grab; |
220 | | - } |
221 | | -
|
222 | | - .countries.dragging { |
223 | | - cursor: grabbing; |
224 | | - } |
225 | | -
|
226 | | - path { |
227 | | - stroke: var(--bg-dark); |
228 | | - stroke-width: 0.35; |
229 | | - transition: |
230 | | - fill 450ms var(--ease-inout), |
231 | | - stroke 180ms var(--ease-out); |
232 | | - } |
233 | | -
|
234 | | - path.clickable { |
235 | | - cursor: pointer; |
236 | | - } |
237 | | -
|
238 | | - path.clickable:hover, |
239 | | - path.selected { |
240 | | - stroke: var(--fg-on-dark-strong); |
241 | | - stroke-width: 1.2; |
242 | | - } |
243 | | -
|
244 | | - @media (prefers-reduced-motion: reduce) { |
245 | | - path { |
246 | | - transition: none; |
247 | | - } |
248 | | - } |
249 | | -</style> |
0 commit comments