-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual-effects.tsx
More file actions
93 lines (80 loc) · 2.96 KB
/
visual-effects.tsx
File metadata and controls
93 lines (80 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use client';
import React, { useEffect, useRef, useCallback } from 'react';
// Interactive glowing bubbles component
export const GlowingBubbles = () => {
return null;
};
// Interactive grid background with high performance
export const InteractiveGrid = () => {
const gridRef = useRef<HTMLDivElement>(null);
const pointerActive = useRef(false);
const lastPos = useRef({ x: 0, y: 0 });
const rafId = useRef<number | null>(null);
// Animation loop for smoothness
const animate = useCallback(() => {
if (!pointerActive.current || !gridRef.current) return;
gridRef.current.style.setProperty('--x-pos', `${lastPos.current.x}px`);
gridRef.current.style.setProperty('--y-pos', `${lastPos.current.y}px`);
rafId.current = requestAnimationFrame(animate);
}, []);
useEffect(() => {
const grid = gridRef.current;
if (!grid) return;
const handlePointerMove = (e: PointerEvent) => {
const rect = grid.getBoundingClientRect();
lastPos.current.x = e.clientX - rect.left;
lastPos.current.y = e.clientY - rect.top;
};
const handlePointerEnter = () => {
pointerActive.current = true;
if (!rafId.current) rafId.current = requestAnimationFrame(animate);
};
const handlePointerLeave = () => {
pointerActive.current = false;
if (rafId.current) {
cancelAnimationFrame(rafId.current);
rafId.current = null;
}
};
grid.addEventListener('pointermove', handlePointerMove);
grid.addEventListener('pointerenter', handlePointerEnter);
grid.addEventListener('pointerleave', handlePointerLeave);
return () => {
grid.removeEventListener('pointermove', handlePointerMove);
grid.removeEventListener('pointerenter', handlePointerEnter);
grid.removeEventListener('pointerleave', handlePointerLeave);
if (rafId.current) {
cancelAnimationFrame(rafId.current);
rafId.current = null;
}
};
}, [animate]);
return (
<div
ref={gridRef}
className="fixed inset-0 z-0 pointer-events-none bg-grid-light dark:bg-grid-dark bg-[length:50px_50px] opacity-40 dark:opacity-20 will-change-[background-position]"
style={{
backgroundImage: "url('/grid.svg')",
backgroundPosition: "calc(50% + var(--x-pos, 0) / 50) calc(50% + var(--y-pos, 0) / 50)",
transition: "background-position 0.08s linear",
}}
>
<div
className="absolute inset-0 bg-gradient-radial from-blue-500/5 dark:from-blue-400/10 to-transparent opacity-0 hover:opacity-100 transition-opacity duration-500 will-change-[opacity]"
style={{
backgroundPosition: "calc(50% + var(--x-pos, 0) / 10) calc(50% + var(--y-pos, 0) / 10)",
backgroundSize: "120% 120%"
}}
/>
</div>
);
};
// Combines all visual effects into one component with performance optimizations
export const VisualEffects: React.FC = () => {
return (
<>
<InteractiveGrid />
</>
);
};
export default VisualEffects;