Skip to content

Commit 27d7bbe

Browse files
author
Stefano Vavassori
committed
fix fairidust canvas dimensions using useLayout hook
1 parent 5edf447 commit 27d7bbe

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

content/components/fairydust-cursor.mdx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const metadata = {
22
title: 'Fairy Dust Cursor Effect',
33
description:
4-
'An interactive React component that adds a dynamic bubble effect, visually tracking cursor movement in real time.',
4+
'An interactive React component that creates a magical fairy dust effect, generating a trail of sparkling emoji particles that follow the cursor in real time.',
55
};
66

77
<ComponentCodePreview name='fairydust-cursor' />
@@ -10,7 +10,7 @@ export const metadata = {
1010

1111
```tsx
1212
'use client';
13-
import React, { useEffect, useRef, useState } from 'react';
13+
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
1414

1515
interface FairyDustCursorProps {
1616
colors?: string[];
@@ -54,7 +54,27 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
5454
const particlesRef = useRef<Particle[]>([]);
5555
const cursorRef = useRef({ x: 0, y: 0 });
5656
const lastPosRef = useRef({ x: 0, y: 0 });
57-
const [canvasSize, setCanvasSize] = useState({ width: 0, height: 0 });
57+
const [canvasSize, setCanvasSize] = useState({
58+
width: element ? element.clientWidth : window.innerWidth,
59+
height: element ? element.clientHeight : window.innerHeight,
60+
});
61+
62+
useLayoutEffect(() => {
63+
const updateCanvasSize = () => {
64+
const newWidth = element ? element.clientWidth : window.innerWidth;
65+
const newHeight = element ? element.clientHeight : window.innerHeight;
66+
67+
console.log('vavva updateCanvasSize', newWidth, newHeight);
68+
setCanvasSize({ width: newWidth, height: newHeight });
69+
};
70+
71+
updateCanvasSize();
72+
window.addEventListener('resize', updateCanvasSize);
73+
74+
return () => {
75+
window.removeEventListener('resize', updateCanvasSize);
76+
};
77+
},[element])
5878

5979
useEffect(() => {
6080
const canvas = canvasRef.current;
@@ -64,16 +84,8 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
6484
const context = canvas.getContext('2d');
6585
if (!context) return;
6686

67-
const updateCanvasSize = () => {
68-
const newWidth = element ? targetElement.clientWidth : window.innerWidth;
69-
const newHeight = element
70-
? targetElement.clientHeight
71-
: window.innerHeight;
72-
setCanvasSize({ width: newWidth, height: newHeight });
73-
};
74-
75-
updateCanvasSize();
76-
window.addEventListener('resize', updateCanvasSize);
87+
canvas.width = canvasSize.width;
88+
canvas.height = canvasSize.height;
7789

7890
// Animation frame setup
7991
let animationFrameId: number;
@@ -181,7 +193,6 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
181193
animate();
182194

183195
return () => {
184-
window.removeEventListener('resize', updateCanvasSize);
185196
targetElement.removeEventListener('mousemove', handleMouseMove);
186197
targetElement.removeEventListener('touchmove', handleTouchMove);
187198
cancelAnimationFrame(animationFrameId);
@@ -195,6 +206,7 @@ export const FairyDustCursor: React.FC<FairyDustCursorProps> = ({
195206
gravity,
196207
fadeSpeed,
197208
initialVelocity,
209+
canvasSize,
198210
]);
199211

200212
return (

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)