tech-sphere.tsx 7.3 KB

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