112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
"use client"
|
|
import { useEffect, useRef } from "react"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
export default function GridBackground({ className }: { className?: string }) {
|
|
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
|
|
useEffect(() => {
|
|
const canvas = canvasRef.current
|
|
if (!canvas) return
|
|
const ctx = canvas.getContext("2d")
|
|
if (!ctx) return
|
|
|
|
let animationId: number
|
|
let width = 0
|
|
let height = 0
|
|
|
|
const PIXEL_SIZE = 6
|
|
const WAVE_SPEED = 0.012
|
|
const WAVE_AMP = 0.10
|
|
const WAVE_FREQ = 2.8
|
|
const FALLOFF = 0.35 // how far upward the glow reaches
|
|
|
|
const resize = () => {
|
|
const dpr = window.devicePixelRatio || 1
|
|
width = window.innerWidth
|
|
height = window.innerHeight
|
|
canvas.width = width * dpr
|
|
canvas.height = height * dpr
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
}
|
|
|
|
resize()
|
|
|
|
let t = 0
|
|
|
|
const bayer4x4 = [
|
|
[0.0, 0.5, 0.125, 0.625],
|
|
[0.75, 0.25, 0.875, 0.375],
|
|
[0.1875, 0.6875, 0.0625, 0.5625],
|
|
[0.9375, 0.4375, 0.8125, 0.3125]
|
|
]
|
|
|
|
const draw = () => {
|
|
ctx.clearRect(0, 0, width, height)
|
|
|
|
// Prioritize the 'dark' class set by next-themes to avoid system overrides
|
|
const isDark = document.documentElement.classList.contains("dark")
|
|
// Use the theme's primary colour (falls back to foreground)
|
|
const cs = getComputedStyle(document.documentElement)
|
|
const primary = cs.getPropertyValue("--primary").trim() || cs.getPropertyValue("--foreground").trim()
|
|
const alpha = isDark ? 0.18 : 0.14
|
|
|
|
const cols = Math.ceil(width / PIXEL_SIZE) + 1
|
|
const rows = Math.ceil(height / PIXEL_SIZE) + 1
|
|
// Falloff reaches upward from the wave
|
|
const falloffPx = FALLOFF * height
|
|
const amp = WAVE_AMP * height
|
|
|
|
// color-mix applies the alpha since --primary may be any CSS colour format
|
|
ctx.fillStyle = `color-mix(in oklab, ${primary} ${alpha * 100}%, transparent)`
|
|
|
|
for (let row = 0; row <= rows; row++) {
|
|
for (let col = 0; col <= cols; col++) {
|
|
const x = col * PIXEL_SIZE
|
|
const y = row * PIXEL_SIZE
|
|
|
|
const wx = (x / width) * Math.PI * 2 * WAVE_FREQ
|
|
// Move the wave lower (75% down the screen)
|
|
const waveY = (height * 0.90) - amp * Math.sin(wx + t)
|
|
|
|
// dist: 0 = at or below the wave, positive = above wave
|
|
const dist = Math.max(0, waveY - y)
|
|
|
|
// intensity: 1 = at wave line, 0 = far above it
|
|
let intensity = Math.max(0, 1 - dist / falloffPx)
|
|
intensity = intensity * intensity
|
|
|
|
// Dither check: below waveY is always intensity 1, so it draws solid
|
|
if (intensity <= bayer4x4[row % 4][col % 4]) continue
|
|
|
|
ctx.fillRect(x, y, PIXEL_SIZE, PIXEL_SIZE)
|
|
}
|
|
}
|
|
|
|
t += WAVE_SPEED
|
|
animationId = requestAnimationFrame(draw)
|
|
}
|
|
|
|
draw()
|
|
|
|
const onResize = () => {
|
|
ctx.resetTransform()
|
|
resize()
|
|
}
|
|
window.addEventListener("resize", onResize)
|
|
|
|
return () => {
|
|
cancelAnimationFrame(animationId)
|
|
window.removeEventListener("resize", onResize)
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<canvas
|
|
ref={canvasRef}
|
|
aria-hidden="true"
|
|
style={{ width: "100%", height: "100%" }}
|
|
className={cn("pointer-events-none fixed inset-0 -z-10", className)}
|
|
/>
|
|
)
|
|
}
|