Files
webb/src/components/magicui/smooth-cursor.tsx
T

245 lines
9.4 KiB
TypeScript

'use client';
import React, { useEffect, useRef, useState } from 'react';
/**
* LiquidGlassLensCursor:
* Pure Optical Liquid Glass Lens cursor with 100% transparent crystal body,
* sharp specular water-drop glints, dynamic velocity squash & stretch.
*
* Exclusively active on Desktop & Laptop computers with precise mouse / trackpad pointers.
* Completely disabled and removed on Phones, Tablets, and Touchscreen devices.
*/
export function SmoothCursor() {
const [isEnabled, setIsEnabled] = useState(false);
const lensRef = useRef<HTMLDivElement>(null);
const dotRef = useRef<HTMLDivElement>(null);
const causticRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (typeof window === 'undefined') return;
// Detect phones, tablets, or touchscreen devices
const isMobileOrTablet =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Tablet/i.test(navigator.userAgent) ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches) ||
(window.matchMedia && !window.matchMedia('(hover: hover)').matches) ||
('ontouchstart' in window && window.innerWidth < 1024) ||
(navigator.maxTouchPoints > 0 && window.innerWidth < 1024);
const hasFinePointer =
window.matchMedia && window.matchMedia('(hover: hover) and (pointer: fine)').matches;
// Completely disable and remove on phones and tablets
if (isMobileOrTablet || !hasFinePointer) {
return;
}
setIsEnabled(true);
}, []);
useEffect(() => {
if (!isEnabled) 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 animId = 0;
// Desktop Mouse & Pointer Movement
const onMouseMove = (e: MouseEvent) => {
mouseX = e.clientX;
mouseY = e.clientY;
if (!isVisible) {
isVisible = true;
lens.style.opacity = '1';
dot.style.opacity = '1';
if (caustic) caustic.style.opacity = '1';
}
};
const onMouseLeave = () => {
isVisible = false;
lens.style.opacity = '0';
dot.style.opacity = '0';
if (caustic) caustic.style.opacity = '0';
};
const onMouseEnter = () => {
isVisible = true;
lens.style.opacity = '1';
dot.style.opacity = '1';
if (caustic) caustic.style.opacity = '1';
};
const onMouseDown = () => {
isPressed = true;
};
const onMouseUp = () => {
isPressed = false;
};
// 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 Node | null;
if (!target) return;
const element = (target instanceof Element ? target : target.parentElement) as HTMLElement | null;
if (!element || typeof element.closest !== 'function') return;
const interactive = element.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 = () => {
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
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);
};
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 });
animId = requestAnimationFrame(loop);
return () => {
cancelAnimationFrame(animId);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseleave', onMouseLeave);
window.removeEventListener('mouseenter', onMouseEnter);
window.removeEventListener('mousedown', onMouseDown);
window.removeEventListener('mouseup', onMouseUp);
document.removeEventListener('mouseover', onMouseOver);
};
}, [isEnabled]);
// Completely removed on phones and tablets
if (!isEnabled) {
return null;
}
return (
<div className="pointer-events-none fixed inset-0 z-[99999] overflow-hidden select-none hidden md:block">
{/* 1. Ambient Caustic Aura (Fluid blue light dispersion) */}
<div
ref={causticRef}
className="fixed top-0 left-0 w-24 h-24 rounded-full pointer-events-none opacity-0 transition-opacity duration-300 will-change-transform transform-gpu [backface-visibility:hidden]"
style={{
background: 'radial-gradient(circle, rgba(56, 189, 248, 0.12) 0%, rgba(99, 102, 241, 0.05) 50%, transparent 70%)',
filter: 'blur(10px)',
}}
/>
{/* 2. Pure Liquid Glass Lens (100% Crystal Transparent Body, Sharp Specular Glints, Zero Frosting) */}
<div
ref={lensRef}
className="fixed top-0 left-0 w-12 h-12 rounded-full pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden] transition-opacity duration-200"
style={{
// Optical clear glass: ZERO blur (eliminates all frosted haze) + optical contrast & saturation pop
backdropFilter: 'contrast(1.16) brightness(1.05) saturate(1.28)',
WebkitBackdropFilter: 'contrast(1.16) brightness(1.05) saturate(1.28)',
// Ultra-sheer crystal body: 98% transparent center, subtle sapphire meniscus tint at the outer rim
background:
'radial-gradient(circle at 40% 35%, rgba(255, 255, 255, 0.12) 0%, rgba(255, 255, 255, 0.02) 40%, rgba(37, 99, 235, 0.04) 75%, rgba(37, 99, 235, 0.12) 100%)',
// Sharp liquid meniscus rim & caustic reflection
boxShadow:
'0 4px 20px -2px rgba(37, 99, 235, 0.22), 0 1px 4px rgba(15, 23, 42, 0.06), inset 0 1.5px 2px rgba(255, 255, 255, 0.85), inset 0 -1.5px 2.5px rgba(37, 99, 235, 0.35), inset 0 0 0 1px rgba(255, 255, 255, 0.45)',
border: '1px solid rgba(255, 255, 255, 0.55)',
}}
>
{/* Primary Sharp Specular Glint (Simulates sunlight reflecting off curved water droplet meniscus) */}
<div
className="absolute top-1.5 left-2 w-3.5 h-1.5 rounded-full pointer-events-none"
style={{
background: 'linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.4) 60%, transparent 100%)',
transform: 'rotate(-25deg)',
}}
/>
{/* Pinpoint Micro-Glint (Sharp point-light reflection) */}
<div
className="absolute top-1 left-2.5 w-1 h-1 rounded-full bg-white pointer-events-none shadow-[0_0_2px_rgba(255,255,255,0.95)]"
/>
{/* Secondary Ambient Caustic Rim (Bottom-right rim reflection) */}
<div
className="absolute bottom-1 right-2 w-2.5 h-1 rounded-full pointer-events-none opacity-80"
style={{
background: 'linear-gradient(315deg, rgba(56, 189, 248, 0.7) 0%, rgba(99, 102, 241, 0.2) 100%)',
transform: 'rotate(-25deg)',
}}
/>
</div>
{/* 3. Precision Point Pip (Desktop Only, Zero Latency Pinpoint) */}
<div
ref={dotRef}
className="fixed top-0 left-0 w-1.5 h-1.5 rounded-full bg-blue-600 shadow-[0_0_8px_rgba(37,99,235,0.9)] pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden] transition-opacity duration-150"
/>
</div>
);
}