This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo GLView
A library that provides GLView that acts as an OpenGL ES render target and provides GLContext. Useful for rendering 2D and 3D graphics.
expo-gl provides a View that acts as an OpenGL ES render target, useful for rendering 2D and 3D graphics. On mounting, an OpenGL ES context is created. Its drawing buffer is presented as the contents of the View every frame.
Installation
If you are installing this in an existing React Native app, make sure to install expo in your project.
Usage
High-level APIs
Since the WebGL API is quite low-level, it can be helpful to use higher-level graphics APIs rendering through a GLView underneath. The following libraries integrate popular graphics APIs:
- expo-three for three.js
- expo-processing for Processing
Any WebGL-supporting library that expects a WebGLRenderingContext could be used. Some times such libraries assume a web JavaScript context (such as assuming document). Usually this is for resource loading or event handling, with the main rendering logic still only using pure WebGL. So these libraries can usually still be used with a couple workarounds. The Expo-specific integrations above include workarounds for some popular libraries.
Integration with Reanimated worklets
To use this API inside Reanimated worklet, you need to pass the GL context ID to the worklet and recreate the GL object like in the example below.
For more in-depth example on how to use expo-gl with Reanimated and Gesture Handler you can check this example.
Limitations
Worklet runtime is imposing some limitations on the code that runs inside it, so if you have existing WebGL code, it'll likely require some modifications to run inside a worklet thread.
- Third-party libraries like Pixi.js or Three.js won't work inside the worklet, you can only use functions that have
'worklet'added at the start. - If you need to load some assets to pass to the WebGL code, it needs to be done on the main thread and passed via some reference to the worklet. If you are using
expo-assetsyou can just pass asset object returned byAsset.fromModuleor from hookuseAssetsto therunOnUIfunction. - To implement a rendering loop you need to use
requestAnimationFrame, APIs likesetTimeoutare not supported.
Check Reanimated documentation to learn more.
Remote debugging and GLView
This API does not function as intended with remote debugging enabled. The React Native debugger runs JavaScript on your computer, not the mobile device. GLView requires synchronous native calls that are not supported in Chrome.
API
import { GLView } from 'expo-gl';
No API data file found, sorry!
WebGL API
Once the component is mounted and the OpenGL ES context has been created, the gl object received through the onContextCreate prop becomes the interface to the OpenGL ES context, providing a WebGL API. It resembles a WebGL2RenderingContext in the WebGL 2 spec.
Some older Android devices may not support WebGL2 features. To check whether the device supports WebGL2 it's recommended to use gl instanceof WebGL2RenderingContext.
An additional method gl.endFrameEXP() is present, which notifies the context that the current frame is ready to present. This is similar to a 'swap buffers' API call in other OpenGL platforms.
The following WebGL2RenderingContext methods are currently unimplemented:
getFramebufferAttachmentParameter()getRenderbufferParameter()compressedTexImage2D()compressedTexSubImage2D()getTexParameter()getUniform()getVertexAttrib()getVertexAttribOffset()getBufferSubData()getInternalformatParameter()renderbufferStorageMultisample()compressedTexImage3D()compressedTexSubImage3D()fenceSync()isSync()deleteSync()clientWaitSync()waitSync()getSyncParameter()getActiveUniformBlockParameter()
The pixels argument of texImage2D() must be null, an ArrayBuffer with pixel data, or an object of the form { localUri } where localUri is the file:// URI of an image in the device's file system. Thus, an Asset object is used once .downloadAsync() has been called on it (and completed) to fetch the resource.
For efficiency reasons, the current implementations of the methods don't perform type or bounds checking on their arguments. So, passing invalid arguments may cause a native crash. There are plans to update the API to perform argument checking in upcoming SDK versions.
Currently, the priority for error checking is low since engines generally don't rely on the OpenGL API to perform argument checking; otherwise, checks performed by the underlying OpenGL ES implementation are often sufficient.