feat(ui): eliminate empty space, add categories to squarical trendy effects, optimize 120fps animations and smooth cursor

This commit is contained in:
2026-11-04 18:37:52 +05:30
parent d2f377dd67
commit d9ef1497a3
8 changed files with 357 additions and 272 deletions
+65 -67
View File
@@ -9,42 +9,83 @@ interface ParticlesProps {
staticity?: number;
ease?: number;
size?: number;
refresh?: boolean;
color?: string;
vx?: number;
vy?: number;
}
export function Particles({
className = '',
quantity = 40,
quantity = 35,
staticity = 50,
ease = 50,
size = 0.5,
refresh = false,
size = 0.6,
color = '#3b82f6',
vx = 0,
vy = 0,
}: ParticlesProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const canvasContainerRef = useRef<HTMLDivElement>(null);
const context = useRef<CanvasRenderingContext2D | null>(null);
const circles = useRef<any[]>([]);
const mousePosition = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
const mouse = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
const canvasSize = useRef<{ w: number; h: number }>({ w: 0, h: 0 });
const isVisibleRef = useRef<boolean>(true);
const dpr = typeof window !== 'undefined' ? Math.min(window.devicePixelRatio || 1, 2) : 1;
useEffect(() => {
if (canvasRef.current) {
context.current = canvasRef.current.getContext('2d');
}
const container = canvasContainerRef.current;
if (!container || !canvasRef.current) return;
context.current = canvasRef.current.getContext('2d');
initCanvas();
animate();
window.addEventListener('resize', initCanvas);
// IntersectionObserver to pause loop when scrolled out of view!
let animId = 0;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
isVisibleRef.current = entry.isIntersecting;
});
},
{ threshold: 0.05 }
);
observer.observe(container);
const animate = () => {
if (isVisibleRef.current && context.current) {
clearContext();
circles.current.forEach((circle: any) => {
const edge = [
circle.x + circle.size < 0,
circle.x - circle.size > canvasSize.current.w,
circle.y + circle.size < 0,
circle.y - circle.size > canvasSize.current.h,
];
if (edge[0] || edge[1]) circle.dx = -circle.dx;
if (edge[2] || edge[3]) circle.dy = -circle.dy;
circle.x += circle.dx;
circle.y += circle.dy;
if (circle.alpha < circle.targetAlpha) {
circle.alpha += 0.015;
}
drawCircle(circle, true);
});
}
animId = window.requestAnimationFrame(animate);
};
animId = window.requestAnimationFrame(animate);
const handleResize = () => {
resizeCanvas();
drawParticles();
};
window.addEventListener('resize', handleResize, { passive: true });
return () => {
window.removeEventListener('resize', initCanvas);
cancelAnimationFrame(animId);
observer.disconnect();
window.removeEventListener('resize', handleResize);
};
}, [color]);
@@ -69,38 +110,29 @@ export function Particles({
const circleParams = (): any => {
const x = Math.floor(Math.random() * canvasSize.current.w);
const y = Math.floor(Math.random() * canvasSize.current.h);
const translateX = 0;
const translateY = 0;
const pSize = Math.floor(Math.random() * 2) + size;
const alpha = 0;
const targetAlpha = parseFloat((Math.random() * 0.5 + 0.1).toFixed(1));
const dx = (Math.random() - 0.5) * 0.2;
const dy = (Math.random() - 0.5) * 0.2;
const magnetism = 0.1 + Math.random() * 4;
const targetAlpha = parseFloat((Math.random() * 0.4 + 0.1).toFixed(2));
const dx = (Math.random() - 0.5) * 0.18;
const dy = (Math.random() - 0.5) * 0.18;
return {
x,
y,
translateX,
translateY,
size: pSize,
alpha,
alpha: 0,
targetAlpha,
dx,
dy,
magnetism,
};
};
const drawCircle = (circle: any, update = false) => {
if (context.current) {
const { x, y, translateX, translateY, size: pSize, alpha } = circle;
context.current.translate(translateX, translateY);
const { x, y, size: pSize, alpha } = circle;
context.current.beginPath();
context.current.arc(x, y, pSize, 0, 2 * Math.PI);
context.current.fillStyle = color;
context.current.globalAlpha = alpha;
context.current.fill();
context.current.setTransform(dpr, 0, 0, dpr, 0, 0);
if (!update) {
circles.current.push(circle);
@@ -110,51 +142,17 @@ export function Particles({
const drawParticles = () => {
clearContext();
const particleCount = quantity;
for (let i = 0; i < particleCount; i++) {
const circle = circleParams();
drawCircle(circle);
for (let i = 0; i < quantity; i++) {
drawCircle(circleParams());
}
};
const clearContext = () => {
if (context.current) {
context.current.clearRect(
0,
0,
canvasSize.current.w,
canvasSize.current.h
);
context.current.clearRect(0, 0, canvasSize.current.w, canvasSize.current.h);
}
};
const animate = () => {
clearContext();
circles.current.forEach((circle: any, i: number) => {
// Handle edge collision
const edge = [
circle.x + circle.translateX - circle.size < 0,
circle.x + circle.translateX + circle.size > canvasSize.current.w,
circle.y + circle.translateY - circle.size < 0,
circle.y + circle.translateY + circle.size > canvasSize.current.h,
];
if (edge[0] || edge[1]) circle.dx = -circle.dx;
if (edge[2] || edge[3]) circle.dy = -circle.dy;
circle.x += circle.dx;
circle.y += circle.dy;
circle.translateX += ((mouse.current.x / (staticity / circle.magnetism)) - circle.translateX) / ease;
circle.translateY += ((mouse.current.y / (staticity / circle.magnetism)) - circle.translateY) / ease;
if (circle.alpha < circle.targetAlpha) {
circle.alpha += 0.02;
}
drawCircle(circle, true);
});
window.requestAnimationFrame(animate);
};
return (
<div className={cn('pointer-events-none', className)} ref={canvasContainerRef} aria-hidden="true">
<canvas ref={canvasRef} />
+89 -69
View File
@@ -1,103 +1,123 @@
'use client';
import React, { useEffect, useState } from 'react';
import { motion, useMotionValue, useSpring } from 'framer-motion';
import React, { useEffect, useRef } from 'react';
export function SmoothCursor() {
const [mounted, setMounted] = useState(false);
const [isHovered, setIsHovered] = useState(false);
const [isVisible, setIsVisible] = useState(false);
const mouseX = useMotionValue(-100);
const mouseY = useMotionValue(-100);
// Smooth spring physics for luxurious trailing movement
const springConfig = { damping: 28, stiffness: 350, mass: 0.5 };
const cursorX = useSpring(mouseX, springConfig);
const cursorY = useSpring(mouseY, springConfig);
const cursorDotRef = useRef<HTMLDivElement>(null);
const cursorRingRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// Disable on touch devices
// Only on non-touch devices
if (typeof window === 'undefined') return;
const isTouch = window.matchMedia('(pointer: coarse)').matches;
if (isTouch) return;
setMounted(true);
const dot = cursorDotRef.current;
const ring = cursorRingRef.current;
if (!dot || !ring) return;
const handleMouseMove = (e: MouseEvent) => {
mouseX.set(e.clientX);
mouseY.set(e.clientY);
if (!isVisible) setIsVisible(true);
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';
}
};
const handleMouseLeave = () => {
setIsVisible(false);
const onMouseLeave = () => {
isVisible = false;
dot.style.opacity = '0';
ring.style.opacity = '0';
};
const handleMouseEnter = () => {
setIsVisible(true);
const onMouseEnter = () => {
isVisible = true;
dot.style.opacity = '1';
ring.style.opacity = '1';
};
// Detect clickable/interactive hover targets
const handleMouseOver = (e: MouseEvent) => {
// Throttled interactive element detection
let lastCheck = 0;
const onMouseOver = (e: MouseEvent) => {
const now = performance.now();
if (now - lastCheck < 30) return;
lastCheck = now;
const target = e.target as HTMLElement | null;
if (!target) return;
const isInteractive = target.closest(
'a, button, input, textarea, select, [role="button"], .cursor-pointer'
);
setIsHovered(!!isInteractive);
const interactive = target.closest('a, button, input, textarea, select, [role="button"], .cursor-pointer');
isHovered = !!interactive;
if (isHovered) {
ring.style.width = '52px';
ring.style.height = '52px';
ring.style.borderColor = 'rgba(37, 99, 235, 0.7)';
ring.style.backgroundColor = 'rgba(37, 99, 235, 0.12)';
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) scale(0)`;
} else {
ring.style.width = '30px';
ring.style.height = '30px';
ring.style.borderColor = 'rgba(37, 99, 235, 0.4)';
ring.style.backgroundColor = 'rgba(37, 99, 235, 0.05)';
}
};
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseleave', handleMouseLeave);
window.addEventListener('mouseenter', handleMouseEnter);
document.addEventListener('mouseover', handleMouseOver);
// Ultra-smooth 120fps/60fps RAF lerp loop (Zero React re-renders!)
const loop = () => {
// Smooth linear interpolation for trailing ring
ringX += (mouseX - ringX) * 0.22;
ringY += (mouseY - ringY) * 0.22;
ring.style.transform = `translate3d(${ringX}px, ${ringY}px, 0) translate(-50%, -50%)`;
if (!isHovered) {
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate(-50%, -50%) scale(1)`;
}
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);
return () => {
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('mouseleave', handleMouseLeave);
window.removeEventListener('mouseenter', handleMouseEnter);
document.removeEventListener('mouseover', handleMouseOver);
cancelAnimationFrame(animId);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseleave', onMouseLeave);
window.removeEventListener('mouseenter', onMouseEnter);
document.removeEventListener('mouseover', onMouseOver);
};
}, [mouseX, mouseY, isVisible]);
if (!mounted || !isVisible) return null;
}, []);
return (
<div className="pointer-events-none fixed inset-0 z-[9999] overflow-hidden select-none">
{/* Outer fluid halo aura */}
<motion.div
className="fixed top-0 left-0 rounded-full border border-blue-500/40 bg-blue-500/10 backdrop-blur-[1px]"
{/* Trailing Fluid Squircle Ring */}
<div
ref={cursorRingRef}
className="fixed top-0 left-0 rounded-full border border-blue-500/40 bg-blue-500/5 backdrop-blur-[0.5px] pointer-events-none opacity-0 transition-[width,height,background-color,border-color] duration-200 ease-out will-change-transform"
style={{
x: cursorX,
y: cursorY,
translateX: '-50%',
translateY: '-50%',
width: '30px',
height: '30px',
}}
animate={{
width: isHovered ? 48 : 28,
height: isHovered ? 48 : 28,
scale: isHovered ? 1.2 : 1,
borderColor: isHovered ? 'rgba(59, 130, 246, 0.7)' : 'rgba(59, 130, 246, 0.35)',
backgroundColor: isHovered ? 'rgba(59, 130, 246, 0.18)' : 'rgba(59, 130, 246, 0.08)',
}}
transition={{ type: 'spring', damping: 25, stiffness: 350 }}
/>
{/* Inner sharp precision dot */}
<motion.div
className="fixed top-0 left-0 w-2 h-2 rounded-full bg-blue-600 shadow-sm"
style={{
x: mouseX,
y: mouseY,
translateX: '-50%',
translateY: '-50%',
}}
animate={{
scale: isHovered ? 0 : 1,
opacity: isHovered ? 0 : 1,
}}
transition={{ duration: 0.15 }}
{/* Precision Dot */}
<div
ref={cursorDotRef}
className="fixed top-0 left-0 w-2 h-2 rounded-full bg-blue-600 shadow-sm pointer-events-none opacity-0 transition-transform duration-100 ease-out will-change-transform"
/>
</div>
);