tech-sphere.tsx 7.9 KB

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