Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch missing webgl extensions #123

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other elements on the page will still be rendered, right? Maybe we tweak language to be more specific (and consistent with minimaps carbonplan/minimaps#34).

Suggested change
setError('Sorry, your device is not supported.')
setError('Your device doesn't support rendering this map.')

}
}
}, [])

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
Loading