fix: gracefully degrade shader background when WebGL is unavailable

This commit is contained in:
Esdras Renan 2025-10-16 23:37:36 -03:00
parent 604216ddec
commit d80712098b

View file

@ -1,19 +1,40 @@
"use client" "use client"
import { useEffect, useState } from "react"
import { MeshGradient } from "@paper-design/shaders-react" import { MeshGradient } from "@paper-design/shaders-react"
import { cn } from "@/lib/utils" import { cn } from "@/lib/utils"
function isWebGL2Supported(): boolean {
try {
const canvas = document.createElement("canvas")
const gl = canvas?.getContext("webgl2") ?? canvas?.getContext("webgl")
return Boolean(gl)
} catch {
return false
}
}
export default function BackgroundPaperShadersWrapper({ className }: { className?: string }) { export default function BackgroundPaperShadersWrapper({ className }: { className?: string }) {
const [supportsWebGL, setSupportsWebGL] = useState<boolean>(true)
useEffect(() => {
setSupportsWebGL(isWebGL2Supported())
}, [])
const speed = 1.0 const speed = 1.0
return ( return (
<div className={cn("relative h-full w-full overflow-hidden bg-black", className)}> <div className={cn("relative h-full w-full overflow-hidden bg-black", className)}>
{supportsWebGL ? (
<MeshGradient <MeshGradient
className="absolute inset-0 h-full w-full" className="absolute inset-0 h-full w-full"
colors={["#000000", "#1a1a1a", "#333333", "#ffffff"]} colors={["#000000", "#1a1a1a", "#333333", "#ffffff"]}
speed={speed * 0.5} speed={speed * 0.5}
/> />
) : (
<div className="absolute inset-0 h-full w-full bg-[radial-gradient(circle_at_top,_rgba(15,15,15,0.95),_rgba(0,0,0,1))]" />
)}
</div> </div>
) )
} }