-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathuse-spotlight.ts
146 lines (125 loc) · 3.98 KB
/
use-spotlight.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// @ts-nocheck
'use client';
import { useEffect, useRef, useState } from 'react';
const useSpotlightEffect = (config = {}) => {
const {
spotlightSize = 200,
spotlightIntensity = 0.8,
fadeSpeed = 0.1,
glowColor = '255, 255, 255',
pulseSpeed = 2000,
} = config;
const canvasRef = useRef(null);
const ctxRef = useRef(null);
const spotlightPos = useRef({ x: 0, y: 0 });
const targetPos = useRef({ x: 0, y: 0 });
const animationFrame = useRef(null);
const [isHovered, setIsHovered] = useState(false);
useEffect(() => {
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
ctxRef.current = ctx;
const resizeCanvas = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
const lerp = (start, end, factor) => {
return start + (end - start) * factor;
};
const handleMouseMove = (e) => {
targetPos.current = { x: e.clientX, y: e.clientY };
setIsHovered(true);
};
const handleMouseLeave = () => {
setIsHovered(false);
};
const render = () => {
if (!canvas || !ctx) return;
// Smooth position transition
spotlightPos.current.x = lerp(
spotlightPos.current.x,
targetPos.current.x,
fadeSpeed
);
spotlightPos.current.y = lerp(
spotlightPos.current.y,
targetPos.current.y,
fadeSpeed
);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Create dark overlay
ctx.fillStyle = 'rgba(0, 0, 0, 0.85)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Calculate pulse effect
const pulseScale =
1 + 0.1 * Math.sin((Date.now() / pulseSpeed) * Math.PI * 2);
const currentSpotlightSize = spotlightSize * pulseScale;
// Create spotlight gradient
const gradient = ctx.createRadialGradient(
spotlightPos.current.x,
spotlightPos.current.y,
0,
spotlightPos.current.x,
spotlightPos.current.y,
currentSpotlightSize
);
// Add multiple color stops for smoother transition
gradient.addColorStop(0, `rgba(${glowColor}, ${spotlightIntensity})`);
gradient.addColorStop(
0.5,
`rgba(${glowColor}, ${spotlightIntensity * 0.5})`
);
gradient.addColorStop(1, 'rgba(0, 0, 0, 0)');
// Apply spotlight effect
ctx.globalCompositeOperation = 'destination-out';
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(
spotlightPos.current.x,
spotlightPos.current.y,
currentSpotlightSize,
0,
Math.PI * 2
);
ctx.fill();
// Add glow effect
ctx.globalCompositeOperation = 'source-over';
const glowGradient = ctx.createRadialGradient(
spotlightPos.current.x,
spotlightPos.current.y,
0,
spotlightPos.current.x,
spotlightPos.current.y,
currentSpotlightSize * 1.2
);
glowGradient.addColorStop(0, `rgba(${glowColor}, 0.2)`);
glowGradient.addColorStop(1, 'rgba(0, 0, 0, 0)');
ctx.fillStyle = glowGradient;
ctx.beginPath();
ctx.arc(
spotlightPos.current.x,
spotlightPos.current.y,
currentSpotlightSize * 1.2,
0,
Math.PI * 2
);
ctx.fill();
animationFrame.current = requestAnimationFrame(render);
};
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseleave', handleMouseLeave);
render();
return () => {
window.removeEventListener('resize', resizeCanvas);
document.addEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseleave', handleMouseLeave);
if (animationFrame.current) {
cancelAnimationFrame(animationFrame.current);
}
};
}, [spotlightSize, spotlightIntensity, fadeSpeed, glowColor, pulseSpeed]);
return canvasRef;
};
export default useSpotlightEffect;