|
| 1 | +import { useEffect, useMemo, useRef } from 'react'; |
| 2 | + |
| 3 | +export interface SkillsCloudSkill { |
| 4 | + slug: string; |
| 5 | + name: string; |
| 6 | + iconSvg: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface Props { |
| 10 | + skills: SkillsCloudSkill[]; |
| 11 | +} |
| 12 | + |
| 13 | +function hash01(s: string): number { |
| 14 | + let h = 2166136261 >>> 0; |
| 15 | + for (let i = 0; i < s.length; i++) { |
| 16 | + h ^= s.charCodeAt(i); |
| 17 | + h = Math.imul(h, 16777619); |
| 18 | + } |
| 19 | + return (h >>> 0) / 0xffffffff; |
| 20 | +} |
| 21 | + |
| 22 | +const MOUSE_SHIFT_PX = 22; |
| 23 | +const SCROLL_SHIFT_PX = 28; |
| 24 | + |
| 25 | +export default function SkillsCloud({ skills }: Props) { |
| 26 | + const ref = useRef<HTMLDivElement>(null); |
| 27 | + |
| 28 | + const positioned = useMemo(() => { |
| 29 | + const n = skills.length; |
| 30 | + return skills.map((s, i) => { |
| 31 | + const phi = (i + 0.5) * Math.PI * (3 - Math.sqrt(5)); |
| 32 | + const r = Math.sqrt((i + 0.5) / n); |
| 33 | + const jitter = hash01(s.slug + ':j'); |
| 34 | + const x = 50 + Math.cos(phi) * r * 40 + (jitter - 0.5) * 6; |
| 35 | + const y = 50 + Math.sin(phi) * r * 38 + (hash01(s.slug + ':k') - 0.5) * 6; |
| 36 | + const zRaw = hash01(s.slug); |
| 37 | + const z = (zRaw - 0.5) * 360; |
| 38 | + const depth = 0.4 + zRaw * 1.1; |
| 39 | + return { ...s, x, y, z, depth, delayIndex: i }; |
| 40 | + }); |
| 41 | + }, [skills]); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + const el = ref.current; |
| 45 | + if (!el) return; |
| 46 | + if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return; |
| 47 | + |
| 48 | + const host = |
| 49 | + (el.closest('[data-skills-cloud-root]') as HTMLElement | null) ?? el; |
| 50 | + |
| 51 | + let mx = 0; |
| 52 | + let my = 0; |
| 53 | + let sy = 0; |
| 54 | + let pending = false; |
| 55 | + let enteringTimer: number | null = null; |
| 56 | + |
| 57 | + const flush = () => { |
| 58 | + pending = false; |
| 59 | + el.style.setProperty('--mx', `${mx}px`); |
| 60 | + el.style.setProperty('--my', `${my}px`); |
| 61 | + el.style.setProperty('--sy', `${sy}px`); |
| 62 | + }; |
| 63 | + const schedule = () => { |
| 64 | + if (!pending) { |
| 65 | + pending = true; |
| 66 | + requestAnimationFrame(flush); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const onMouse = (e: MouseEvent) => { |
| 71 | + const rect = host.getBoundingClientRect(); |
| 72 | + const nx = ((e.clientX - rect.left) / rect.width) * 2 - 1; |
| 73 | + const ny = ((e.clientY - rect.top) / rect.height) * 2 - 1; |
| 74 | + mx = -nx * MOUSE_SHIFT_PX; |
| 75 | + my = -ny * MOUSE_SHIFT_PX; |
| 76 | + if (el.dataset.returning !== 'entering') { |
| 77 | + el.dataset.returning = 'false'; |
| 78 | + } |
| 79 | + schedule(); |
| 80 | + }; |
| 81 | + |
| 82 | + const onEnter = (e: MouseEvent) => { |
| 83 | + const rect = host.getBoundingClientRect(); |
| 84 | + const nx = ((e.clientX - rect.left) / rect.width) * 2 - 1; |
| 85 | + const ny = ((e.clientY - rect.top) / rect.height) * 2 - 1; |
| 86 | + mx = -nx * MOUSE_SHIFT_PX; |
| 87 | + my = -ny * MOUSE_SHIFT_PX; |
| 88 | + el.dataset.returning = 'entering'; |
| 89 | + schedule(); |
| 90 | + if (enteringTimer !== null) window.clearTimeout(enteringTimer); |
| 91 | + enteringTimer = window.setTimeout(() => { |
| 92 | + if (el.dataset.returning === 'entering') { |
| 93 | + el.dataset.returning = 'false'; |
| 94 | + } |
| 95 | + enteringTimer = null; |
| 96 | + }, 900); |
| 97 | + }; |
| 98 | + |
| 99 | + const onLeave = () => { |
| 100 | + mx = 0; |
| 101 | + my = 0; |
| 102 | + el.dataset.returning = 'true'; |
| 103 | + if (enteringTimer !== null) { |
| 104 | + window.clearTimeout(enteringTimer); |
| 105 | + enteringTimer = null; |
| 106 | + } |
| 107 | + schedule(); |
| 108 | + }; |
| 109 | + |
| 110 | + const onScroll = () => { |
| 111 | + const rect = host.getBoundingClientRect(); |
| 112 | + const vh = window.innerHeight || 1; |
| 113 | + const centered = (rect.top + rect.height / 2 - vh / 2) / vh; |
| 114 | + sy = -centered * SCROLL_SHIFT_PX; |
| 115 | + schedule(); |
| 116 | + }; |
| 117 | + |
| 118 | + host.addEventListener('mousemove', onMouse); |
| 119 | + host.addEventListener('mouseenter', onEnter); |
| 120 | + host.addEventListener('mouseleave', onLeave); |
| 121 | + window.addEventListener('scroll', onScroll, { passive: true }); |
| 122 | + |
| 123 | + return () => { |
| 124 | + host.removeEventListener('mousemove', onMouse); |
| 125 | + host.removeEventListener('mouseenter', onEnter); |
| 126 | + host.removeEventListener('mouseleave', onLeave); |
| 127 | + window.removeEventListener('scroll', onScroll); |
| 128 | + if (enteringTimer !== null) window.clearTimeout(enteringTimer); |
| 129 | + }; |
| 130 | + }, []); |
| 131 | + |
| 132 | + return ( |
| 133 | + <div ref={ref} aria-hidden="true" className="skills-cloud"> |
| 134 | + {positioned.map((s) => ( |
| 135 | + <span |
| 136 | + key={s.slug} |
| 137 | + className="skills-cloud__icon" |
| 138 | + style={{ |
| 139 | + left: `${s.x}%`, |
| 140 | + top: `${s.y}%`, |
| 141 | + ['--depth' as string]: String(s.depth), |
| 142 | + ['--z' as string]: `${s.z}px`, |
| 143 | + ['--delay' as string]: `${(s.delayIndex % 7) * 0.6}s`, |
| 144 | + ['--duration' as string]: `${6 + (s.delayIndex % 5)}s`, |
| 145 | + ['--size' as string]: `${Math.round(26 + s.depth * 22)}px`, |
| 146 | + opacity: 0.45 + s.depth * 0.4, |
| 147 | + }} |
| 148 | + dangerouslySetInnerHTML={{ __html: s.iconSvg }} |
| 149 | + /> |
| 150 | + ))} |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments