globe.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. "use client";
  2. import type { COBEOptions, Globe } from "cobe";
  3. import { useTheme } from "next-themes";
  4. import { useEffect, useLayoutEffect, useRef } from "react";
  5. import { useT } from "@/components/locale-provider";
  6. /**
  7. * The cities lifted from the lit-up regions of the reference map. Kept as
  8. * data for later use — nothing renders them right now: the card shows only
  9. * the globe, resting on Qingdao.
  10. */
  11. export const FOOTPRINTS: {
  12. id: string;
  13. zh: string;
  14. en: string;
  15. at: [number, number];
  16. }[] = [
  17. { id: "shenyang", zh: "沈阳", en: "Shenyang", at: [41.8, 123.43] },
  18. { id: "zhangjiakou", zh: "张家口", en: "Zhangjiakou", at: [40.77, 114.89] },
  19. { id: "beijing", zh: "北京", en: "Beijing", at: [39.9, 116.41] },
  20. { id: "linfen", zh: "临汾", en: "Linfen", at: [36.09, 111.52] },
  21. { id: "xian", zh: "西安", en: "Xi'an", at: [34.34, 108.94] },
  22. { id: "dezhou", zh: "德州", en: "Dezhou", at: [37.44, 116.36] },
  23. { id: "binzhou", zh: "滨州", en: "Binzhou", at: [37.38, 117.97] },
  24. { id: "yantai", zh: "烟台", en: "Yantai", at: [37.46, 121.45] },
  25. { id: "weifang", zh: "潍坊", en: "Weifang", at: [36.71, 119.16] },
  26. { id: "jinan", zh: "济南", en: "Jinan", at: [36.65, 117.12] },
  27. { id: "qingdao", zh: "青岛", en: "Qingdao", at: [36.07, 120.38] },
  28. { id: "zhengzhou", zh: "郑州", en: "Zhengzhou", at: [34.75, 113.63] },
  29. { id: "jining", zh: "济宁", en: "Jining", at: [35.41, 116.59] },
  30. { id: "linyi", zh: "临沂", en: "Linyi", at: [35.1, 118.36] },
  31. { id: "nanjing", zh: "南京", en: "Nanjing", at: [32.06, 118.8] },
  32. { id: "shanghai", zh: "上海", en: "Shanghai", at: [31.23, 121.47] },
  33. { id: "wuhan", zh: "武汉", en: "Wuhan", at: [30.59, 114.31] },
  34. { id: "nanchang", zh: "南昌", en: "Nanchang", at: [28.68, 115.86] },
  35. { id: "ganzhou", zh: "赣州", en: "Ganzhou", at: [25.83, 114.93] },
  36. ];
  37. type Palette = Pick<
  38. COBEOptions,
  39. | "dark"
  40. | "diffuse"
  41. | "mapBrightness"
  42. | "mapBaseBrightness"
  43. | "baseColor"
  44. | "markerColor"
  45. | "glowColor"
  46. | "arcColor"
  47. >;
  48. const PALETTES: Record<"light" | "dark", Palette> = {
  49. light: {
  50. dark: 0,
  51. diffuse: 1.2,
  52. mapBrightness: 7,
  53. mapBaseBrightness: 0,
  54. baseColor: [1, 1, 1],
  55. markerColor: [0.16, 0.53, 0.87],
  56. glowColor: [0.98, 0.9, 0.93],
  57. arcColor: [0.85, 0.28, 0.48],
  58. },
  59. dark: {
  60. dark: 1,
  61. diffuse: 1.4,
  62. mapBrightness: 4.5,
  63. mapBaseBrightness: 0.02,
  64. baseColor: [0.15, 0.2, 0.27],
  65. markerColor: [0.55, 0.79, 1],
  66. glowColor: [0.12, 0.22, 0.32],
  67. arcColor: [1, 0.62, 0.74],
  68. },
  69. };
  70. /** cobe's angles that put a place at the center of the view. */
  71. const facing = (lat: number, lon: number) => [
  72. Math.PI - ((lon * Math.PI) / 180 - Math.PI / 2),
  73. (lat * Math.PI) / 180,
  74. ];
  75. // Rest on Qingdao: the city sits at the centre of the view. Still a globe, not
  76. // a flat country map (no borders are drawn).
  77. const [HOME_PHI, HOME_THETA] = facing(36.0671, 120.3826);
  78. const TAU = Math.PI * 2;
  79. /**
  80. * "我的足迹" on the dotted WebGL globe (cobe, ~6KB, loaded when visible). The
  81. * globe rests on Qingdao with no markers and no list — just the globe, in the
  82. * site's own glass/light-dark style.
  83. */
  84. export function GlobeCard() {
  85. const { globe: t } = useT();
  86. const wrapRef = useRef<HTMLDivElement>(null);
  87. const canvasRef = useRef<HTMLCanvasElement>(null);
  88. const globeRef = useRef<Globe | null>(null);
  89. const { resolvedTheme } = useTheme();
  90. const palette = PALETTES[resolvedTheme === "dark" ? "dark" : "light"];
  91. const paletteRef = useRef(palette);
  92. const ready = resolvedTheme !== undefined;
  93. // A theme switch recolours the globe in place, in the same frame as the
  94. // page: rebuilding it left a frame of the old globe, then an empty card.
  95. useLayoutEffect(() => {
  96. paletteRef.current = palette;
  97. globeRef.current?.update(palette);
  98. }, [palette]);
  99. useEffect(() => {
  100. const wrap = wrapRef.current;
  101. const canvas = canvasRef.current;
  102. if (!wrap || !canvas || !ready) return;
  103. let globe: Globe | null = null;
  104. let frame = 0;
  105. let visible = false;
  106. let disposed = false;
  107. let phi = HOME_PHI;
  108. let theta = HOME_THETA;
  109. let spin = 0;
  110. let dragging = false;
  111. let lastX = 0;
  112. let lastY = 0;
  113. const loop = () => {
  114. frame = 0;
  115. if (!globe || !visible || document.hidden) return;
  116. if (!dragging) {
  117. // Coast on the fling, then ease back to the home view along the shortest way round.
  118. const goal = HOME_PHI + 0.16 * Math.sin(performance.now() / 5200);
  119. const delta = ((((goal - phi) % TAU) + TAU * 1.5) % TAU) - Math.PI;
  120. phi += spin + delta * 0.012 * (1 - Math.min(1, Math.abs(spin) * 40));
  121. theta += (HOME_THETA - theta) * 0.02;
  122. spin *= 0.95;
  123. }
  124. const size = wrap.clientWidth;
  125. globe.update({ phi, theta, width: size * 2, height: size * 2 });
  126. frame = requestAnimationFrame(loop);
  127. };
  128. const start = () => {
  129. if (!frame) frame = requestAnimationFrame(loop);
  130. };
  131. const create = async () => {
  132. if (globe || disposed) return;
  133. const { default: createGlobe } = await import("cobe");
  134. if (disposed) return;
  135. const size = wrap.clientWidth;
  136. globe = createGlobe(canvas, {
  137. devicePixelRatio: 2,
  138. width: size * 2,
  139. height: size * 2,
  140. phi,
  141. theta,
  142. mapSamples: 14000,
  143. ...paletteRef.current,
  144. // Deliberately no markers: the globe is a quiet backdrop centred on
  145. // Qingdao, and nothing else is drawn over it.
  146. markers: [],
  147. markerElevation: 0.015,
  148. scale: 1.75,
  149. });
  150. globeRef.current = globe;
  151. canvas.style.opacity = "1";
  152. start();
  153. };
  154. const io = new IntersectionObserver(
  155. ([entry]) => {
  156. visible = entry.isIntersecting;
  157. if (visible) {
  158. create();
  159. start();
  160. }
  161. },
  162. { rootMargin: "120px" },
  163. );
  164. io.observe(wrap);
  165. const onDown = (e: PointerEvent) => {
  166. dragging = true;
  167. lastX = e.clientX;
  168. lastY = e.clientY;
  169. canvas.setPointerCapture(e.pointerId);
  170. };
  171. const onMove = (e: PointerEvent) => {
  172. if (!dragging) return;
  173. const dx = e.clientX - lastX;
  174. const dy = e.clientY - lastY;
  175. lastX = e.clientX;
  176. lastY = e.clientY;
  177. phi += dx * 0.008;
  178. theta = Math.max(-0.4, Math.min(1.1, theta + dy * 0.004));
  179. spin = dx * 0.0009;
  180. };
  181. const onUp = () => {
  182. dragging = false;
  183. };
  184. const onVisibility = () => !document.hidden && start();
  185. canvas.addEventListener("pointerdown", onDown);
  186. canvas.addEventListener("pointermove", onMove);
  187. canvas.addEventListener("pointerup", onUp);
  188. canvas.addEventListener("pointercancel", onUp);
  189. document.addEventListener("visibilitychange", onVisibility);
  190. return () => {
  191. disposed = true;
  192. cancelAnimationFrame(frame);
  193. io.disconnect();
  194. globe?.destroy();
  195. globeRef.current = null;
  196. canvas.removeEventListener("pointerdown", onDown);
  197. canvas.removeEventListener("pointermove", onMove);
  198. canvas.removeEventListener("pointerup", onUp);
  199. canvas.removeEventListener("pointercancel", onUp);
  200. document.removeEventListener("visibilitychange", onVisibility);
  201. };
  202. }, [ready]);
  203. return (
  204. <div className="@container relative h-full overflow-hidden">
  205. <div className="absolute top-5 left-5 z-10 leading-tight sm:top-6 sm:left-6">
  206. <p className="font-display text-[15px] leading-snug font-semibold tracking-[-0.005em] @min-[250px]:text-[17px]">
  207. {t.title}
  208. </p>
  209. </div>
  210. {/* The globe's rim sits just under the title. */}
  211. <div
  212. ref={wrapRef}
  213. className="absolute top-[26%] right-[-20%] left-[-8%] aspect-square @min-[220px]:top-[19%] @min-[220px]:right-[-14%] @min-[220px]:left-[-14%]"
  214. >
  215. <canvas
  216. ref={canvasRef}
  217. aria-label={t.aria}
  218. className="size-full cursor-grab opacity-0 transition-opacity duration-1000 active:cursor-grabbing"
  219. data-cursor={t.cursor}
  220. role="img"
  221. />
  222. </div>
  223. </div>
  224. );
  225. }