hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | /** |
| 2 | * Ultra-lightweight zero-dependency confetti particle burst utility. |
| 3 | * Renders celebratory particle effects on an overlay canvas with auto-cleanup. |
| 4 | */ |
| 5 | export function triggerConfetti(options?: { |
| 6 | particleCount?: number; |
| 7 | durationMs?: number; |
| 8 | colors?: string[]; |
| 9 | }) { |
| 10 | if (typeof window === "undefined") return; |
| 11 | if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; |
| 12 | |
| 13 | const count = options?.particleCount ?? 60; |
| 14 | const duration = options?.durationMs ?? 2500; |
| 15 | const colors = options?.colors ?? [ |
| 16 | "#3b82f6", // blue |
| 17 | "#ec4899", // pink |
| 18 | "#8b5cf6", // purple |
| 19 | "#10b981", // green |
| 20 | "#f59e0b", // amber |
| 21 | "#6366f1", // indigo |
| 22 | ]; |
| 23 | |
| 24 | const canvas = document.createElement("canvas"); |
| 25 | canvas.style.position = "fixed"; |
| 26 | canvas.style.top = "0"; |
| 27 | canvas.style.left = "0"; |
| 28 | canvas.style.width = "100vw"; |
| 29 | canvas.style.height = "100vh"; |
| 30 | canvas.style.pointerEvents = "none"; |
| 31 | canvas.style.zIndex = "99999"; |
| 32 | document.body.appendChild(canvas); |
| 33 | |
| 34 | const ctx = canvas.getContext("2d"); |
| 35 | if (!ctx) { |
| 36 | canvas.remove(); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | const dpr = Math.min(window.devicePixelRatio || 1, 2); |
| 41 | const width = window.innerWidth; |
| 42 | const height = window.innerHeight; |
| 43 | canvas.width = width * dpr; |
| 44 | canvas.height = height * dpr; |
| 45 | ctx.scale(dpr, dpr); |
| 46 | |
| 47 | type Particle = { |
| 48 | x: number; |
| 49 | y: number; |
| 50 | vx: number; |
| 51 | vy: number; |
| 52 | size: number; |
| 53 | color: string; |
| 54 | rotation: number; |
| 55 | vRot: number; |
| 56 | opacity: number; |
| 57 | shape: "square" | "circle"; |
| 58 | }; |
| 59 | |
| 60 | const particles: Particle[] = []; |
| 61 | const startX = width / 2; |
| 62 | const startY = height * 0.4; |
| 63 | |
| 64 | for (let i = 0; i < count; i += 1) { |
| 65 | const angle = Math.random() * Math.PI * 2; |
| 66 | const speed = Math.random() * 12 + 6; |
| 67 | particles.push({ |
| 68 | x: startX, |
| 69 | y: startY, |
| 70 | vx: Math.cos(angle) * speed, |
| 71 | vy: Math.sin(angle) * speed - 4, |
| 72 | size: Math.random() * 8 + 5, |
| 73 | color: colors[Math.floor(Math.random() * colors.length)], |
| 74 | rotation: Math.random() * Math.PI * 2, |
| 75 | vRot: (Math.random() - 0.5) * 0.2, |
| 76 | opacity: 1, |
| 77 | shape: Math.random() > 0.4 ? "square" : "circle", |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | let animationId: number; |
| 82 | const startTime = performance.now(); |
| 83 | |
| 84 | const render = (now: number) => { |
| 85 | const elapsed = now - startTime; |
| 86 | if (elapsed >= duration) { |
| 87 | cancelAnimationFrame(animationId); |
| 88 | canvas.remove(); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | ctx.clearRect(0, 0, width, height); |
| 93 | |
| 94 | const progress = elapsed / duration; |
| 95 | const fadeOut = progress > 0.7 ? (1 - progress) / 0.3 : 1; |
| 96 | |
| 97 | for (const p of particles) { |
| 98 | p.x += p.vx; |
| 99 | p.y += p.vy; |
| 100 | p.vy += 0.3; // gravity |
| 101 | p.vx *= 0.98; // drag |
| 102 | p.vy *= 0.98; |
| 103 | p.rotation += p.vRot; |
| 104 | |
| 105 | ctx.save(); |
| 106 | ctx.globalAlpha = p.opacity * fadeOut; |
| 107 | ctx.translate(p.x, p.y); |
| 108 | ctx.rotate(p.rotation); |
| 109 | ctx.fillStyle = p.color; |
| 110 | |
| 111 | if (p.shape === "circle") { |
| 112 | ctx.beginPath(); |
| 113 | ctx.arc(0, 0, p.size / 2, 0, Math.PI * 2); |
| 114 | ctx.fill(); |
| 115 | } else { |
| 116 | ctx.fillRect(-p.size / 2, -p.size / 2, p.size, p.size * 0.7); |
| 117 | } |
| 118 | |
| 119 | ctx.restore(); |
| 120 | } |
| 121 | |
| 122 | animationId = requestAnimationFrame(render); |
| 123 | }; |
| 124 | |
| 125 | animationId = requestAnimationFrame(render); |
| 126 | } |