| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- "use client";
- import type { COBEOptions, Globe } from "cobe";
- import { useTheme } from "next-themes";
- import { useEffect, useLayoutEffect, useRef } from "react";
- import { useT } from "@/components/locale-provider";
- /**
- * The cities lifted from the lit-up regions of the reference map. Kept as
- * data for later use — nothing renders them right now: the card shows only
- * the globe, resting on Qingdao.
- */
- export const FOOTPRINTS: {
- id: string;
- zh: string;
- en: string;
- at: [number, number];
- }[] = [
- { id: "shenyang", zh: "沈阳", en: "Shenyang", at: [41.8, 123.43] },
- { id: "zhangjiakou", zh: "张家口", en: "Zhangjiakou", at: [40.77, 114.89] },
- { id: "beijing", zh: "北京", en: "Beijing", at: [39.9, 116.41] },
- { id: "linfen", zh: "临汾", en: "Linfen", at: [36.09, 111.52] },
- { id: "xian", zh: "西安", en: "Xi'an", at: [34.34, 108.94] },
- { id: "dezhou", zh: "德州", en: "Dezhou", at: [37.44, 116.36] },
- { id: "binzhou", zh: "滨州", en: "Binzhou", at: [37.38, 117.97] },
- { id: "yantai", zh: "烟台", en: "Yantai", at: [37.46, 121.45] },
- { id: "weifang", zh: "潍坊", en: "Weifang", at: [36.71, 119.16] },
- { id: "jinan", zh: "济南", en: "Jinan", at: [36.65, 117.12] },
- { id: "qingdao", zh: "青岛", en: "Qingdao", at: [36.07, 120.38] },
- { id: "zhengzhou", zh: "郑州", en: "Zhengzhou", at: [34.75, 113.63] },
- { id: "jining", zh: "济宁", en: "Jining", at: [35.41, 116.59] },
- { id: "linyi", zh: "临沂", en: "Linyi", at: [35.1, 118.36] },
- { id: "nanjing", zh: "南京", en: "Nanjing", at: [32.06, 118.8] },
- { id: "shanghai", zh: "上海", en: "Shanghai", at: [31.23, 121.47] },
- { id: "wuhan", zh: "武汉", en: "Wuhan", at: [30.59, 114.31] },
- { id: "nanchang", zh: "南昌", en: "Nanchang", at: [28.68, 115.86] },
- { id: "ganzhou", zh: "赣州", en: "Ganzhou", at: [25.83, 114.93] },
- ];
- type Palette = Pick<
- COBEOptions,
- | "dark"
- | "diffuse"
- | "mapBrightness"
- | "mapBaseBrightness"
- | "baseColor"
- | "markerColor"
- | "glowColor"
- | "arcColor"
- >;
- const PALETTES: Record<"light" | "dark", Palette> = {
- light: {
- dark: 0,
- diffuse: 1.2,
- mapBrightness: 7,
- mapBaseBrightness: 0,
- baseColor: [1, 1, 1],
- markerColor: [0.16, 0.53, 0.87],
- glowColor: [0.98, 0.9, 0.93],
- arcColor: [0.85, 0.28, 0.48],
- },
- dark: {
- dark: 1,
- diffuse: 1.4,
- mapBrightness: 4.5,
- mapBaseBrightness: 0.02,
- baseColor: [0.15, 0.2, 0.27],
- markerColor: [0.55, 0.79, 1],
- glowColor: [0.12, 0.22, 0.32],
- arcColor: [1, 0.62, 0.74],
- },
- };
- /** cobe's angles that put a place at the center of the view. */
- const facing = (lat: number, lon: number) => [
- Math.PI - ((lon * Math.PI) / 180 - Math.PI / 2),
- (lat * Math.PI) / 180,
- ];
- // Rest on Qingdao: the city sits at the centre of the view. Still a globe, not
- // a flat country map (no borders are drawn).
- const [HOME_PHI, HOME_THETA] = facing(36.0671, 120.3826);
- const TAU = Math.PI * 2;
- /**
- * "我的足迹" on the dotted WebGL globe (cobe, ~6KB, loaded when visible). The
- * globe rests on Qingdao with no markers and no list — just the globe, in the
- * site's own glass/light-dark style.
- */
- export function GlobeCard() {
- const { globe: t } = useT();
- const wrapRef = useRef<HTMLDivElement>(null);
- const canvasRef = useRef<HTMLCanvasElement>(null);
- const globeRef = useRef<Globe | null>(null);
- const { resolvedTheme } = useTheme();
- const palette = PALETTES[resolvedTheme === "dark" ? "dark" : "light"];
- const paletteRef = useRef(palette);
- const ready = resolvedTheme !== undefined;
- // A theme switch recolours the globe in place, in the same frame as the
- // page: rebuilding it left a frame of the old globe, then an empty card.
- useLayoutEffect(() => {
- paletteRef.current = palette;
- globeRef.current?.update(palette);
- }, [palette]);
- useEffect(() => {
- const wrap = wrapRef.current;
- const canvas = canvasRef.current;
- if (!wrap || !canvas || !ready) return;
- let globe: Globe | null = null;
- let frame = 0;
- let visible = false;
- let disposed = false;
- let phi = HOME_PHI;
- let theta = HOME_THETA;
- let spin = 0;
- let dragging = false;
- let lastX = 0;
- let lastY = 0;
- const loop = () => {
- frame = 0;
- if (!globe || !visible || document.hidden) return;
- if (!dragging) {
- // Coast on the fling, then ease back to the home view along the shortest way round.
- const goal = HOME_PHI + 0.16 * Math.sin(performance.now() / 5200);
- const delta = ((((goal - phi) % TAU) + TAU * 1.5) % TAU) - Math.PI;
- phi += spin + delta * 0.012 * (1 - Math.min(1, Math.abs(spin) * 40));
- theta += (HOME_THETA - theta) * 0.02;
- spin *= 0.95;
- }
- const size = wrap.clientWidth;
- globe.update({ phi, theta, width: size * 2, height: size * 2 });
- frame = requestAnimationFrame(loop);
- };
- const start = () => {
- if (!frame) frame = requestAnimationFrame(loop);
- };
- const create = async () => {
- if (globe || disposed) return;
- const { default: createGlobe } = await import("cobe");
- if (disposed) return;
- const size = wrap.clientWidth;
- globe = createGlobe(canvas, {
- devicePixelRatio: 2,
- width: size * 2,
- height: size * 2,
- phi,
- theta,
- mapSamples: 14000,
- ...paletteRef.current,
- // Deliberately no markers: the globe is a quiet backdrop centred on
- // Qingdao, and nothing else is drawn over it.
- markers: [],
- markerElevation: 0.015,
- scale: 1.75,
- });
- globeRef.current = globe;
- canvas.style.opacity = "1";
- start();
- };
- const io = new IntersectionObserver(
- ([entry]) => {
- visible = entry.isIntersecting;
- if (visible) {
- create();
- start();
- }
- },
- { rootMargin: "120px" },
- );
- io.observe(wrap);
- const onDown = (e: PointerEvent) => {
- dragging = true;
- lastX = e.clientX;
- lastY = e.clientY;
- canvas.setPointerCapture(e.pointerId);
- };
- const onMove = (e: PointerEvent) => {
- if (!dragging) return;
- const dx = e.clientX - lastX;
- const dy = e.clientY - lastY;
- lastX = e.clientX;
- lastY = e.clientY;
- phi += dx * 0.008;
- theta = Math.max(-0.4, Math.min(1.1, theta + dy * 0.004));
- spin = dx * 0.0009;
- };
- const onUp = () => {
- dragging = false;
- };
- const onVisibility = () => !document.hidden && start();
- canvas.addEventListener("pointerdown", onDown);
- canvas.addEventListener("pointermove", onMove);
- canvas.addEventListener("pointerup", onUp);
- canvas.addEventListener("pointercancel", onUp);
- document.addEventListener("visibilitychange", onVisibility);
- return () => {
- disposed = true;
- cancelAnimationFrame(frame);
- io.disconnect();
- globe?.destroy();
- globeRef.current = null;
- canvas.removeEventListener("pointerdown", onDown);
- canvas.removeEventListener("pointermove", onMove);
- canvas.removeEventListener("pointerup", onUp);
- canvas.removeEventListener("pointercancel", onUp);
- document.removeEventListener("visibilitychange", onVisibility);
- };
- }, [ready]);
- return (
- <div className="@container relative h-full overflow-hidden">
- <div className="absolute top-5 left-5 z-10 leading-tight sm:top-6 sm:left-6">
- <p className="font-display text-[15px] leading-snug font-semibold tracking-[-0.005em] @min-[250px]:text-[17px]">
- {t.title}
- </p>
- </div>
- {/* The globe's rim sits just under the title. */}
- <div
- ref={wrapRef}
- className="absolute top-[26%] right-[-20%] left-[-8%] aspect-square @min-[220px]:top-[19%] @min-[220px]:right-[-14%] @min-[220px]:left-[-14%]"
- >
- <canvas
- ref={canvasRef}
- aria-label={t.aria}
- className="size-full cursor-grab opacity-0 transition-opacity duration-1000 active:cursor-grabbing"
- data-cursor={t.cursor}
- role="img"
- />
- </div>
- </div>
- );
- }
|