-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
343 additions
and
61 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
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"use client"; | ||
|
||
import React, { RefObject, useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
interface StarProps { | ||
x: number; | ||
y: number; | ||
radius: number; | ||
opacity: number; | ||
twinkleSpeed: number | null; | ||
} | ||
|
||
interface StarBackgroundProps { | ||
starDensity?: number; | ||
allStarsTwinkle?: boolean; | ||
twinkleProbability?: number; | ||
minTwinkleSpeed?: number; | ||
maxTwinkleSpeed?: number; | ||
className?: string; | ||
} | ||
|
||
export const StarsBackground: React.FC<StarBackgroundProps> = ({ | ||
starDensity = 0.00015, | ||
allStarsTwinkle = true, | ||
twinkleProbability = 0.7, | ||
minTwinkleSpeed = 0.5, | ||
maxTwinkleSpeed = 1, | ||
}) => { | ||
const [stars, setStars] = useState<StarProps[]>([]); | ||
const canvasRef: RefObject<HTMLCanvasElement> = useRef<HTMLCanvasElement>(null); | ||
|
||
const generateStars = useCallback( | ||
(width: number, height: number): StarProps[] => { | ||
const area = width * height; | ||
const numStars = Math.floor(area * starDensity); | ||
return Array.from({ length: numStars }, () => createStar(width, height)); | ||
}, | ||
[starDensity, allStarsTwinkle, twinkleProbability, minTwinkleSpeed, maxTwinkleSpeed], | ||
); | ||
|
||
const createStar = (width: number, height: number): StarProps => { | ||
const shouldTwinkle = allStarsTwinkle || Math.random() < twinkleProbability; | ||
return { | ||
x: Math.random() * width, | ||
y: Math.random() * height, | ||
radius: Math.random() * 0.05 + 0.5, | ||
opacity: Math.random() * 0.5 + 0.5, | ||
twinkleSpeed: shouldTwinkle ? minTwinkleSpeed + Math.random() * (maxTwinkleSpeed - minTwinkleSpeed) : null, | ||
}; | ||
}; | ||
|
||
useEffect(() => { | ||
const updateStars = () => { | ||
if (canvasRef.current) { | ||
const canvas = canvasRef.current; | ||
const ctx = canvas.getContext("2d"); | ||
if (!ctx) return; | ||
|
||
const { width, height } = canvas.getBoundingClientRect(); | ||
canvas.width = width; | ||
canvas.height = height; | ||
setStars(generateStars(width, height)); | ||
} | ||
}; | ||
|
||
updateStars(); | ||
|
||
const resizeObserver = new ResizeObserver(updateStars); | ||
const currentCanvas = canvasRef.current; // Copy the ref value to a variable | ||
if (currentCanvas) { | ||
resizeObserver.observe(currentCanvas); | ||
} | ||
|
||
return () => { | ||
if (currentCanvas) { | ||
resizeObserver.unobserve(currentCanvas); | ||
} | ||
}; | ||
}, [starDensity, allStarsTwinkle, twinkleProbability, minTwinkleSpeed, maxTwinkleSpeed, generateStars]); | ||
|
||
useEffect(() => { | ||
const canvas = canvasRef.current; | ||
if (!canvas) return; | ||
|
||
const ctx = canvas.getContext("2d"); | ||
if (!ctx) return; | ||
|
||
let animationFrameId: number; | ||
|
||
const render = () => { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
stars.forEach(star => drawStar(ctx, star)); | ||
|
||
animationFrameId = requestAnimationFrame(render); | ||
}; | ||
|
||
render(); | ||
|
||
return () => { | ||
cancelAnimationFrame(animationFrameId); | ||
}; | ||
}, [stars]); | ||
|
||
const drawStar = (ctx: CanvasRenderingContext2D, star: StarProps) => { | ||
ctx.beginPath(); | ||
ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2); | ||
ctx.fillStyle = `rgba(255, 255, 255, ${star.opacity})`; | ||
ctx.fill(); | ||
|
||
if (star.twinkleSpeed !== null) { | ||
star.opacity = 0.5 + Math.abs(Math.sin((Date.now() * 0.001) / star.twinkleSpeed) * 0.5); | ||
} | ||
}; | ||
|
||
return <canvas ref={canvasRef} className={"min-h-full w-full absolute inset-0 !bg-black"} />; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.