From aab8363c16a2173769fa2303acf8cf8dd9f6f4b7 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Thu, 19 Nov 2026 13:15:55 +0800 Subject: [PATCH 01/12] Fix versor drag API usage and slerp rotate-to-country --- src/components/MembersGlobe.astro | 48 +++++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/components/MembersGlobe.astro b/src/components/MembersGlobe.astro index 8d79410..d56dbc5 100644 --- a/src/components/MembersGlobe.astro +++ b/src/components/MembersGlobe.astro @@ -182,11 +182,11 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) } } - // --- versor drag with inertia ------------------------------------------- + // --- versor drag (Fil's canonical versor-dragging) with inertia ---------- let dragging = false - let v0: number[][] | null = null - let r0: [number, number, number] = [0, 0, 0] - let lastT = 0 + let q0: number[] | null = null // quaternion at drag start / last move + let v0: [number, number, number] | null = null // cartesian on sphere at last move + let lastMoveT = 0 let vel = 0 // degrees per second around λ let lastInteraction = 0 @@ -198,31 +198,37 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) svg.addEventListener('pointerdown', (e) => { dragging = true vel = 0 - lastT = performance.now() - lastInteraction = lastT + lastMoveT = performance.now() + lastInteraction = lastMoveT svg.setPointerCapture(e.pointerId) - v0 = versor(point(e)) as unknown as number[][] - r0 = [...rotation] as [number, number, number] + q0 = versor(rotation) as unknown as number[] + const spherical = projection.invert(point(e)) + v0 = spherical ? (versor.cartesian(spherical) as unknown as [number, number, number]) : null }) svg.addEventListener('pointermove', (e) => { - if (!dragging || !v0) return + if (!dragging || !q0 || !v0) return + const spherical = projection.invert(point(e)) + if (!spherical) return // pointer left the sphere; keep tracking + const v1 = versor.cartesian(spherical) as unknown as [number, number, number] + const q1 = versor.multiply(versor.delta(v0, v1), q0) as unknown as number[] + const r = versor.rotation(q1 as unknown as [number, number, number]) as [number, number, number] const now = performance.now() - const dt = Math.max(now - lastT, 1) - const v1 = versor(point(e)) as unknown as number[][] - const q = versor.delta(r0, v0, v1) - const r = versor.rotation(q) as [number, number, number] + const dt = Math.max(now - lastMoveT, 1) vel = ((r[0] - rotation[0]) / dt) * 1000 rotation[0] = r[0] rotation[1] = Math.max(-89, Math.min(89, r[1])) rotation[2] = 0 - lastT = now + v0 = v1 + q0 = q1 + lastMoveT = now lastInteraction = now setRotation() }) const endDrag = () => { dragging = false + q0 = null v0 = null lastInteraction = performance.now() } @@ -294,15 +300,17 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) then?.() return } + const interpolate = versor.interpolate(from, to) const t0 = performance.now() - const duration = 700 + const duration = 850 lastInteraction = t0 + duration animating = true const step = (t: number) => { const p = Math.min((t - t0) / duration, 1) const e = 1 - Math.pow(1 - p, 3) - rotation[0] = from[0] + (to[0] - from[0]) * e - rotation[1] = from[1] + (to[1] - from[1]) * e + const r = interpolate(e) as [number, number, number] + rotation[0] = r[0] + rotation[1] = Math.max(-89, Math.min(89, r[1])) setRotation() if (p < 1) requestAnimationFrame(step) else { @@ -337,11 +345,13 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) if (visible && loaded && !dragging && !animating) { if (Math.abs(vel) > 1) { rotation[0] += (vel * dt) / 1000 - vel *= Math.pow(0.94, dt / 16.7) + vel *= Math.pow(0.95, dt / 16.7) lastInteraction = t setRotation() } else if (!reducedMotion && t - lastInteraction > AUTO_RESUME_MS) { - rotation[0] += (AUTO_SPEED * dt) / 1000 + const since = t - lastInteraction - AUTO_RESUME_MS + const ramp = Math.min(since / 1200, 1) // ease auto-rotation in + rotation[0] += (AUTO_SPEED * ramp * dt) / 1000 setRotation() } } From e4cd10d503d17cbff2eafd363f3b02294dff156d Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Thu, 19 Nov 2026 13:26:22 +0800 Subject: [PATCH 02/12] Smooth globe interaction, Hong Kong membership with CN polygon highlight --- scripts/members-data.test.ts | 2 +- src/components/MembersGlobe.astro | 73 ++++++++++++++++++++++--------- src/data/members.yaml | 2 +- src/data/people.yaml | 2 +- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/scripts/members-data.test.ts b/scripts/members-data.test.ts index f93189f..46f33dd 100644 --- a/scripts/members-data.test.ts +++ b/scripts/members-data.test.ts @@ -10,7 +10,7 @@ countries.registerLocale(en) const read = (p: string) => loadYaml(readFileSync(resolve(__dirname, '..', p), 'utf-8')) const ORG_CATEGORIES = ['founding', 'corporate', 'small', 'nonprofit', 'university'] -const COUNTRIES = ['United States', 'France', 'Norway', 'Japan', 'United Kingdom', 'Austria'] +const COUNTRIES = ['United States', 'France', 'Norway', 'Japan', 'United Kingdom', 'Austria', 'Hong Kong'] describe('members data', () => { const members = read('src/data/members.yaml') as any[] diff --git a/src/components/MembersGlobe.astro b/src/components/MembersGlobe.astro index d56dbc5..464967e 100644 --- a/src/components/MembersGlobe.astro +++ b/src/components/MembersGlobe.astro @@ -32,13 +32,20 @@ const projection = geoOrthographic() ) const pathGen = geoPath(projection) +// Hong Kong has no polygon in Natural Earth 110m — it is part of the CN +// feature. Highlight the containing polygon alongside the member country. +const FEATURE_FALLBACKS: Record = { '344': '156' } const memberById = new Map(stats.map((s) => [s.id, s])) +// reverse: feature id -> stat for member countries without their own polygon +const memberByFeatureFallback = new Map( + Object.entries(FEATURE_FALLBACKS).map(([memberId, featureId]) => [featureId, memberById.get(memberId)]), +) const countryPaths = worldShapes.features.map((f) => { const id = String(f.id).padStart(3, '0') return { id, d: pathGen(f), - stat: memberById.get(id), + stat: memberById.get(id) ?? memberByFeatureFallback.get(id), } }) const graticuleD = pathGen(geoGraticule10()) @@ -61,7 +68,7 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) label(s)).join(', ')}`} @@ -173,22 +180,27 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) }) const rotation: [number, number, number] = [...initial, 0] - const setRotation = () => { + const renderFrame = () => { projection.rotate(rotation) - if (!loaded) return graticuleEl.setAttribute('d', pathGen(graticuleObj) ?? '') for (const [id, el] of paths) { el.setAttribute('d', pathGen(featuresById.get(id) as object) ?? '') } } + const setRotation = () => { + dirty = true + if (loaded) renderFrame() + } - // --- versor drag (Fil's canonical versor-dragging) with inertia ---------- + // --- versor drag (Fil's canonical versor-dragging), rAF-coalesced -------- + // Math runs per pointermove; DOM writes happen once per frame in the loop. let dragging = false let q0: number[] | null = null // quaternion at drag start / last move let v0: [number, number, number] | null = null // cartesian on sphere at last move let lastMoveT = 0 - let vel = 0 // degrees per second around λ + let vel = 0 // smoothed degrees per second around λ let lastInteraction = 0 + let dirty = false const point = (e: PointerEvent): [number, number] => { const rect = svg.getBoundingClientRect() @@ -215,7 +227,8 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) const r = versor.rotation(q1 as unknown as [number, number, number]) as [number, number, number] const now = performance.now() const dt = Math.max(now - lastMoveT, 1) - vel = ((r[0] - rotation[0]) / dt) * 1000 + const instant = ((r[0] - rotation[0]) / dt) * 1000 + vel = Math.abs(instant) > 4000 ? instant : vel * 0.6 + instant * 0.4 // low-pass; bypass on teleports rotation[0] = r[0] rotation[1] = Math.max(-89, Math.min(89, r[1])) rotation[2] = 0 @@ -223,7 +236,7 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) q0 = q1 lastMoveT = now lastInteraction = now - setRotation() + dirty = true }) const endDrag = () => { @@ -235,6 +248,14 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) svg.addEventListener('pointerup', endDrag) svg.addEventListener('pointercancel', endDrag) + // --- hover pauses auto-rotation so the globe never spins under the cursor + let hovering = false + svg.addEventListener('pointerenter', () => (hovering = true)) + svg.addEventListener('pointerleave', () => { + hovering = false + lastInteraction = performance.now() + }) + // --- tooltip ------------------------------------------------------------- const tooltip = section.querySelector('[data-globe-tooltip]')! const tooltipTitle = tooltip.querySelector('[data-globe-tooltip-title]')! @@ -286,7 +307,7 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) let animating = false const rotateTo = (name: string, then?: () => void) => { const stat = stats.find((s) => s.name === name) - const c = stat && centroids.get(stat.id) + const c = stat && (centroids.get(stat.id) ?? centroids.get(FEATURE_FALLBACKS[stat.id])) if (!c) return let dλ = -c[0] - rotation[0] dλ = ((dλ + 540) % 360) - 180 @@ -342,17 +363,23 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) const loop = (t: number) => { const dt = Math.min(t - lastFrame, 100) lastFrame = t - if (visible && loaded && !dragging && !animating) { - if (Math.abs(vel) > 1) { - rotation[0] += (vel * dt) / 1000 - vel *= Math.pow(0.95, dt / 16.7) - lastInteraction = t - setRotation() - } else if (!reducedMotion && t - lastInteraction > AUTO_RESUME_MS) { - const since = t - lastInteraction - AUTO_RESUME_MS - const ramp = Math.min(since / 1200, 1) // ease auto-rotation in - rotation[0] += (AUTO_SPEED * ramp * dt) / 1000 - setRotation() + if (visible && loaded) { + if (dirty) { + renderFrame() // one DOM write per frame, however often events fire + dirty = false + } + if (!dragging && !animating && !hovering) { + if (Math.abs(vel) > 1) { + rotation[0] += (vel * dt) / 1000 + vel *= Math.pow(0.95, dt / 16.7) + lastInteraction = t + dirty = true + } else if (!reducedMotion && t - lastInteraction > AUTO_RESUME_MS) { + const since = t - lastInteraction - AUTO_RESUME_MS + const ramp = Math.min(since / 1200, 1) // ease auto-rotation in + rotation[0] += (AUTO_SPEED * ramp * dt) / 1000 + dirty = true + } } } requestAnimationFrame(loop) @@ -362,6 +389,12 @@ const serializedRotate = JSON.stringify(INITIAL_ROTATE) +