fix: gracefully degrade shader background when WebGL is unavailable
This commit is contained in:
parent
604216ddec
commit
d80712098b
1 changed files with 26 additions and 5 deletions
|
|
@ -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)}>
|
||||||
<MeshGradient
|
{supportsWebGL ? (
|
||||||
className="absolute inset-0 h-full w-full"
|
<MeshGradient
|
||||||
colors={["#000000", "#1a1a1a", "#333333", "#ffffff"]}
|
className="absolute inset-0 h-full w-full"
|
||||||
speed={speed * 0.5}
|
colors={["#000000", "#1a1a1a", "#333333", "#ffffff"]}
|
||||||
/>
|
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>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue