Skip to content

Commit 83c49d1

Browse files
author
Stefano Vavassori
committed
fix fairidust canvas dimensions using useLayout hook
1 parent 6ce0b61 commit 83c49d1

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

registry/components/cursor/fairydust/FairyDustCursor.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import React, { useEffect, useRef, useState } from 'react';
2+
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
33

44
interface FairyDustCursorProps {
55
colors?: string[];
@@ -43,7 +43,27 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
4343
const particlesRef = useRef<Particle[]>([]);
4444
const cursorRef = useRef({ x: 0, y: 0 });
4545
const lastPosRef = useRef({ x: 0, y: 0 });
46-
const [canvasSize, setCanvasSize] = useState({ width: 0, height: 0 });
46+
const [canvasSize, setCanvasSize] = useState({
47+
width: element ? element.clientWidth : window.innerWidth,
48+
height: element ? element.clientHeight : window.innerHeight,
49+
});
50+
51+
useLayoutEffect(() => {
52+
const updateCanvasSize = () => {
53+
const newWidth = element ? element.clientWidth : window.innerWidth;
54+
const newHeight = element ? element.clientHeight : window.innerHeight;
55+
56+
console.log('vavva updateCanvasSize', newWidth, newHeight);
57+
setCanvasSize({ width: newWidth, height: newHeight });
58+
};
59+
60+
updateCanvasSize();
61+
window.addEventListener('resize', updateCanvasSize);
62+
63+
return () => {
64+
window.removeEventListener('resize', updateCanvasSize);
65+
};
66+
},[element])
4767

4868
useEffect(() => {
4969
const canvas = canvasRef.current;
@@ -53,16 +73,8 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
5373
const context = canvas.getContext('2d');
5474
if (!context) return;
5575

56-
const updateCanvasSize = () => {
57-
const newWidth = element ? targetElement.clientWidth : window.innerWidth;
58-
const newHeight = element
59-
? targetElement.clientHeight
60-
: window.innerHeight;
61-
setCanvasSize({ width: newWidth, height: newHeight });
62-
};
63-
64-
updateCanvasSize();
65-
window.addEventListener('resize', updateCanvasSize);
76+
canvas.width = canvasSize.width;
77+
canvas.height = canvasSize.height;
6678

6779
// Animation frame setup
6880
let animationFrameId: number;
@@ -170,7 +182,6 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
170182
animate();
171183

172184
return () => {
173-
window.removeEventListener('resize', updateCanvasSize);
174185
targetElement.removeEventListener('mousemove', handleMouseMove);
175186
targetElement.removeEventListener('touchmove', handleTouchMove);
176187
cancelAnimationFrame(animationFrameId);
@@ -184,6 +195,7 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
184195
gravity,
185196
fadeSpeed,
186197
initialVelocity,
198+
canvasSize,
187199
]);
188200

189201
return (

0 commit comments

Comments
 (0)