tech-sphere.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. "use client";
  2. import { useEffect, useRef, useState, type CSSProperties } from "react";
  3. import {
  4. siDocker,
  5. siFigma,
  6. siFirebase,
  7. siGit,
  8. siGithub,
  9. siJavascript,
  10. siNextdotjs,
  11. siNodedotjs,
  12. siOpenjdk,
  13. siPostgresql,
  14. siPrisma,
  15. siPython,
  16. siReact,
  17. siTailwindcss,
  18. siThreedotjs,
  19. siTypescript,
  20. siVercel,
  21. siVitest,
  22. } from "simple-icons";
  23. import { useT } from "@/components/locale-provider";
  24. // Most-used first: small tiles show only the first few.
  25. const ICONS = [
  26. siTypescript,
  27. siReact,
  28. siNextdotjs,
  29. siPython,
  30. siNodedotjs,
  31. siDocker,
  32. siPostgresql,
  33. siTailwindcss,
  34. siOpenjdk,
  35. siGithub,
  36. siFigma,
  37. siVercel,
  38. siJavascript,
  39. siFirebase,
  40. siPrisma,
  41. siGit,
  42. siThreedotjs,
  43. siVitest,
  44. ];
  45. function luminance(hex: string) {
  46. const c = [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
  47. const [r, g, b] = c.map((v) =>
  48. v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4,
  49. );
  50. return 0.2126 * r + 0.7152 * g + 0.0722 * b;
  51. }
  52. // Brand colours, swapped for ink when they'd vanish against the card.
  53. const STYLED = ICONS.map((icon) => {
  54. const lum = luminance(icon.hex);
  55. return {
  56. ...icon,
  57. light: lum > 0.72 ? "var(--ink-2)" : `#${icon.hex}`,
  58. dark: lum < 0.07 ? "var(--ink)" : `#${icon.hex}`,
  59. };
  60. });
  61. /**
  62. * An even spread of `n` points over a sphere (Fibonacci lattice), offset by
  63. * half a step so no mark sits on a pole, where others would pass too close.
  64. */
  65. const lattice = (n: number) =>
  66. Array.from({ length: n }, (_, i) => {
  67. const y = 1 - ((i + 0.5) / n) * 2;
  68. const r = Math.sqrt(1 - y * y);
  69. const a = i * Math.PI * (3 - Math.sqrt(5));
  70. return [Math.cos(a) * r, y, Math.sin(a) * r] as const;
  71. });
  72. /** The toolbox as a sphere of brand marks: drag to spin, hover to read. */
  73. export function TechSphere() {
  74. const { tech: t } = useT();
  75. const stageRef = useRef<HTMLDivElement>(null);
  76. const items = useRef<(HTMLSpanElement | null)[]>([]);
  77. const [active, setActive] = useState<string | null>(null);
  78. useEffect(() => {
  79. const stage = stageRef.current;
  80. if (!stage) return;
  81. const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  82. let rx = -0.35;
  83. let ry = 0.4;
  84. let vx = 0;
  85. let vy = still ? 0 : 0.0035;
  86. let frame = 0;
  87. let visible = false;
  88. let dragging = false;
  89. let lastX = 0;
  90. let lastY = 0;
  91. let count = ICONS.length;
  92. let points = lattice(count);
  93. let radius = 24;
  94. // Size it all to the stage: the chips scale with it, the sphere fills it,
  95. // and it carries only as many marks as fit a chip's width apart.
  96. const fit = () => {
  97. const side = Math.min(stage.clientWidth, stage.clientHeight);
  98. const chip = Math.round(Math.min(34, Math.max(22, side * 0.19)));
  99. // Chips at the rim render at under 0.9x, so half a chip keeps them inside.
  100. radius = Math.max(24, side / 2 - chip / 2);
  101. // Neighbours sit about 3.5 * radius / sqrt(count) apart on the sphere;
  102. // keeping that near two chips leaves the front row a chip apart.
  103. const next = Math.min(
  104. ICONS.length,
  105. Math.max(8, Math.floor(3.6 * (radius / chip) ** 2)),
  106. );
  107. stage.style.setProperty("--chip", `${chip}px`);
  108. if (next !== count) {
  109. count = next;
  110. points = lattice(count);
  111. }
  112. items.current.forEach((el, i) => {
  113. if (el) el.style.display = i < count ? "" : "none";
  114. });
  115. };
  116. const render = () => {
  117. const cx = Math.cos(rx);
  118. const sx = Math.sin(rx);
  119. const cy = Math.cos(ry);
  120. const sy = Math.sin(ry);
  121. points.forEach(([x, y, z], i) => {
  122. const el = items.current[i];
  123. if (!el) return;
  124. // Rotate around Y, then X.
  125. const x1 = x * cy + z * sy;
  126. const z1 = -x * sy + z * cy;
  127. const y2 = y * cx - z1 * sx;
  128. const z2 = y * sx + z1 * cx;
  129. const depth = (z2 + 1) / 2;
  130. el.style.transform = `translate3d(${(x1 * radius).toFixed(1)}px, ${(y2 * radius).toFixed(1)}px, 0) scale(${(0.62 + depth * 0.5).toFixed(3)})`;
  131. el.style.opacity = (0.45 + depth * 0.55).toFixed(2);
  132. el.style.zIndex = String(Math.round(depth * 100));
  133. });
  134. };
  135. const loop = () => {
  136. frame = 0;
  137. if (!visible || document.hidden) return;
  138. if (!dragging) {
  139. ry += vy;
  140. rx += vx;
  141. vx *= 0.96;
  142. vy = vy * 0.97 + (still ? 0 : 0.0035) * 0.03;
  143. }
  144. render();
  145. frame = requestAnimationFrame(loop);
  146. };
  147. const start = () => {
  148. if (!frame) frame = requestAnimationFrame(loop);
  149. };
  150. fit();
  151. render();
  152. const io = new IntersectionObserver(([e]) => {
  153. visible = e.isIntersecting;
  154. if (visible) start();
  155. });
  156. const ro = new ResizeObserver(() => {
  157. fit();
  158. render();
  159. });
  160. io.observe(stage);
  161. ro.observe(stage);
  162. const onDown = (e: PointerEvent) => {
  163. dragging = true;
  164. lastX = e.clientX;
  165. lastY = e.clientY;
  166. stage.setPointerCapture(e.pointerId);
  167. };
  168. const onMove = (e: PointerEvent) => {
  169. if (!dragging) return;
  170. const dx = e.clientX - lastX;
  171. const dy = e.clientY - lastY;
  172. lastX = e.clientX;
  173. lastY = e.clientY;
  174. ry += dx * 0.01;
  175. rx -= dy * 0.01;
  176. vy = dx * 0.0016;
  177. vx = -dy * 0.0016;
  178. render();
  179. };
  180. const onUp = () => {
  181. dragging = false;
  182. start();
  183. };
  184. stage.addEventListener("pointerdown", onDown);
  185. stage.addEventListener("pointermove", onMove);
  186. stage.addEventListener("pointerup", onUp);
  187. stage.addEventListener("pointercancel", onUp);
  188. return () => {
  189. cancelAnimationFrame(frame);
  190. io.disconnect();
  191. ro.disconnect();
  192. stage.removeEventListener("pointerdown", onDown);
  193. stage.removeEventListener("pointermove", onMove);
  194. stage.removeEventListener("pointerup", onUp);
  195. stage.removeEventListener("pointercancel", onUp);
  196. };
  197. }, []);
  198. return (
  199. <div className="relative flex h-full flex-col p-5 sm:p-6">
  200. <p className="font-display text-[17px] font-semibold tracking-[-0.005em]">
  201. {t.title}
  202. </p>
  203. {/* The sphere may run into the card's padding: only the text keeps the inset.
  204. Vertical swipes still scroll the page on touch. */}
  205. <div
  206. ref={stageRef}
  207. className="relative -mx-5 mt-1 -mb-2.5 min-h-0 flex-1 cursor-grab touch-pan-y select-none [--chip:26px] active:cursor-grabbing sm:-mx-6 sm:mb-1"
  208. data-nodrag
  209. >
  210. {STYLED.map((icon, i) => (
  211. <span
  212. key={icon.slug}
  213. ref={(el) => {
  214. items.current[i] = el;
  215. }}
  216. className="absolute top-1/2 left-1/2 -mt-[calc(var(--chip)/2)] -ml-[calc(var(--chip)/2)] grid size-(--chip) place-items-center rounded-[calc(var(--chip)*0.3)] bg-[linear-gradient(var(--card),var(--card))] text-[var(--c)] shadow-[0_2px_8px_-2px_rgb(0_0_0/0.18)] ring-1 ring-line will-change-transform dark:bg-[linear-gradient(var(--card-2),var(--card-2))] dark:text-[var(--cd)]"
  217. data-cursor={icon.title}
  218. style={{ "--c": icon.light, "--cd": icon.dark } as CSSProperties}
  219. onPointerEnter={() => setActive(icon.title)}
  220. onPointerLeave={() => setActive(null)}
  221. >
  222. <svg
  223. aria-label={icon.title}
  224. className="size-[calc(var(--chip)/2)]"
  225. fill="currentColor"
  226. role="img"
  227. viewBox="0 0 24 24"
  228. >
  229. <path d={icon.path} />
  230. </svg>
  231. </span>
  232. ))}
  233. </div>
  234. <p
  235. aria-live="polite"
  236. className="h-4 text-center text-[12px] text-muted max-sm:hidden"
  237. >
  238. {active ?? t.drag}
  239. </p>
  240. </div>
  241. );
  242. }