109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
'use client';
|
|||
|
|
|
||
|
|
import React, { useState, useRef, useEffect } from 'react';
|
||
|
|
|
||
|
|
interface Kinetic3DTextProps {
|
||
|
|
primaryText: string;
|
||
|
|
highlightText?: string;
|
||
|
|
subtitle?: string;
|
||
|
|
className?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function Kinetic3DText({
|
||
|
|
primaryText,
|
||
|
|
highlightText,
|
||
|
|
subtitle,
|
||
|
|
className = '',
|
||
|
|
}: Kinetic3DTextProps) {
|
||
|
|
const [tilt, setTilt] = useState({ rx: 0, ry: 0, active: false });
|
||
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
let rafId: number;
|
||
|
|
let targetRx = 0;
|
||
|
|
let targetRy = 0;
|
||
|
|
let currentRx = 0;
|
||
|
|
let currentRy = 0;
|
||
|
|
|
||
|
|
const handleMouseMove = (e: MouseEvent) => {
|
||
|
|
if (!containerRef.current) return;
|
||
|
|
const rect = containerRef.current.getBoundingClientRect();
|
||
|
|
const centerX = rect.left + rect.width / 2;
|
||
|
|
const centerY = rect.top + rect.height / 2;
|
||
|
|
|
||
|
|
// Normalized coordinates -1 to +1
|
||
|
|
const nx = (e.clientX - centerX) / (window.innerWidth * 0.5);
|
||
|
|
const ny = (e.clientY - centerY) / (window.innerHeight * 0.5);
|
||
|
|
|
||
|
|
targetRy = Math.max(Math.min(nx * 10, 12), -12);
|
||
|
|
targetRx = Math.max(Math.min(-ny * 8, 10), -10);
|
||
|
|
};
|
||
|
|
|
||
|
|
const updateLoop = () => {
|
||
|
|
// Smooth inertial lerp
|
||
|
|
currentRx += (targetRx - currentRx) * 0.1;
|
||
|
|
currentRy += (targetRy - currentRy) * 0.1;
|
||
|
|
|
||
|
|
if (containerRef.current) {
|
||
|
|
containerRef.current.style.transform = `rotateX(${currentRx.toFixed(2)}deg) rotateY(${currentRy.toFixed(2)}deg)`;
|
||
|
|
}
|
||
|
|
|
||
|
|
rafId = requestAnimationFrame(updateLoop);
|
||
|
|
};
|
||
|
|
|
||
|
|
window.addEventListener('mousemove', handleMouseMove, { passive: true });
|
||
|
|
rafId = requestAnimationFrame(updateLoop);
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
window.removeEventListener('mousemove', handleMouseMove);
|
||
|
|
cancelAnimationFrame(rafId);
|
||
|
|
};
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className={`relative perspective-1000 select-none ${className}`}>
|
||
|
|
{/* 3D Stereoscopic Kinetic Text Stage */}
|
||
|
|
<div
|
||
|
|
ref={containerRef}
|
||
|
|
className="preserve-3d transition-transform duration-75 ease-out will-change-transform"
|
||
|
|
>
|
||
|
|
{/* Layer 1: Ambient 3D Text Holographic Depth Shadow */}
|
||
|
|
<div
|
||
|
|
className="absolute inset-0 select-none pointer-events-none opacity-20 blur-md transform translate-z-[-20px] scale-[1.03]"
|
||
|
|
aria-hidden="true"
|
||
|
|
>
|
||
|
|
<span className="text-4xl sm:text-5xl lg:text-6xl xl:text-7xl font-display font-black text-blue-700 leading-[1.08] tracking-tight block">
|
||
|
|
{primaryText} <br className="hidden sm:inline" />
|
||
|
|
{highlightText}
|
||
|
|
</span>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{/* Layer 2: Main Floating 3D Headline */}
|
||
|
|
<h1
|
||
|
|
className="text-4xl sm:text-5xl lg:text-6xl xl:text-7xl font-display font-black text-slate-900 tracking-tight leading-[1.08] relative z-10 preserve-3d"
|
||
|
|
style={{ transform: 'translateZ(35px)' }}
|
||
|
|
>
|
||
|
|
<span>{primaryText}</span> <br className="hidden sm:inline" />
|
||
|
|
{highlightText && (
|
||
|
|
<span className="relative inline-block mt-1 sm:mt-0 preserve-3d">
|
||
|
|
<span className="text-transparent bg-clip-text bg-gradient-to-r from-blue-700 via-indigo-600 to-amber-600 drop-shadow-[0_4px_12px_rgba(37,99,235,0.25)]">
|
||
|
|
{highlightText}
|
||
|
|
</span>
|
||
|
|
</span>
|
||
|
|
)}
|
||
|
|
</h1>
|
||
|
|
|
||
|
|
{/* Layer 3: Elevated Subtitle */}
|
||
|
|
{subtitle && (
|
||
|
|
<p
|
||
|
|
className="mt-6 text-base sm:text-lg lg:text-xl text-slate-600 leading-relaxed max-w-2xl mx-auto font-normal preserve-3d"
|
||
|
|
style={{ transform: 'translateZ(20px)' }}
|
||
|
|
>
|
||
|
|
{subtitle}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|