|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useRef } from 'react' |
| 4 | +import * as THREE from 'three' |
| 5 | +import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry.js' |
| 6 | + |
| 7 | +export default function LibraryIcon() { |
| 8 | + const mountRef = useRef<HTMLDivElement | null>(null) |
| 9 | + |
| 10 | + useEffect(() => { |
| 11 | + // 1. Scene Setup |
| 12 | + const scene = new THREE.Scene() |
| 13 | + const camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000) |
| 14 | + const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }) |
| 15 | + |
| 16 | + renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // Improve sharpness |
| 17 | + renderer.setSize(200, 200) // Match container size |
| 18 | + |
| 19 | + // Keep the canvas inside its container so centering works |
| 20 | + renderer.domElement.style.width = '100%' |
| 21 | + renderer.domElement.style.height = '100%' |
| 22 | + renderer.domElement.style.display = 'block' |
| 23 | + const mountEl = mountRef.current |
| 24 | + if (!mountEl) return |
| 25 | + mountEl.appendChild(renderer.domElement) |
| 26 | + |
| 27 | + // 2. Create the "Stacked Books" Group |
| 28 | + const group = new THREE.Group() |
| 29 | + const primaryColor = |
| 30 | + getComputedStyle(document.documentElement) |
| 31 | + .getPropertyValue('--color-primary') |
| 32 | + .trim() || '#4f46e5' |
| 33 | + const material = new THREE.MeshStandardMaterial({ color: primaryColor }) |
| 34 | + |
| 35 | + const heights = [2.5, 1.8, 2.2, 2.2] // first/last tallest, second shortest, third medium |
| 36 | + const baseY = -1 // anchor so all books rest on a shared baseline |
| 37 | + |
| 38 | + for (let i = 0; i < 4; i++) { |
| 39 | + const height = heights[i] |
| 40 | + const bookGeom = new RoundedBoxGeometry(0.4, height, 1.7, 3, 0.07) |
| 41 | + const book = new THREE.Mesh(bookGeom, material) |
| 42 | + book.position.x = (i - 1.5) * 0.6 |
| 43 | + book.position.y = baseY + height / 2 |
| 44 | + |
| 45 | + if (i === 3) { |
| 46 | + // Tilt the last book |
| 47 | + book.rotation.z = Math.PI / 10 |
| 48 | + book.position.x += 0.1 |
| 49 | + } |
| 50 | + |
| 51 | + group.add(book) |
| 52 | + } |
| 53 | + scene.add(group) |
| 54 | + |
| 55 | + // 3. Lighting |
| 56 | + const light = new THREE.DirectionalLight(0xffffff, 1) |
| 57 | + light.position.set(2, 2, 5) |
| 58 | + scene.add(light, new THREE.AmbientLight(0xffffff, 0.5)) |
| 59 | + |
| 60 | + camera.position.z = 5 |
| 61 | + |
| 62 | + // 4. Mouse Tracking Logic |
| 63 | + const onMouseMove = (event: MouseEvent) => { |
| 64 | + // Normalize mouse coordinates to -1 to +1 |
| 65 | + const x = (event.clientX / window.innerWidth) * 2 - 1 |
| 66 | + const y = -(event.clientY / window.innerHeight) * 2 + 1 |
| 67 | + |
| 68 | + // Rotate the group slightly based on mouse |
| 69 | + group.rotation.y = x * 0.5 |
| 70 | + group.rotation.x = -y * 0.5 |
| 71 | + } |
| 72 | + |
| 73 | + window.addEventListener('mousemove', onMouseMove) |
| 74 | + |
| 75 | + // 5. Animation Loop |
| 76 | + const animate = () => { |
| 77 | + requestAnimationFrame(animate) |
| 78 | + renderer.render(scene, camera) |
| 79 | + } |
| 80 | + animate() |
| 81 | + |
| 82 | + // Cleanup on unmount |
| 83 | + return () => { |
| 84 | + window.removeEventListener('mousemove', onMouseMove) |
| 85 | + mountEl.removeChild(renderer.domElement) |
| 86 | + renderer.dispose() |
| 87 | + } |
| 88 | + }, []) |
| 89 | + |
| 90 | + return <div ref={mountRef} style={{ width: '200px', height: '200px' }} /> |
| 91 | +} |
0 commit comments