tech-sphere.tsx 7.4 KB

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