Overview
TheGlCanvas component provides a hardware-accelerated canvas for ThorVG vector graphics using WebGL. It uses GPU-based rendering for superior performance with complex scenes compared to the CPU-based SwCanvas.
When to Use GlCanvas
- High performance is critical
- Complex scenes with many shapes (1000+)
- Animations and real-time updates
- Large canvas dimensions
- WebGL is available and enabled in the target environment
Import
Basic Usage
Props
Required unique ID for the canvas element. Used by the WebGL context to bind to the canvas.This must be a unique identifier in your application, as WebGL requires a specific DOM element reference.
Width of the canvas in CSS pixels. This determines the logical width of the canvas element.
Height of the canvas in CSS pixels. This determines the logical height of the canvas element.
Device pixel ratio for high-DPI displays. Controls the actual resolution of the WebGL buffer.
Higher values provide sharper rendering on high-DPI screens but require more GPU memory and processing.If not specified, defaults to
window.devicePixelRatio to automatically match the device’s pixel density.Optional function to customize the location of WebAssembly files. This is useful for:
- Custom asset paths in bundlers
- CDN configurations
- Non-standard public directory structures
path: The requested file pathprefix: The default prefix for the file
Additional Props
TheGlCanvas component extends all standard HTML canvas element props (from ComponentPropsWithoutRef<'canvas'>), including:
className: CSS class namestyle: Inline stylesonClick,onMouseMove, etc.: Event handlers- And all other standard canvas attributes
Advanced Examples
High-Performance Animation
High-DPI Display Support
Custom WASM File Location
Complex Scene with Many Shapes
Multiple Canvases
Type Signature
Implementation Details
TheGlCanvas component:
- Initializes ThorVG: Loads the WebAssembly module and creates a ThorVG engine instance
- Creates WebGL Canvas: Sets up a WebGL rendering context with ABGR8888S (straight alpha) color space
- React Reconciler: Establishes a custom React reconciler to handle declarative child components
- Automatic Updates: Updates the canvas whenever children or props change
- Resource Cleanup: Properly disposes of WebGL and ThorVG resources on unmount
Performance Characteristics
- Rendering: GPU-accelerated, hardware-optimized
- Compatibility: Requires WebGL support (available in 97%+ of browsers)
- Memory: Higher GPU memory usage for large buffers
- Best for: Complex scenes with 1000+ shapes, animations, large dimensions
Color Space
TheGlCanvas uses ABGR8888S (straight alpha) color space, which is optimized for WebGL rendering. This differs from SwCanvas which uses ABGR8888 (premultiplied alpha).
Differences from SwCanvas
| Feature | GlCanvas | SwCanvas |
|---|---|---|
| Rendering | GPU (WebGL) | CPU (Software) |
| Performance | Excellent for complex scenes | Good for simple scenes |
| Compatibility | Requires WebGL | Universal |
| ID Prop | Required | Not needed |
| Color Space | ABGR8888S (straight alpha) | ABGR8888 (premultiplied) |
| Memory | Higher GPU memory | Lower system memory |
| Best Use | Complex graphics, animations | Simple graphics, compatibility |