Skip to content

Commit

Permalink
catch missing extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane98c committed Oct 16, 2024
1 parent 22bb883 commit b729cfa
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/regl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,43 @@ export const useRegl = () => {
const Regl = ({ style, extensions, children }) => {
const regl = useRef()
const [ready, setReady] = useState(false)
const [error, setError] = useState(null)

const ref = useCallback((node) => {
if (node !== null) {
regl.current = _regl({
container: node,
extensions: ['OES_texture_float', 'OES_element_index_uint'],
})
setReady(true)
try {
const canvas = document.createElement('canvas')
const context =
canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
if (!context) {
throw new Error('WebGL is not supported in this browser.')
}

const requiredExtensions = [
'OES_texture_float',
'OES_element_index_uint',
]
const missingExtensions = requiredExtensions.filter(
(ext) => !context.getExtension(ext)
)

if (missingExtensions.length > 0) {
throw new Error(
`Required WebGL extensions not supported: ${missingExtensions.join(
', '
)}`
)
}
canvas.remove()
regl.current = _regl({
container: node,
extensions: requiredExtensions,
})
setReady(true)
} catch (error) {
console.error('Error initializing WebGL:', error)
setError('Sorry, your device is not supported.')
}
}
}, [])

Expand All @@ -35,6 +64,10 @@ const Regl = ({ style, extensions, children }) => {
}
}, [])

if (error) {
return <div style={{ marginTop: '56px', fontSize: '20px' }}>{error}</div>
}

return (
<ReglContext.Provider
value={{
Expand Down

0 comments on commit b729cfa

Please sign in to comment.