|
| 1 | +"use client"; |
| 2 | +import { useEffect, useRef } from "react"; |
| 3 | + |
| 4 | +export default function HeartsMinimal() { |
| 5 | + const ref = useRef<HTMLCanvasElement | null>(null); |
| 6 | + |
| 7 | + useEffect(() => { |
| 8 | + const canvas = ref.current; |
| 9 | + if (!canvas) return; |
| 10 | + |
| 11 | + const ctx = canvas.getContext("2d"); |
| 12 | + if (!ctx) return; |
| 13 | + |
| 14 | + const SPEED_Y = 0.35; |
| 15 | + const DRIFT_X = 0.2; |
| 16 | + const heartSpriteSize = 27; |
| 17 | + |
| 18 | + function randomInt(min: number, max: number) { |
| 19 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 20 | + } |
| 21 | + function rand(min: number, max: number) { |
| 22 | + return Math.random() * (max - min) + min; |
| 23 | + } |
| 24 | + |
| 25 | + let w = 0, |
| 26 | + h = 0, |
| 27 | + DPR = 1; |
| 28 | + |
| 29 | + const resize = () => { |
| 30 | + w = window.innerWidth; |
| 31 | + h = window.innerHeight; |
| 32 | + DPR = Math.min(Math.max(window.devicePixelRatio || 1, 1), 2); |
| 33 | + canvas.width = Math.floor(w * DPR); |
| 34 | + canvas.height = Math.floor(h * DPR); |
| 35 | + canvas.style.width = w + "px"; |
| 36 | + canvas.style.height = h + "px"; |
| 37 | + ctx.setTransform(DPR, 0, 0, DPR, 0, 0); |
| 38 | + }; |
| 39 | + |
| 40 | + resize(); |
| 41 | + window.addEventListener("resize", resize); |
| 42 | + |
| 43 | + const heartSprite = makeWaffleSprite(heartSpriteSize); |
| 44 | + |
| 45 | + type Heart = { |
| 46 | + x: number; |
| 47 | + y: number; |
| 48 | + rot: number; |
| 49 | + vr: number; |
| 50 | + s: number; |
| 51 | + vy: number; |
| 52 | + vx: number; |
| 53 | + alpha: number; |
| 54 | + }; |
| 55 | + |
| 56 | + const hearts: Heart[] = []; |
| 57 | + const numHearts = randomInt(150, 200); |
| 58 | + |
| 59 | + for (let i = 0; i < numHearts; i += 1) { |
| 60 | + hearts.push({ |
| 61 | + x: randomInt(0, w), |
| 62 | + y: randomInt(0, h), |
| 63 | + rot: Math.random() * Math.PI * 2, |
| 64 | + vr: rand(-0.01, 0.01), |
| 65 | + s: rand(12, 22), |
| 66 | + vy: rand(0.6, 1.2), |
| 67 | + vx: rand(0.6, 1.15), |
| 68 | + alpha: rand(0.45, 0.8) |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + let raf = 0; |
| 73 | + |
| 74 | + const draw = () => { |
| 75 | + ctx.clearRect(0, 0, w, h); |
| 76 | + |
| 77 | + for (let i = 0; i < hearts.length; i += 1) { |
| 78 | + const ht = hearts[i]; |
| 79 | + |
| 80 | + ctx.save(); |
| 81 | + ctx.translate(ht.x, ht.y); |
| 82 | + ht.rot += ht.vr; |
| 83 | + ctx.rotate(ht.rot); |
| 84 | + ctx.globalAlpha = ht.alpha; |
| 85 | + |
| 86 | + ctx.drawImage(heartSprite, -ht.s / 2, -ht.s / 2, ht.s, ht.s); |
| 87 | + |
| 88 | + ctx.restore(); |
| 89 | + ctx.globalAlpha = 1; |
| 90 | + |
| 91 | + ht.y += SPEED_Y * ht.vy; |
| 92 | + ht.x += DRIFT_X * ht.vx; |
| 93 | + |
| 94 | + // litt “svev” |
| 95 | + ht.x += Math.sin((ht.y + i) * 0.02) * 0.15; |
| 96 | + |
| 97 | + // wrap sidene |
| 98 | + if (ht.x > w) ht.x = 0; |
| 99 | + else if (ht.x < 0) ht.x = w; |
| 100 | + |
| 101 | + // respawn på toppen |
| 102 | + if (ht.y > h) { |
| 103 | + ht.x = randomInt(0, w); |
| 104 | + ht.y = -20; |
| 105 | + ht.rot = Math.random() * Math.PI * 2; |
| 106 | + ht.vr = rand(-0.01, 0.01); |
| 107 | + ht.s = rand(12, 22); |
| 108 | + ht.vy = rand(0.6, 1.2); |
| 109 | + ht.vx = rand(0.6, 1.15); |
| 110 | + ht.alpha = rand(0.45, 0.8); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + raf = requestAnimationFrame(draw); |
| 115 | + }; |
| 116 | + |
| 117 | + raf = requestAnimationFrame(draw); |
| 118 | + |
| 119 | + return () => { |
| 120 | + cancelAnimationFrame(raf); |
| 121 | + window.removeEventListener("resize", resize); |
| 122 | + }; |
| 123 | + }, []); |
| 124 | + |
| 125 | + return ( |
| 126 | + <canvas |
| 127 | + ref={ref} |
| 128 | + aria-hidden |
| 129 | + style={{ |
| 130 | + position: "fixed", |
| 131 | + inset: 0, |
| 132 | + width: "100vw", |
| 133 | + height: "100vh", |
| 134 | + pointerEvents: "none", |
| 135 | + zIndex: 9999 |
| 136 | + }} |
| 137 | + /> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +function makeWaffleSprite(size: number): HTMLCanvasElement { |
| 142 | + const spriteSize = size * 3; |
| 143 | + const c = document.createElement("canvas"); |
| 144 | + c.width = c.height = spriteSize; |
| 145 | + |
| 146 | + const g = c.getContext("2d")!; |
| 147 | + g.font = `${spriteSize * 0.85}px serif`; |
| 148 | + g.textAlign = "center"; |
| 149 | + g.textBaseline = "middle"; |
| 150 | + g.globalAlpha = 0.75; |
| 151 | + g.fillText("🧇", spriteSize / 2, spriteSize / 2); |
| 152 | + |
| 153 | + return c; |
| 154 | +} |
0 commit comments