-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from opherlabs/main
fix countdown component
- Loading branch information
Showing
4 changed files
with
99 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 80 additions & 31 deletions
111
components/src/components/CountDown/ParticleComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,97 @@ | ||
'use client'; | ||
|
||
import React, { useRef, useEffect } from 'react'; | ||
import React, { useRef, useEffect, useState } from 'react'; | ||
|
||
interface Particle { | ||
x: number; | ||
y: number; | ||
dx: number; | ||
dy: number; | ||
size: number; | ||
color: string; | ||
} | ||
|
||
interface ParticleProps { | ||
context: CanvasRenderingContext2D | null; | ||
width: number; | ||
height: number; | ||
count: number; | ||
} | ||
|
||
export const ParticleComponent: React.FC<ParticleProps> = ({ count }) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const [isClient, setIsClient] = useState(false); | ||
|
||
export const ParticleComponent: React.FC<ParticleProps> = ({ context, width, height }) => { | ||
const x = useRef(Math.random() * width); | ||
const y = useRef(Math.random() * height); | ||
const dx = useRef((Math.random() - 0.5) * 0.5); | ||
const dy = useRef((Math.random() - 0.5) * 0.5); | ||
const color = `hsl(${Math.random() * 360}, 50%, 50%)`; | ||
const size = Math.random() * 3 + 1; | ||
useEffect(() => { | ||
setIsClient(true); | ||
}, []); | ||
|
||
const animate = () => { | ||
if (!context) return; | ||
useEffect(() => { | ||
if (!isClient) return; | ||
|
||
x.current += dx.current; | ||
if (x.current < 0 || x.current > width) { | ||
dx.current = -dx.current; | ||
x.current = Math.max(0, Math.min(x.current, width)); | ||
} | ||
const canvas = canvasRef.current; | ||
if (!canvas) return; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) return; | ||
|
||
const resizeCanvas = () => { | ||
canvas.width = window.innerWidth; | ||
canvas.height = window.innerHeight; | ||
}; | ||
|
||
resizeCanvas(); | ||
window.addEventListener('resize', resizeCanvas); | ||
|
||
const particles: Particle[] = []; | ||
|
||
y.current += dy.current; | ||
if (y.current < 0 || y.current > height) { | ||
dy.current = -dy.current; | ||
y.current = Math.max(0, Math.min(y.current, height)); | ||
for (let i = 0; i < count; i++) { | ||
particles.push({ | ||
x: Math.random() * canvas.width, | ||
y: Math.random() * canvas.height, | ||
dx: (Math.random() - 0.5) * 2, | ||
dy: (Math.random() - 0.5) * 2, | ||
size: Math.random() * 5 + 1, | ||
color: `hsl(${Math.random() * 360}, 50%, 50%)`, | ||
}); | ||
} | ||
|
||
context.beginPath(); | ||
context.arc(x.current, y.current, size, 0, Math.PI * 2); | ||
context.fillStyle = color; | ||
context.fill(); | ||
let animationFrameId: number; | ||
|
||
requestAnimationFrame(animate); | ||
}; | ||
const animate = () => { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
|
||
particles.forEach((particle) => { | ||
particle.x += particle.dx; | ||
particle.y += particle.dy; | ||
|
||
if (particle.x < 0 || particle.x > canvas.width) { | ||
particle.dx = -particle.dx; | ||
} | ||
|
||
if (particle.y < 0 || particle.y > canvas.height) { | ||
particle.dy = -particle.dy; | ||
} | ||
|
||
ctx.beginPath(); | ||
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2); | ||
ctx.fillStyle = particle.color; | ||
ctx.fill(); | ||
}); | ||
|
||
animationFrameId = requestAnimationFrame(animate); | ||
}; | ||
|
||
useEffect(() => { | ||
animate(); | ||
}, [context, width, height]); | ||
|
||
return null; | ||
return () => { | ||
window.removeEventListener('resize', resizeCanvas); | ||
cancelAnimationFrame(animationFrameId); | ||
}; | ||
}, [count, isClient]); | ||
|
||
if (!isClient) { | ||
return <div className="absolute inset-0 bg-gray-900" />; | ||
} | ||
|
||
return <canvas ref={canvasRef} className="absolute inset-0 bg-gray-900" />; | ||
}; | ||
|
||
export default ParticleComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters