-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from MastanSayyad/main
Added "Cursor Trail Effect" to Website
- Loading branch information
Showing
3 changed files
with
93 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use client"; | ||
import React, { useEffect } from 'react'; | ||
|
||
interface ExtendedHTMLElement extends HTMLElement { | ||
x?: number; | ||
y?: number; | ||
} | ||
|
||
const CursorTrail: React.FC = () => { | ||
useEffect(() => { | ||
const circles: ExtendedHTMLElement[] = []; | ||
const numCircles = 25; | ||
|
||
|
||
for (let i = 0; i < numCircles; i++) { | ||
circles.push({ x: 0, y: 0, size: 25 }); | ||
} | ||
|
||
let mouseX = 0; | ||
let mouseY = 0; | ||
let isMouseMoving = false; | ||
|
||
const handleMouseMove = (e: MouseEvent) => { | ||
mouseX = e.pageX; | ||
mouseY = e.pageY - window.scrollY; | ||
isMouseMoving = true; | ||
}; | ||
|
||
const updateCircles = () => { | ||
circles.forEach((circle, index) => { | ||
const targetX = index === 0 ? mouseX : circles[index - 1].x; | ||
const targetY = index === 0 ? mouseY : circles[index - 1].y; | ||
|
||
// for faster movement | ||
circle.x += (targetX - circle.x) * 0.6; | ||
circle.y += (targetY - circle.y) * 0.6; | ||
|
||
|
||
circle.size = 25 - (index * 0.8); // the rate of size decrease | ||
|
||
const circleElement = document.querySelector(`#circle-${index}`) as ExtendedHTMLElement; | ||
if (circleElement) { | ||
circleElement.style.left = `${circle.x}px`; | ||
circleElement.style.top = `${circle.y}px`; | ||
circleElement.style.width = `${circle.size}px`; // width for circle effect | ||
circleElement.style.height = `${circle.size}px`; // height for circle effect | ||
} | ||
}); | ||
|
||
if (!isMouseMoving) { | ||
circles.forEach(circle => { | ||
circle.x += (mouseX - circle.x) * 0.1; | ||
circle.y += (mouseY - circle.y) * 0.1; | ||
}); | ||
} | ||
|
||
requestAnimationFrame(updateCircles); | ||
}; | ||
|
||
window.addEventListener("mousemove", handleMouseMove); | ||
updateCircles(); | ||
|
||
return () => { | ||
window.removeEventListener("mousemove", handleMouseMove); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className="circle-container"> | ||
{Array.from({ length: 25 }).map((_, index) => ( | ||
<div | ||
key={index} | ||
id={`circle-${index}`} | ||
className="circle" | ||
style={{ width: '25px', height: '25px' }} // Initial size | ||
></div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default CursorTrail; |