import * as React from "react"; export interface WebGLSupportResult { supported: boolean; context: "webgl2" | "webgl" | "none"; reason?: string; } export interface WebGLSupportBoundaryProps { children: React.ReactNode; fallback?: React.ReactNode; } export function detectWebGLSupport(): WebGLSupportResult { if (typeof window === "undefined" || typeof document === "undefined") { return { supported: false, context: "none", reason: "WebGL can only be detected in a browser environment.", }; } if (!("WebGLRenderingContext" in window)) { return { supported: false, context: "none", reason: "This browser does not expose WebGLRenderingContext.", }; } const canvas = document.createElement("canvas"); try { const webgl2 = canvas.getContext("webgl2", { antialias: false, powerPreference: "high-performance", }); if (webgl2) { return { supported: true, context: "webgl2" }; } const webgl = canvas.getContext("webgl", { antialias: false }) ?? canvas.getContext("experimental-webgl", { antialias: false }); if (webgl) { return { supported: true, context: "webgl" }; } return { supported: false, context: "none", reason: "A WebGL context could not be created. Hardware acceleration may be disabled.", }; } catch (error) { return { supported: false, context: "none", reason: error instanceof Error ? error.message : "WebGL context creation failed.", }; } } function DefaultWebGLFallback({ reason }: { reason?: string }): JSX.Element { return (

WebGL unavailable

The 3D viewer cannot start.

Mechanica needs a WebGL-capable browser with hardware acceleration enabled. You can still browse the catalogue and engineering notes, then return to the 3D scene from a supported device.

{reason ? (

Diagnostic: {reason}

) : null} Back to catalogue
); } export function WebGLSupportBoundary({ children, fallback, }: WebGLSupportBoundaryProps): JSX.Element { const [support, setSupport] = React.useState(() => ({ supported: true, context: "webgl2", })); React.useEffect(() => { setSupport(detectWebGLSupport()); }, []); if (!support.supported) { return <>{fallback ?? }; } return <>{children}; }