2026-11-04 17:55:18 +05:30
|
|
|
'use client';
|
|
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
import React, { useEffect, useRef } from 'react';
|
2026-11-04 17:55:18 +05:30
|
|
|
|
|
|
|
|
export function SmoothCursor() {
|
2026-11-04 18:37:52 +05:30
|
|
|
const cursorDotRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const cursorRingRef = useRef<HTMLDivElement>(null);
|
2026-11-04 17:55:18 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-11-04 18:37:52 +05:30
|
|
|
// Only on non-touch devices
|
2026-11-04 17:55:18 +05:30
|
|
|
if (typeof window === 'undefined') return;
|
|
|
|
|
const isTouch = window.matchMedia('(pointer: coarse)').matches;
|
|
|
|
|
if (isTouch) return;
|
|
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
const dot = cursorDotRef.current;
|
|
|
|
|
const ring = cursorRingRef.current;
|
|
|
|
|
if (!dot || !ring) return;
|
2026-11-04 17:55:18 +05:30
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
let mouseX = -100;
|
|
|
|
|
let mouseY = -100;
|
|
|
|
|
let ringX = -100;
|
|
|
|
|
let ringY = -100;
|
|
|
|
|
let isHovered = false;
|
|
|
|
|
let isVisible = false;
|
|
|
|
|
let animId = 0;
|
|
|
|
|
|
|
|
|
|
const onMouseMove = (e: MouseEvent) => {
|
|
|
|
|
mouseX = e.clientX;
|
|
|
|
|
mouseY = e.clientY;
|
|
|
|
|
if (!isVisible) {
|
|
|
|
|
isVisible = true;
|
|
|
|
|
dot.style.opacity = '1';
|
|
|
|
|
ring.style.opacity = '1';
|
|
|
|
|
}
|
2026-11-04 17:55:18 +05:30
|
|
|
};
|
|
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
const onMouseLeave = () => {
|
|
|
|
|
isVisible = false;
|
|
|
|
|
dot.style.opacity = '0';
|
|
|
|
|
ring.style.opacity = '0';
|
2026-11-04 17:55:18 +05:30
|
|
|
};
|
|
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
const onMouseEnter = () => {
|
|
|
|
|
isVisible = true;
|
|
|
|
|
dot.style.opacity = '1';
|
|
|
|
|
ring.style.opacity = '1';
|
2026-11-04 17:55:18 +05:30
|
|
|
};
|
|
|
|
|
|
2026-11-04 18:37:52 +05:30
|
|
|
// Throttled interactive element detection
|
|
|
|
|
let lastCheck = 0;
|
|
|
|
|
const onMouseOver = (e: MouseEvent) => {
|
|
|
|
|
const now = performance.now();
|
|
|
|
|
if (now - lastCheck < 30) return;
|
|
|
|
|
lastCheck = now;
|
|
|
|
|
|
2026-11-04 17:55:18 +05:30
|
|
|
const target = e.target as HTMLElement | null;
|
|
|
|
|
if (!target) return;
|
2026-11-04 18:37:52 +05:30
|
|
|
const interactive = target.closest('a, button, input, textarea, select, [role="button"], .cursor-pointer');
|
2026-11-04 18:48:24 +05:30
|
|
|
const nextHovered = !!interactive;
|
2026-11-04 18:37:52 +05:30
|
|
|
|
2026-11-04 18:48:24 +05:30
|
|
|
if (nextHovered !== isHovered) {
|
|
|
|
|
isHovered = nextHovered;
|
|
|
|
|
if (isHovered) {
|
|
|
|
|
ring.style.borderColor = 'rgba(37, 99, 235, 0.7)';
|
|
|
|
|
ring.style.backgroundColor = 'rgba(37, 99, 235, 0.12)';
|
|
|
|
|
} else {
|
|
|
|
|
ring.style.borderColor = 'rgba(37, 99, 235, 0.35)';
|
|
|
|
|
ring.style.backgroundColor = 'rgba(37, 99, 235, 0.04)';
|
|
|
|
|
}
|
2026-11-04 18:37:52 +05:30
|
|
|
}
|
2026-11-04 17:55:18 +05:30
|
|
|
};
|
|
|
|
|
|
2026-11-04 18:48:24 +05:30
|
|
|
// Locked 60-120fps RAF lerp loop (Zero React re-renders, 100% GPU composited transforms)
|
2026-11-04 18:37:52 +05:30
|
|
|
const loop = () => {
|
|
|
|
|
// Smooth linear interpolation for trailing ring
|
|
|
|
|
ringX += (mouseX - ringX) * 0.22;
|
|
|
|
|
ringY += (mouseY - ringY) * 0.22;
|
|
|
|
|
|
2026-11-04 18:48:24 +05:30
|
|
|
const ringScale = isHovered ? 1.45 : 1;
|
|
|
|
|
ring.style.transform = `translate3d(${ringX}px, ${ringY}px, 0) translate3d(-50%, -50%, 0) scale(${ringScale})`;
|
|
|
|
|
|
|
|
|
|
const dotScale = isHovered ? 0 : 1;
|
|
|
|
|
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`;
|
2026-11-04 18:37:52 +05:30
|
|
|
|
|
|
|
|
animId = requestAnimationFrame(loop);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('mousemove', onMouseMove, { passive: true });
|
|
|
|
|
window.addEventListener('mouseleave', onMouseLeave);
|
|
|
|
|
window.addEventListener('mouseenter', onMouseEnter);
|
|
|
|
|
document.addEventListener('mouseover', onMouseOver, { passive: true });
|
|
|
|
|
|
|
|
|
|
animId = requestAnimationFrame(loop);
|
2026-11-04 17:55:18 +05:30
|
|
|
|
|
|
|
|
return () => {
|
2026-11-04 18:37:52 +05:30
|
|
|
cancelAnimationFrame(animId);
|
|
|
|
|
window.removeEventListener('mousemove', onMouseMove);
|
|
|
|
|
window.removeEventListener('mouseleave', onMouseLeave);
|
|
|
|
|
window.removeEventListener('mouseenter', onMouseEnter);
|
|
|
|
|
document.removeEventListener('mouseover', onMouseOver);
|
2026-11-04 17:55:18 +05:30
|
|
|
};
|
2026-11-04 18:37:52 +05:30
|
|
|
}, []);
|
2026-11-04 17:55:18 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="pointer-events-none fixed inset-0 z-[9999] overflow-hidden select-none">
|
2026-11-04 18:48:24 +05:30
|
|
|
{/* Trailing Fluid Squircle Ring (GPU-scale transform only, zero layout reflows) */}
|
2026-11-04 18:37:52 +05:30
|
|
|
<div
|
|
|
|
|
ref={cursorRingRef}
|
2026-11-04 18:48:24 +05:30
|
|
|
className="fixed top-0 left-0 w-8 h-8 rounded-full border border-blue-500/35 bg-blue-500/4 pointer-events-none opacity-0 transition-[border-color,background-color] duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] will-change-transform transform-gpu [backface-visibility:hidden]"
|
2026-11-04 17:55:18 +05:30
|
|
|
/>
|
|
|
|
|
|
2026-11-04 18:48:24 +05:30
|
|
|
{/* Precision Dot (GPU-scale transform only) */}
|
2026-11-04 18:37:52 +05:30
|
|
|
<div
|
|
|
|
|
ref={cursorDotRef}
|
2026-11-04 18:48:24 +05:30
|
|
|
className="fixed top-0 left-0 w-2 h-2 rounded-full bg-blue-600 shadow-sm pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden]"
|
2026-11-04 17:55:18 +05:30
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|