From c88ad46d69b66952f3366c3483c4aad32d2f7840 Mon Sep 17 00:00:00 2001 From: Abhi-hertz <93651229+AE-Hertz@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:29:38 +0530 Subject: [PATCH] bug: fixed cursor animation --- client/src/component/Cursor.jsx | 43 ++++++++++++++++++++++++--------- client/src/css/Cursor.css | 6 +++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/client/src/component/Cursor.jsx b/client/src/component/Cursor.jsx index 2b54dee8..5c0be353 100644 --- a/client/src/component/Cursor.jsx +++ b/client/src/component/Cursor.jsx @@ -5,30 +5,51 @@ import '../css/Cursor.css'; const CustomCursor = () => { const mainCursor = useRef(null); const trailingCircles = useRef([]); + const [isCursorVisible, setIsCursorVisible] = useState(true); useEffect(() => { const onMouseMove = (e) => { - mainCursor.current.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`; + if (isCursorVisible) { + mainCursor.current.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`; - trailingCircles.current.forEach((circle, index) => { - setTimeout(() => { - circle.style.transform = `translate(${e.clientX}px, ${e.clientY}px) scale(${1 - index * 0.1})`; - }, index * 20); - }); + trailingCircles.current.forEach((circle, index) => { + setTimeout(() => { + circle.style.transform = `translate(${e.clientX}px, ${e.clientY}px) scale(${1 - index * 0.1})`; + }, index * 20); + }); + } }; - window.addEventListener("mousemove", onMouseMove); - return () => window.removeEventListener("mousemove", onMouseMove); - }, []); + const handleMouseLeave = () => { + setIsCursorVisible(false); + }; + + const handleMouseEnter = () => { + setIsCursorVisible(true); + }; + + document.addEventListener("mousemove", onMouseMove); + document.addEventListener("mouseleave", handleMouseLeave); + document.addEventListener("mouseenter", handleMouseEnter); + + return () => { + document.removeEventListener("mousemove", onMouseMove); + document.removeEventListener("mouseleave", handleMouseLeave); + document.removeEventListener("mouseenter", handleMouseEnter); + }; + }, [isCursorVisible]); return ( <> -
+
{[...Array(8)].map((_, i) => (
(trailingCircles.current[i] = el)} - className="trailing-circle" + className={`trailing-circle ${!isCursorVisible ? "hidden" : ""}`} >
))} diff --git a/client/src/css/Cursor.css b/client/src/css/Cursor.css index f3041bc6..221e39b9 100644 --- a/client/src/css/Cursor.css +++ b/client/src/css/Cursor.css @@ -7,6 +7,7 @@ height: 22px; border-radius: 50%; pointer-events: none; + transition: opacity 0.1s ease, transform 0.07s ease; } .main-cursor { @@ -19,3 +20,8 @@ z-index: 999; transition: transform 0.07s ease; } + +.main-cursor.hidden, +.trailing-circle.hidden { + opacity: 0; +}