'use client'; import React, { useEffect, useRef } from 'react'; /** * LiquidGlassLensCursor: * Pure Optical Liquid Glass Lens cursor with 100% transparent crystal body, * sharp specular water-drop glints, dynamic velocity squash & stretch, * and universal responsiveness across all devices (Desktop, Mobile, Tablet, Trackpad). * * Key Capabilities: * - Ultra-transparent liquid glass (Zero frosting, zero milky haze, pure optical clarity) * - Sharp meniscus rim & caustic reflections matching brand theme (#2563EB, #06B6D4) * - True multi-device responsiveness: Mouse on desktop + fluid touch lens on mobile/tablets * - Hardware-accelerated GPU transforms (locked 60-120fps, zero layout reflows) * - Strictly pointer-events-none: never blocks taps, clicks, scrolls, or dragging */ export function SmoothCursor() { const lensRef = useRef(null); const dotRef = useRef(null); const causticRef = useRef(null); useEffect(() => { if (typeof window === 'undefined') return; const lens = lensRef.current; const dot = dotRef.current; const caustic = causticRef.current; if (!lens || !dot) return; let mouseX = -200; let mouseY = -200; let prevMouseX = -200; let prevMouseY = -200; let lensX = -200; let lensY = -200; let causticX = -200; let causticY = -200; let currentSpeed = 0; let isHovered = false; let isPressed = false; let isVisible = false; let isTouchActive = false; let fadeTimeout: NodeJS.Timeout | null = null; let animId = 0; // 1. Mouse & Pointer Movement (Desktop & Trackpads) const onMouseMove = (e: MouseEvent) => { isTouchActive = false; mouseX = e.clientX; mouseY = e.clientY; if (fadeTimeout) clearTimeout(fadeTimeout); if (!isVisible) { isVisible = true; lens.style.opacity = '1'; dot.style.opacity = '1'; if (caustic) caustic.style.opacity = '1'; } }; const onMouseLeave = () => { if (isTouchActive) return; isVisible = false; lens.style.opacity = '0'; dot.style.opacity = '0'; if (caustic) caustic.style.opacity = '0'; }; const onMouseEnter = () => { if (isTouchActive) return; isVisible = true; lens.style.opacity = '1'; dot.style.opacity = '1'; if (caustic) caustic.style.opacity = '1'; }; const onMouseDown = () => { isPressed = true; }; const onMouseUp = () => { isPressed = false; }; // 2. Touch Handlers (Full Mobile, Tablet, iPad & Touchscreen Compatibility) const onTouchStart = (e: TouchEvent) => { if (!e.touches || e.touches.length === 0) return; isTouchActive = true; if (fadeTimeout) clearTimeout(fadeTimeout); const touch = e.touches[0]; mouseX = touch.clientX; mouseY = touch.clientY; // If opening fresh, snap directly to finger touch point if (!isVisible) { lensX = mouseX; lensY = mouseY; prevMouseX = mouseX; prevMouseY = mouseY; isVisible = true; lens.style.opacity = '1'; if (caustic) caustic.style.opacity = '1'; } // On touch, hide the tiny dot (the finger is right there) dot.style.opacity = '0'; isPressed = true; }; const onTouchMove = (e: TouchEvent) => { if (!e.touches || e.touches.length === 0) return; const touch = e.touches[0]; mouseX = touch.clientX; mouseY = touch.clientY; }; const onTouchEnd = () => { isPressed = false; // Graceful liquid fade out after touch interaction completes if (fadeTimeout) clearTimeout(fadeTimeout); fadeTimeout = setTimeout(() => { isVisible = false; lens.style.opacity = '0'; if (caustic) caustic.style.opacity = '0'; }, 350); }; // Throttled interactive element detection for magnetic lens scaling let lastCheck = 0; const onMouseOver = (e: MouseEvent) => { const now = performance.now(); if (now - lastCheck < 25) return; lastCheck = now; const target = e.target as HTMLElement | null; if (!target) return; const interactive = target.closest('a, button, input, textarea, select, [role="button"], .cursor-pointer, [data-interactive]'); isHovered = !!interactive; }; // High performance RAF loop (locked 60-120fps, 100% GPU composited) const loop = () => { // Calculate velocity vector of pointer movement const vx = mouseX - prevMouseX; const vy = mouseY - prevMouseY; prevMouseX = mouseX; prevMouseY = mouseY; const rawSpeed = Math.hypot(vx, vy); currentSpeed += (rawSpeed - currentSpeed) * 0.2; // Silky liquid lag for pure organic fluid following lensX += (mouseX - lensX) * 0.35; lensY += (mouseY - lensY) * 0.35; // Secondary ambient caustic trailing slightly behind if (caustic) { causticX += (mouseX - causticX) * 0.18; causticY += (mouseY - causticY) * 0.18; caustic.style.transform = `translate3d(${causticX}px, ${causticY}px, 0) translate3d(-50%, -50%, 0)`; } // Dynamic liquid squash & stretch based on velocity and direction const angle = Math.atan2(vy, vx); const stretch = Math.min(currentSpeed * 0.0035, 0.32); // Max stretch 32% const baseScale = isPressed ? (isHovered ? 1.35 : 0.9) : (isHovered ? 1.55 : 1.0); const scaleX = baseScale * (1 + stretch); const scaleY = baseScale * (1 - stretch * 0.5); // Hardware accelerated transform with velocity alignment if (stretch > 0.02) { lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) rotate(${angle}rad) scale(${scaleX}, ${scaleY}) rotate(${-angle}rad)`; } else { lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) scale(${baseScale})`; } // Precision pinpoint dot (Desktop only, hidden on touch or hover) if (!isTouchActive) { const dotScale = isHovered ? 0 : (isPressed ? 0.7 : 1); dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`; } animId = requestAnimationFrame(loop); }; // Desktop Pointer Listeners window.addEventListener('mousemove', onMouseMove, { passive: true }); window.addEventListener('mouseleave', onMouseLeave); window.addEventListener('mouseenter', onMouseEnter); window.addEventListener('mousedown', onMouseDown); window.addEventListener('mouseup', onMouseUp); document.addEventListener('mouseover', onMouseOver, { passive: true }); // Touch Device Listeners window.addEventListener('touchstart', onTouchStart, { passive: true }); window.addEventListener('touchmove', onTouchMove, { passive: true }); window.addEventListener('touchend', onTouchEnd, { passive: true }); window.addEventListener('touchcancel', onTouchEnd, { passive: true }); animId = requestAnimationFrame(loop); return () => { cancelAnimationFrame(animId); if (fadeTimeout) clearTimeout(fadeTimeout); window.removeEventListener('mousemove', onMouseMove); window.removeEventListener('mouseleave', onMouseLeave); window.removeEventListener('mouseenter', onMouseEnter); window.removeEventListener('mousedown', onMouseDown); window.removeEventListener('mouseup', onMouseUp); document.removeEventListener('mouseover', onMouseOver); window.removeEventListener('touchstart', onTouchStart); window.removeEventListener('touchmove', onTouchMove); window.removeEventListener('touchend', onTouchEnd); window.removeEventListener('touchcancel', onTouchEnd); }; }, []); return (
{/* 1. Ambient Caustic Aura (Fluid blue light dispersion) */}
{/* 2. Pure Liquid Glass Lens (100% Crystal Transparent Body, Sharp Specular Glints, Zero Frosting) */}
{/* Primary Sharp Specular Glint (Simulates sunlight reflecting off curved water droplet meniscus) */}
{/* Pinpoint Micro-Glint (Sharp point-light reflection) */}
{/* Secondary Ambient Caustic Rim (Bottom-right rim reflection) */}
{/* 3. Precision Point Pip (Desktop Only, Zero Latency Pinpoint) */}
); }