tech-sphere.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: phones show the first 12.
  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. const PHONE_COUNT = 12;
  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. /** An even spread of `n` points over a sphere (Fibonacci lattice). */
  62. const lattice = (n: number) =>
  63. Array.from({ length: n }, (_, i) => {
  64. const y = 1 - (i / (n - 1)) * 2;
  65. const r = Math.sqrt(1 - y * y);
  66. const a = i * Math.PI * (3 - Math.sqrt(5));
  67. return [Math.cos(a) * r, y, Math.sin(a) * r] as const;
  68. });
  69. /** The toolbox as a sphere of brand marks: drag to spin, hover to read. */
  70. export function TechSphere() {
  71. const stageRef = useRef<HTMLDivElement>(null);
  72. const items = useRef<(HTMLSpanElement | null)[]>([]);
  73. const [active, setActive] = useState<string | null>(null);
  74. useEffect(() => {
  75. const stage = stageRef.current;
  76. if (!stage) return;
  77. const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  78. let rx = -0.35;
  79. let ry = 0.4;
  80. let vx = 0;
  81. let vy = still ? 0 : 0.0035;
  82. let frame = 0;
  83. let visible = false;
  84. let dragging = false;
  85. let lastX = 0;
  86. let lastY = 0;
  87. let count = ICONS.length;
  88. let points = lattice(count);
  89. // Thin the sphere out on small tiles so the marks never crowd.
  90. const fit = () => {
  91. const next = stage.clientWidth < 200 ? PHONE_COUNT : ICONS.length;
  92. if (next !== count) {
  93. count = next;
  94. points = lattice(count);
  95. }
  96. items.current.forEach((el, i) => {
  97. if (el) el.style.display = i < count ? "" : "none";
  98. });
  99. };
  100. const render = () => {
  101. const chip = stage.clientWidth < 200 ? 26 : 34;
  102. // Size the sphere to the space it has, leaving room for the nearest chips.
  103. const radius = Math.max(
  104. 24,
  105. Math.min(stage.clientWidth, stage.clientHeight) / 2 - chip * 0.62,
  106. );
  107. const cx = Math.cos(rx);
  108. const sx = Math.sin(rx);
  109. const cy = Math.cos(ry);
  110. const sy = Math.sin(ry);
  111. points.forEach(([x, y, z], i) => {
  112. const el = items.current[i];
  113. if (!el) return;
  114. // Rotate around Y, then X.
  115. const x1 = x * cy + z * sy;
  116. const z1 = -x * sy + z * cy;
  117. const y2 = y * cx - z1 * sx;
  118. const z2 = y * sx + z1 * cx;
  119. const depth = (z2 + 1) / 2;
  120. el.style.transform = `translate3d(${(x1 * radius).toFixed(1)}px, ${(y2 * radius).toFixed(1)}px, 0) scale(${(0.62 + depth * 0.5).toFixed(3)})`;
  121. el.style.opacity = (0.45 + depth * 0.55).toFixed(2);
  122. el.style.zIndex = String(Math.round(depth * 100));
  123. });
  124. };
  125. const loop = () => {
  126. frame = 0;
  127. if (!visible || document.hidden) return;
  128. if (!dragging) {
  129. ry += vy;
  130. rx += vx;
  131. vx *= 0.96;
  132. vy = vy * 0.97 + (still ? 0 : 0.0035) * 0.03;
  133. }
  134. render();
  135. frame = requestAnimationFrame(loop);
  136. };
  137. const start = () => {
  138. if (!frame) frame = requestAnimationFrame(loop);
  139. };
  140. fit();
  141. render();
  142. const io = new IntersectionObserver(([e]) => {
  143. visible = e.isIntersecting;
  144. if (visible) start();
  145. });
  146. const ro = new ResizeObserver(() => {
  147. fit();
  148. render();
  149. });
  150. io.observe(stage);
  151. ro.observe(stage);
  152. const onDown = (e: PointerEvent) => {
  153. dragging = true;
  154. lastX = e.clientX;
  155. lastY = e.clientY;
  156. stage.setPointerCapture(e.pointerId);
  157. };
  158. const onMove = (e: PointerEvent) => {
  159. if (!dragging) return;
  160. const dx = e.clientX - lastX;
  161. const dy = e.clientY - lastY;
  162. lastX = e.clientX;
  163. lastY = e.clientY;
  164. ry += dx * 0.01;
  165. rx -= dy * 0.01;
  166. vy = dx * 0.0016;
  167. vx = -dy * 0.0016;
  168. render();
  169. };
  170. const onUp = () => {
  171. dragging = false;
  172. start();
  173. };
  174. stage.addEventListener("pointerdown", onDown);
  175. stage.addEventListener("pointermove", onMove);
  176. stage.addEventListener("pointerup", onUp);
  177. stage.addEventListener("pointercancel", onUp);
  178. return () => {
  179. cancelAnimationFrame(frame);
  180. io.disconnect();
  181. ro.disconnect();
  182. stage.removeEventListener("pointerdown", onDown);
  183. stage.removeEventListener("pointermove", onMove);
  184. stage.removeEventListener("pointerup", onUp);
  185. stage.removeEventListener("pointercancel", onUp);
  186. };
  187. }, []);
  188. return (
  189. <div className="relative flex h-full flex-col p-5 sm:p-6">
  190. <p className="font-display text-[17px] font-semibold tracking-[-0.005em]">
  191. Toolbox
  192. </p>
  193. <div
  194. ref={stageRef}
  195. className="relative my-1 min-h-0 flex-1 cursor-grab touch-none select-none active:cursor-grabbing"
  196. data-nodrag
  197. >
  198. {STYLED.map((icon, i) => (
  199. <span
  200. key={icon.slug}
  201. ref={(el) => {
  202. items.current[i] = el;
  203. }}
  204. className="absolute top-1/2 left-1/2 -mt-[13px] -ml-[13px] grid size-[26px] place-items-center rounded-[8px] bg-card text-[var(--c)] shadow-[0_2px_8px_-2px_rgb(0_0_0/0.18)] ring-1 ring-line will-change-transform sm:-mt-[17px] sm:-ml-[17px] sm:size-[34px] sm:rounded-[10px] dark:bg-card-2 dark:text-[var(--cd)]"
  205. data-cursor={icon.title}
  206. style={{ "--c": icon.light, "--cd": icon.dark } as CSSProperties}
  207. onPointerEnter={() => setActive(icon.title)}
  208. onPointerLeave={() => setActive(null)}
  209. >
  210. <svg
  211. aria-label={icon.title}
  212. className="size-[14px] sm:size-[17px]"
  213. fill="currentColor"
  214. role="img"
  215. viewBox="0 0 24 24"
  216. >
  217. <path d={icon.path} />
  218. </svg>
  219. </span>
  220. ))}
  221. </div>
  222. <p
  223. aria-live="polite"
  224. className="h-4 text-center text-[12px] text-muted max-sm:hidden"
  225. >
  226. {active ?? "Drag to spin"}
  227. </p>
  228. </div>
  229. );
  230. }