Skip to content
Myr

fix: handle hardware acceleration disabled scenario in demo site (#3082)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: handle hardware acceleration disabled scenario in demo site This commit improves the demo site's error handling when hardware acceleration is disabled in the browser. The changes include: - Improved fallback UI with a more informative message The fallback scene now shows: - A static image of the Zustand ogimage This fixes the issue where the demo site would break without any feedback when hardware acceleration is disabled. Testing: - fallback works when hardware acceleration is disabled * fixed code format ---------

authored by Rahul Singh · committed by GitHub · parent 21fc1b1 · Browse files · 1 file+68 added−30 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

Diff stats

1 file+68 added−30 removed

  • other:+68 added−30 removed

1 file+68 added−30 removed

modified:examples/demo/src/components/Scene.jsx+68 added-30 removed
145 unmodified lines
146 </EffectComposer>147 )148}149150function FallbackScene() {151 return (152 <div153 style={{154 position: 'absolute',155 top: 0,156 left: 0,157 width: '100%',158 height: '100%',159 display: 'flex',160 alignItems: 'center',161 justifyContent: 'center',162 background: '#010101',163 }}164 >165 <img166 src="/ogimage.jpg"167 alt="Zustand Bear"168 style={{169 width: '100%',170 height: '100%',171 objectFit: 'cover',172 }}173 />174 </div>175 )176}177178export default function Scene() {179 const [error, setError] = useState(null)180181 if (error) {182 return <FallbackScene />183 }184185 return (152 <Canvas>186 <Canvas onError={setError}>187 <Experience />188 <Effects />189 </Canvas>190 )191}192159function Canvas({ children }) {193function Canvas({ children, onError }) {194 extend({ Mesh, PlaneGeometry, Group })195 const canvas = useRef(null)196 const root = useRef(null)197 useLayoutEffect(() => {198 try {164 if (!root.current) {165 root.current = createRoot(canvas.current).configure({166 events,167 orthographic: true,168 gl: { antialias: false },169 camera: { zoom: 5, position: [0, 0, 200], far: 300, near: 50 },170 onCreated: (state) => {171 state.events.connect(document.getElementById('root'))172 state.setEvents({173 compute: (event, state) => {174 state.pointer.set(175 (event.clientX / state.size.width) * 2 - 1,176 -(event.clientY / state.size.height) * 2 + 1,177 )178 state.raycaster.setFromCamera(state.pointer, state.camera)179 },180 })181 },182 })199 if (!root.current) {200 root.current = createRoot(canvas.current).configure({201 events,202 orthographic: true,203 gl: { antialias: false },204 camera: { zoom: 5, position: [0, 0, 200], far: 300, near: 50 },205 onCreated: (state) => {206 state.events.connect(document.getElementById('root'))207 state.setEvents({208 compute: (event, state) => {209 state.pointer.set(210 (event.clientX / state.size.width) * 2 - 1,211 -(event.clientY / state.size.height) * 2 + 1,212 )213 state.raycaster.setFromCamera(state.pointer, state.camera)214 },215 })216 },217 })218 }219 const resize = () =>220 root.current.configure({221 width: window.innerWidth,222 height: window.innerHeight,223 })224 window.addEventListener('resize', resize)225 root.current.render(children)226 return () => window.removeEventListener('resize', resize)227 } catch (e) {228 onError?.(e)229 }184 const resize = () =>185 root.current.configure({186 width: window.innerWidth,187 height: window.innerHeight,188 })189 window.addEventListener('resize', resize)190 root.current.render(children)191 return () => window.removeEventListener('resize', resize)192 }, [children])230 }, [children, onError])231232 return (233 <canvas234 ref={canvas}
10 unmodified lines