Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: the cursor animation is bugged #368 #438

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions client/src/component/Cursor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<div ref={mainCursor} className="main-cursor"></div>
<div
ref={mainCursor}
className={`main-cursor ${!isCursorVisible ? "hidden" : ""}`}
></div>
{[...Array(8)].map((_, i) => (
<div
key={i}
ref={(el) => (trailingCircles.current[i] = el)}
className="trailing-circle"
className={`trailing-circle ${!isCursorVisible ? "hidden" : ""}`}
></div>
))}
</>
Expand Down
6 changes: 6 additions & 0 deletions client/src/css/Cursor.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
height: 22px;
border-radius: 50%;
pointer-events: none;
transition: opacity 0.1s ease, transform 0.07s ease;
}

.main-cursor {
Expand All @@ -19,3 +20,8 @@
z-index: 999;
transition: transform 0.07s ease;
}

.main-cursor.hidden,
.trailing-circle.hidden {
opacity: 0;
}
Loading