fix(cursor): make liquid glass lens crystal transparent and universally responsive across touch and desktop
This commit is contained in:
@@ -4,17 +4,16 @@ import React, { useEffect, useRef } from 'react';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* LiquidGlassLensCursor:
|
* LiquidGlassLensCursor:
|
||||||
* An optical, fluid liquid glass lens cursor that dynamically magnifies, refracts,
|
* Pure Optical Liquid Glass Lens cursor with 100% transparent crystal body,
|
||||||
* and reflects light across all content as you glide over the website.
|
* sharp specular water-drop glints, dynamic velocity squash & stretch,
|
||||||
|
* and universal responsiveness across all devices (Desktop, Mobile, Tablet, Trackpad).
|
||||||
*
|
*
|
||||||
* Capabilities:
|
* Key Capabilities:
|
||||||
* - Real optical magnification & refraction with GPU backdrop-filter
|
* - Ultra-transparent liquid glass (Zero frosting, zero milky haze, pure optical clarity)
|
||||||
* - Fluid hydrodynamic squash & stretch deformation based on pointer velocity vector
|
* - Sharp meniscus rim & caustic reflections matching brand theme (#2563EB, #06B6D4)
|
||||||
* - Multi-layer specular convex reflections & iridescent caustic rim
|
* - True multi-device responsiveness: Mouse on desktop + fluid touch lens on mobile/tablets
|
||||||
* - Organic expansion when hovering over interactive elements (cards, buttons, links, media)
|
* - Hardware-accelerated GPU transforms (locked 60-120fps, zero layout reflows)
|
||||||
* - Click surface tension compression feedback
|
* - Strictly pointer-events-none: never blocks taps, clicks, scrolls, or dragging
|
||||||
* - Zero CPU layout reflows (100% GPU composited translate3d and scale transforms)
|
|
||||||
* - Strict pointer-events-none (never disrupts interactions, dragging, or text selection)
|
|
||||||
*/
|
*/
|
||||||
export function SmoothCursor() {
|
export function SmoothCursor() {
|
||||||
const lensRef = useRef<HTMLDivElement>(null);
|
const lensRef = useRef<HTMLDivElement>(null);
|
||||||
@@ -22,10 +21,7 @@ export function SmoothCursor() {
|
|||||||
const causticRef = useRef<HTMLDivElement>(null);
|
const causticRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only on non-touch devices with fine pointers
|
|
||||||
if (typeof window === 'undefined') return;
|
if (typeof window === 'undefined') return;
|
||||||
const isTouch = window.matchMedia('(pointer: coarse)').matches;
|
|
||||||
if (isTouch) return;
|
|
||||||
|
|
||||||
const lens = lensRef.current;
|
const lens = lensRef.current;
|
||||||
const dot = dotRef.current;
|
const dot = dotRef.current;
|
||||||
@@ -46,11 +42,17 @@ export function SmoothCursor() {
|
|||||||
let isHovered = false;
|
let isHovered = false;
|
||||||
let isPressed = false;
|
let isPressed = false;
|
||||||
let isVisible = false;
|
let isVisible = false;
|
||||||
|
let isTouchActive = false;
|
||||||
|
let fadeTimeout: NodeJS.Timeout | null = null;
|
||||||
let animId = 0;
|
let animId = 0;
|
||||||
|
|
||||||
|
// 1. Mouse & Pointer Movement (Desktop & Trackpads)
|
||||||
const onMouseMove = (e: MouseEvent) => {
|
const onMouseMove = (e: MouseEvent) => {
|
||||||
|
isTouchActive = false;
|
||||||
mouseX = e.clientX;
|
mouseX = e.clientX;
|
||||||
mouseY = e.clientY;
|
mouseY = e.clientY;
|
||||||
|
if (fadeTimeout) clearTimeout(fadeTimeout);
|
||||||
|
|
||||||
if (!isVisible) {
|
if (!isVisible) {
|
||||||
isVisible = true;
|
isVisible = true;
|
||||||
lens.style.opacity = '1';
|
lens.style.opacity = '1';
|
||||||
@@ -60,6 +62,7 @@ export function SmoothCursor() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onMouseLeave = () => {
|
const onMouseLeave = () => {
|
||||||
|
if (isTouchActive) return;
|
||||||
isVisible = false;
|
isVisible = false;
|
||||||
lens.style.opacity = '0';
|
lens.style.opacity = '0';
|
||||||
dot.style.opacity = '0';
|
dot.style.opacity = '0';
|
||||||
@@ -67,6 +70,7 @@ export function SmoothCursor() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onMouseEnter = () => {
|
const onMouseEnter = () => {
|
||||||
|
if (isTouchActive) return;
|
||||||
isVisible = true;
|
isVisible = true;
|
||||||
lens.style.opacity = '1';
|
lens.style.opacity = '1';
|
||||||
dot.style.opacity = '1';
|
dot.style.opacity = '1';
|
||||||
@@ -81,7 +85,50 @@ export function SmoothCursor() {
|
|||||||
isPressed = false;
|
isPressed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Throttled interactive element detection for snappy magnetic lens response
|
// 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;
|
let lastCheck = 0;
|
||||||
const onMouseOver = (e: MouseEvent) => {
|
const onMouseOver = (e: MouseEvent) => {
|
||||||
const now = performance.now();
|
const now = performance.now();
|
||||||
@@ -96,19 +143,18 @@ export function SmoothCursor() {
|
|||||||
|
|
||||||
// High performance RAF loop (locked 60-120fps, 100% GPU composited)
|
// High performance RAF loop (locked 60-120fps, 100% GPU composited)
|
||||||
const loop = () => {
|
const loop = () => {
|
||||||
// Calculate velocity vector of mouse movement
|
// Calculate velocity vector of pointer movement
|
||||||
const vx = mouseX - prevMouseX;
|
const vx = mouseX - prevMouseX;
|
||||||
const vy = mouseY - prevMouseY;
|
const vy = mouseY - prevMouseY;
|
||||||
prevMouseX = mouseX;
|
prevMouseX = mouseX;
|
||||||
prevMouseY = mouseY;
|
prevMouseY = mouseY;
|
||||||
|
|
||||||
const rawSpeed = Math.hypot(vx, vy);
|
const rawSpeed = Math.hypot(vx, vy);
|
||||||
// Low-pass filter for silky organic fluid momentum
|
|
||||||
currentSpeed += (rawSpeed - currentSpeed) * 0.2;
|
currentSpeed += (rawSpeed - currentSpeed) * 0.2;
|
||||||
|
|
||||||
// Smooth lag for liquid lens (organic water droplet following behavior)
|
// Silky liquid lag for pure organic fluid following
|
||||||
lensX += (mouseX - lensX) * 0.32;
|
lensX += (mouseX - lensX) * 0.35;
|
||||||
lensY += (mouseY - lensY) * 0.32;
|
lensY += (mouseY - lensY) * 0.35;
|
||||||
|
|
||||||
// Secondary ambient caustic trailing slightly behind
|
// Secondary ambient caustic trailing slightly behind
|
||||||
if (caustic) {
|
if (caustic) {
|
||||||
@@ -121,7 +167,7 @@ export function SmoothCursor() {
|
|||||||
const angle = Math.atan2(vy, vx);
|
const angle = Math.atan2(vy, vx);
|
||||||
const stretch = Math.min(currentSpeed * 0.0035, 0.32); // Max stretch 32%
|
const stretch = Math.min(currentSpeed * 0.0035, 0.32); // Max stretch 32%
|
||||||
|
|
||||||
const baseScale = isPressed ? (isHovered ? 1.35 : 0.88) : (isHovered ? 1.6 : 1.0);
|
const baseScale = isPressed ? (isHovered ? 1.35 : 0.9) : (isHovered ? 1.55 : 1.0);
|
||||||
const scaleX = baseScale * (1 + stretch);
|
const scaleX = baseScale * (1 + stretch);
|
||||||
const scaleY = baseScale * (1 - stretch * 0.5);
|
const scaleY = baseScale * (1 - stretch * 0.5);
|
||||||
|
|
||||||
@@ -132,13 +178,16 @@ export function SmoothCursor() {
|
|||||||
lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) scale(${baseScale})`;
|
lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) scale(${baseScale})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zero-latency precision dot (instant pinpoint at mouse coordinates)
|
// Precision pinpoint dot (Desktop only, hidden on touch or hover)
|
||||||
const dotScale = isHovered ? 0 : (isPressed ? 0.7 : 1);
|
if (!isTouchActive) {
|
||||||
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`;
|
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);
|
animId = requestAnimationFrame(loop);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Desktop Pointer Listeners
|
||||||
window.addEventListener('mousemove', onMouseMove, { passive: true });
|
window.addEventListener('mousemove', onMouseMove, { passive: true });
|
||||||
window.addEventListener('mouseleave', onMouseLeave);
|
window.addEventListener('mouseleave', onMouseLeave);
|
||||||
window.addEventListener('mouseenter', onMouseEnter);
|
window.addEventListener('mouseenter', onMouseEnter);
|
||||||
@@ -146,70 +195,85 @@ export function SmoothCursor() {
|
|||||||
window.addEventListener('mouseup', onMouseUp);
|
window.addEventListener('mouseup', onMouseUp);
|
||||||
document.addEventListener('mouseover', onMouseOver, { passive: true });
|
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);
|
animId = requestAnimationFrame(loop);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
cancelAnimationFrame(animId);
|
cancelAnimationFrame(animId);
|
||||||
|
if (fadeTimeout) clearTimeout(fadeTimeout);
|
||||||
window.removeEventListener('mousemove', onMouseMove);
|
window.removeEventListener('mousemove', onMouseMove);
|
||||||
window.removeEventListener('mouseleave', onMouseLeave);
|
window.removeEventListener('mouseleave', onMouseLeave);
|
||||||
window.removeEventListener('mouseenter', onMouseEnter);
|
window.removeEventListener('mouseenter', onMouseEnter);
|
||||||
window.removeEventListener('mousedown', onMouseDown);
|
window.removeEventListener('mousedown', onMouseDown);
|
||||||
window.removeEventListener('mouseup', onMouseUp);
|
window.removeEventListener('mouseup', onMouseUp);
|
||||||
document.removeEventListener('mouseover', onMouseOver);
|
document.removeEventListener('mouseover', onMouseOver);
|
||||||
|
|
||||||
|
window.removeEventListener('touchstart', onTouchStart);
|
||||||
|
window.removeEventListener('touchmove', onTouchMove);
|
||||||
|
window.removeEventListener('touchend', onTouchEnd);
|
||||||
|
window.removeEventListener('touchcancel', onTouchEnd);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pointer-events-none fixed inset-0 z-[99999] overflow-hidden select-none">
|
<div className="pointer-events-none fixed inset-0 z-[99999] overflow-hidden select-none">
|
||||||
{/* 1. Ambient Caustic Aura (Fluid light dispersion) */}
|
{/* 1. Ambient Caustic Aura (Fluid blue light dispersion) */}
|
||||||
<div
|
<div
|
||||||
ref={causticRef}
|
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]"
|
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={{
|
style={{
|
||||||
background: 'radial-gradient(circle, rgba(56, 189, 248, 0.16) 0%, rgba(99, 102, 241, 0.08) 45%, transparent 72%)',
|
background: 'radial-gradient(circle, rgba(56, 189, 248, 0.12) 0%, rgba(99, 102, 241, 0.05) 50%, transparent 70%)',
|
||||||
filter: 'blur(8px)',
|
filter: 'blur(10px)',
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* 2. The Liquid Glass Lens (Refractive Convex Optical Glass Bubble) */}
|
{/* 2. Pure Liquid Glass Lens (100% Crystal Transparent Body, Sharp Specular Glints, Zero Frosting) */}
|
||||||
<div
|
<div
|
||||||
ref={lensRef}
|
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"
|
className="fixed top-0 left-0 w-11 h-11 sm:w-12 sm:h-12 rounded-full pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden] transition-opacity duration-200"
|
||||||
style={{
|
style={{
|
||||||
// Optical glass refraction & magnification filter (crystal clarity with heightened contrast & saturation)
|
// Optical clear glass: ZERO blur (eliminates all frosted haze) + optical contrast & saturation pop
|
||||||
backdropFilter: 'blur(0.8px) contrast(1.24) brightness(1.1) saturate(1.4)',
|
backdropFilter: 'contrast(1.16) brightness(1.05) saturate(1.28)',
|
||||||
WebkitBackdropFilter: 'blur(0.8px) contrast(1.24) brightness(1.1) saturate(1.4)',
|
WebkitBackdropFilter: 'contrast(1.16) brightness(1.05) saturate(1.28)',
|
||||||
// Multi-layer specular convex glass gradient
|
// Ultra-sheer crystal body: 98% transparent center, subtle sapphire meniscus tint at the outer rim
|
||||||
background:
|
background:
|
||||||
'radial-gradient(circle at 35% 28%, rgba(255, 255, 255, 0.72) 0%, rgba(255, 255, 255, 0.22) 32%, rgba(255, 255, 255, 0) 65%), linear-gradient(135deg, rgba(255, 255, 255, 0.24) 0%, rgba(56, 189, 248, 0.08) 45%, rgba(99, 102, 241, 0.12) 100%)',
|
'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%)',
|
||||||
// Refractive caustic bevel, specular highlight rim & prismatic drop shadow
|
// Sharp liquid meniscus rim & caustic reflection
|
||||||
boxShadow:
|
boxShadow:
|
||||||
'0 8px 32px -4px rgba(37, 99, 235, 0.28), 0 2px 10px rgba(15, 23, 42, 0.08), inset 0 1.5px 2.5px rgba(255, 255, 255, 0.95), inset 0 -1.5px 3px rgba(37, 99, 235, 0.35), inset 0 0 0 1px rgba(255, 255, 255, 0.65)',
|
'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.6)',
|
border: '1px solid rgba(255, 255, 255, 0.55)',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{/* Curved Specular Reflection Glint (Simulates spherical glass refraction curvature) */}
|
{/* Primary Sharp Specular Glint (Simulates sunlight reflecting off curved water droplet meniscus) */}
|
||||||
<div
|
<div
|
||||||
className="absolute top-1.5 left-2 w-4 h-1.5 rounded-full pointer-events-none"
|
className="absolute top-1.5 left-2 w-3.5 h-1.5 rounded-full pointer-events-none"
|
||||||
style={{
|
style={{
|
||||||
background: 'linear-gradient(to right, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.25))',
|
background: 'linear-gradient(135deg, rgba(255, 255, 255, 0.95) 0%, rgba(255, 255, 255, 0.4) 60%, transparent 100%)',
|
||||||
transform: 'rotate(-25deg)',
|
transform: 'rotate(-25deg)',
|
||||||
filter: 'blur(0.2px)',
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Secondary Caustic Rim Reflection */}
|
{/* 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
|
<div
|
||||||
className="absolute bottom-1.5 right-2 w-2.5 h-1 rounded-full pointer-events-none opacity-70"
|
className="absolute bottom-1 right-2 w-2.5 h-1 rounded-full pointer-events-none opacity-80"
|
||||||
style={{
|
style={{
|
||||||
background: 'linear-gradient(to left, rgba(147, 197, 253, 0.8), rgba(199, 210, 254, 0.25))',
|
background: 'linear-gradient(315deg, rgba(56, 189, 248, 0.7) 0%, rgba(99, 102, 241, 0.2) 100%)',
|
||||||
transform: 'rotate(-25deg)',
|
transform: 'rotate(-25deg)',
|
||||||
filter: 'blur(0.3px)',
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 3. Ultra-Crisp Precision Dot (Zero Latency Pinpoint) */}
|
{/* 3. Precision Point Pip (Desktop Only, Zero Latency Pinpoint) */}
|
||||||
<div
|
<div
|
||||||
ref={dotRef}
|
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"
|
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"
|
||||||
|
|||||||
Reference in New Issue
Block a user