|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useTheme } from "next-themes" |
| 4 | +import { useEffect, useRef } from "react" |
| 5 | + |
| 6 | +interface EraserCursorProps { |
| 7 | + size: number |
| 8 | + isActive: boolean |
| 9 | +} |
| 10 | + |
| 11 | +export function EraserCursor({ size, isActive }: EraserCursorProps) { |
| 12 | + const { theme } = useTheme() |
| 13 | + const cursorRef = useRef<HTMLDivElement>(null) |
| 14 | + const trailRef = useRef<HTMLCanvasElement>(null) |
| 15 | + const positionRef = useRef({ x: 0, y: 0 }) |
| 16 | + const prevPositionRef = useRef({ x: 0, y: 0 }) |
| 17 | + const animationFrameRef = useRef<number | null>(null) |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (!isActive) return |
| 21 | + |
| 22 | + const handleMouseMove = (e: MouseEvent) => { |
| 23 | + if (cursorRef.current) { |
| 24 | + const x = e.clientX |
| 25 | + const y = e.clientY |
| 26 | + cursorRef.current.style.transform = `translate(${x}px, ${y}px)` |
| 27 | + |
| 28 | + prevPositionRef.current = { ...positionRef.current } |
| 29 | + positionRef.current = { x, y } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const drawTrail = () => { |
| 34 | + if (!trailRef.current || !isActive) return |
| 35 | + const canvas = trailRef.current |
| 36 | + const ctx = canvas.getContext("2d") |
| 37 | + if (!ctx) return |
| 38 | + |
| 39 | + canvas.width = window.innerWidth |
| 40 | + canvas.height = window.innerHeight |
| 41 | + |
| 42 | + // Fade out previous trails for smoother effect |
| 43 | + ctx.fillStyle = "rgba(0, 0, 0, 0.05)" // Adjust opacity for smooth fading |
| 44 | + ctx.fillRect(0, 0, canvas.width, canvas.height) |
| 45 | + |
| 46 | + // Add blur for softer trail effect |
| 47 | + ctx.filter = "blur(1px)" |
| 48 | + |
| 49 | + const positions = calculateTrailPositions( |
| 50 | + prevPositionRef.current, |
| 51 | + positionRef.current, |
| 52 | + 8, // Increased segments for smoother trail |
| 53 | + ) |
| 54 | + |
| 55 | + positions.forEach((pos, index) => { |
| 56 | + const decayFactor = easeOut(1 - index / positions.length) |
| 57 | + const trailSize = Math.max(size * decayFactor, 1) // Ensures it doesn't go to 0 |
| 58 | + |
| 59 | + ctx.beginPath() |
| 60 | + ctx.arc(pos.x, pos.y, trailSize / 2, 0, Math.PI * 2) |
| 61 | + ctx.fillStyle = `rgba(255, 255, 255, ${decayFactor * 0.8})` |
| 62 | + ctx.fill() |
| 63 | + |
| 64 | + ctx.strokeStyle = theme === "dark" ? "rgba(255, 255, 255, 0.8)" : "rgba(0, 0, 0, 0.8)" |
| 65 | + ctx.lineWidth = 1.5 |
| 66 | + ctx.stroke() |
| 67 | + }) |
| 68 | + |
| 69 | + // Reset filter after drawing |
| 70 | + ctx.filter = "none" |
| 71 | + |
| 72 | + animationFrameRef.current = requestAnimationFrame(drawTrail) |
| 73 | + } |
| 74 | + |
| 75 | + const calculateTrailPositions = ( |
| 76 | + prev: { x: number; y: number }, |
| 77 | + current: { x: number; y: number }, |
| 78 | + segments: number, |
| 79 | + ) => { |
| 80 | + const positions = [] |
| 81 | + for (let i = 0; i < segments; i++) { |
| 82 | + const ratio = i / segments |
| 83 | + positions.push({ |
| 84 | + x: current.x * (1 - ratio) + prev.x * ratio, |
| 85 | + y: current.y * (1 - ratio) + prev.y * ratio, |
| 86 | + }) |
| 87 | + } |
| 88 | + return positions |
| 89 | + } |
| 90 | + |
| 91 | + const easeOut = (t: number) => 1 - Math.pow(1 - t, 3) // Smooth fade-out effect |
| 92 | + |
| 93 | + document.addEventListener("mousemove", handleMouseMove) |
| 94 | + animationFrameRef.current = requestAnimationFrame(drawTrail) |
| 95 | + |
| 96 | + const handleResize = () => { |
| 97 | + if (trailRef.current) { |
| 98 | + trailRef.current.width = window.innerWidth |
| 99 | + trailRef.current.height = window.innerHeight |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + window.addEventListener("resize", handleResize) |
| 104 | + handleResize() |
| 105 | + |
| 106 | + return () => { |
| 107 | + document.removeEventListener("mousemove", handleMouseMove) |
| 108 | + window.removeEventListener("resize", handleResize) |
| 109 | + if (animationFrameRef.current) { |
| 110 | + cancelAnimationFrame(animationFrameRef.current) |
| 111 | + } |
| 112 | + } |
| 113 | + }, [isActive, size, theme]) |
| 114 | + |
| 115 | + if (!isActive) return null |
| 116 | + |
| 117 | + return ( |
| 118 | + <> |
| 119 | + <canvas ref={trailRef} className="fixed inset-0 pointer-events-none z-50" /> |
| 120 | + <div |
| 121 | + ref={cursorRef} |
| 122 | + className="fixed top-0 left-0 pointer-events-none z-50" |
| 123 | + style={{ |
| 124 | + width: `${size}px`, |
| 125 | + height: `${size}px`, |
| 126 | + borderRadius: "50%", |
| 127 | + backgroundColor: "white", |
| 128 | + border: `1.5px solid ${theme === "dark" ? "white" : "black"}`, |
| 129 | + transform: "translate(-50%, -50%)", |
| 130 | + boxShadow: "0 0 5px rgba(0, 0, 0, 0.2)", |
| 131 | + }} |
| 132 | + /> |
| 133 | + </> |
| 134 | + ) |
| 135 | +} |
| 136 | + |
0 commit comments