@@ -7,11 +7,8 @@ interface MousePosition {
7
7
y : number ;
8
8
}
9
9
10
- function MousePosition ( ) : MousePosition {
11
- const [ mousePosition , setMousePosition ] = useState < MousePosition > ( {
12
- x : 0 ,
13
- y : 0 ,
14
- } ) ;
10
+ function useMousePosition ( ) : MousePosition {
11
+ const [ mousePosition , setMousePosition ] = useState < MousePosition > ( { x : 0 , y : 0 } ) ;
15
12
16
13
useEffect ( ( ) => {
17
14
const handleMouseMove = ( event : MouseEvent ) => {
@@ -71,10 +68,15 @@ const Particles: React.FC<ParticlesProps> = ({
71
68
const canvasContainerRef = useRef < HTMLDivElement > ( null ) ;
72
69
const context = useRef < CanvasRenderingContext2D | null > ( null ) ;
73
70
const circles = useRef < any [ ] > ( [ ] ) ;
74
- const mousePosition = MousePosition ( ) ;
71
+ const mousePosition = useMousePosition ( ) ;
75
72
const mouse = useRef < { x : number ; y : number } > ( { x : 0 , y : 0 } ) ;
76
73
const canvasSize = useRef < { w : number ; h : number } > ( { w : 0 , h : 0 } ) ;
77
- const dpr = typeof window !== "undefined" ? window . devicePixelRatio : 1 ;
74
+
75
+ // Defer devicePixelRatio calculation to client
76
+ const [ dpr , setDpr ] = useState ( 1 ) ;
77
+ useEffect ( ( ) => {
78
+ setDpr ( window . devicePixelRatio ) ;
79
+ } , [ ] ) ;
78
80
79
81
useEffect ( ( ) => {
80
82
if ( canvasRef . current ) {
@@ -87,7 +89,7 @@ const Particles: React.FC<ParticlesProps> = ({
87
89
return ( ) => {
88
90
window . removeEventListener ( "resize" , initCanvas ) ;
89
91
} ;
90
- } , [ color ] ) ;
92
+ } , [ color , dpr ] ) ;
91
93
92
94
useEffect ( ( ) => {
93
95
onMouseMove ( ) ;
@@ -116,7 +118,7 @@ const Particles: React.FC<ParticlesProps> = ({
116
118
}
117
119
} ;
118
120
119
- interface Circle {
121
+ interface Circle {
120
122
x : number ;
121
123
y : number ;
122
124
translateX : number ;
@@ -127,7 +129,7 @@ const Particles: React.FC<ParticlesProps> = ({
127
129
dx : number ;
128
130
dy : number ;
129
131
magnetism : number ;
130
- } ;
132
+ }
131
133
132
134
const resizeCanvas = ( ) => {
133
135
if ( canvasContainerRef . current && canvasRef . current && context . current ) {
0 commit comments