Overview
TheuseWebGLRenderer hook provides state management for the WebGL renderer component, including initialization state tracking, shader selection, and fullscreen controls.
Import
Signature
Parameters
This hook takes no parameters.Return value
Whether the WebGL renderer has finished initializing and is ready to render frames.
The currently active shader preset. Possible values:
'default' or 'crt'.Callback to invoke when the renderer has completed initialization. Sets
isReady to true.Stores a reference to the WebGL renderer instance for internal use. Pass
null to clear the reference.Changes the active shader preset. Only applies if a renderer instance has been set via
setRenderer.The shader preset to activate:
'default' or 'crt'.Toggles fullscreen mode for the specified canvas element. Exits fullscreen if already in fullscreen mode.
The canvas element to enter fullscreen mode.
Types
Usage example
Basic usage in overlay mode
App.tsx
Advanced usage with manual renderer control
Features
Shader management
Manages the current shader state and provides a type-safe API for switching between shader presets. The shader change is applied immediately to the stored renderer instance.Initialization tracking
Tracks whether the WebGL renderer has finished initialization, allowing you to conditionally render UI elements that depend on a ready renderer.Fullscreen API integration
Provides a simple wrapper around the Fullscreen API with error handling. Automatically detects if already in fullscreen mode and exits accordingly.Implementation details
State management
UsesuseState for reactive state tracking of isReady and currentShader, ensuring UI updates when these values change.
Renderer reference
Stores the WebGL renderer instance in a ref to avoid re-rendering when the instance is set or updated.Callback stability
All returned callbacks are wrapped inuseCallback to maintain stable references across re-renders, preventing unnecessary re-initialization in consuming components.
Error handling
ThetoggleFullscreen function catches and logs any errors from the Fullscreen API:
Fullscreen API requires user interaction (e.g., button click) to work. Calling
toggleFullscreen from an effect or timer will fail due to browser security restrictions.Related components
- WebGLRenderer: The core WebGL rendering class that this hook manages
- WebGLRendererComponent: Pre-built React component that uses WebGL renderer internally
Shader types
GameLord currently supports two shader presets:| Shader | Description | Use Case |
|---|---|---|
default | Sharp pixel rendering with no effects | Modern clean look, pixel-perfect rendering |
crt | CRT monitor simulation with scanlines and curvature | Authentic retro aesthetic |