|
| 1 | +import { useEffect, useRef } from 'react' |
| 2 | + |
| 3 | +interface RainifyProps { |
| 4 | + className?: string |
| 5 | + intensity?: number |
| 6 | + color?: string |
| 7 | + zIndex?: number |
| 8 | + speed?: number |
| 9 | + wind?: number |
| 10 | + thickness?: number |
| 11 | +} |
| 12 | + |
| 13 | +interface Raindrop { |
| 14 | + x: number |
| 15 | + y: number |
| 16 | + length: number |
| 17 | + velocityY: number |
| 18 | + velocityX: number |
| 19 | +} |
| 20 | + |
| 21 | +export function Rainify({ |
| 22 | + className, |
| 23 | + intensity = 50, |
| 24 | + color = 'rgb(128,128,128, .5)', |
| 25 | + zIndex = 0, |
| 26 | + speed = 1, |
| 27 | + wind = 0, |
| 28 | + thickness = 1, |
| 29 | +}: RainifyProps) { |
| 30 | + const canvasRef = useRef<HTMLCanvasElement>(null) |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const canvas = canvasRef.current |
| 34 | + if (!canvas) return |
| 35 | + |
| 36 | + const ctx = canvas.getContext('2d') |
| 37 | + if (!ctx) return |
| 38 | + |
| 39 | + canvas.width = window.innerWidth |
| 40 | + canvas.height = window.innerHeight |
| 41 | + |
| 42 | + const raindrops: Raindrop[] = Array.from({ length: intensity }, () => ({ |
| 43 | + x: Math.random() * canvas.width, |
| 44 | + y: Math.random() * canvas.height, |
| 45 | + length: Math.random() * 20 + 10, |
| 46 | + velocityY: (Math.random() * 2 + 2) * speed, |
| 47 | + velocityX: wind, |
| 48 | + })) |
| 49 | + |
| 50 | + const drawRaindrop = (drop: Raindrop) => { |
| 51 | + ctx.beginPath() |
| 52 | + ctx.moveTo(drop.x, drop.y) |
| 53 | + ctx.lineTo(drop.x + drop.velocityX, drop.y + drop.length) |
| 54 | + ctx.strokeStyle = color |
| 55 | + ctx.lineWidth = thickness |
| 56 | + ctx.stroke() |
| 57 | + } |
| 58 | + |
| 59 | + const updateRaindrop = (drop: Raindrop) => { |
| 60 | + drop.y += drop.velocityY |
| 61 | + drop.x += drop.velocityX |
| 62 | + |
| 63 | + if (drop.y > canvas.height) { |
| 64 | + drop.y = 0 - drop.length |
| 65 | + drop.x = Math.random() * canvas.width |
| 66 | + } |
| 67 | + |
| 68 | + if (drop.x > canvas.width) { |
| 69 | + drop.x = 0 |
| 70 | + } else if (drop.x < 0) { |
| 71 | + drop.x = canvas.width |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const animate = () => { |
| 76 | + ctx.clearRect(0, 0, canvas.width, canvas.height) |
| 77 | + raindrops.forEach((drop) => { |
| 78 | + drawRaindrop(drop) |
| 79 | + updateRaindrop(drop) |
| 80 | + }) |
| 81 | + requestAnimationFrame(animate) |
| 82 | + } |
| 83 | + |
| 84 | + animate() |
| 85 | + |
| 86 | + const handleResize = () => { |
| 87 | + canvas.width = window.innerWidth |
| 88 | + canvas.height = window.innerHeight |
| 89 | + } |
| 90 | + |
| 91 | + window.addEventListener('resize', handleResize) |
| 92 | + |
| 93 | + return () => { |
| 94 | + window.removeEventListener('resize', handleResize) |
| 95 | + } |
| 96 | + }, [intensity, color, speed, wind, thickness]) |
| 97 | + |
| 98 | + return ( |
| 99 | + <canvas |
| 100 | + className={className} |
| 101 | + ref={canvasRef} |
| 102 | + style={{ position: 'fixed', top: 0, left: 0, zIndex, pointerEvents: 'none' }} |
| 103 | + /> |
| 104 | + ) |
| 105 | +} |
0 commit comments