Overview
TheSwCanvas component provides a software-rendered canvas for ThorVG vector graphics. It uses CPU-based rendering, making it highly compatible across different platforms and browsers, though it may be slower than the WebGL variant for complex scenes.
When to Use SwCanvas
- Maximum browser and device compatibility is required
- WebGL is not available or disabled
- Simple to moderate complexity graphics
- Server-side rendering or environments without GPU access
- Testing and development where consistency is more important than performance
Import
Basic Usage
Props
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 canvas buffer.
Higher values provide sharper rendering on high-DPI screens but require more 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
TheSwCanvas 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-DPI Display Support
Custom WASM File Location
Responsive Canvas with Event Handlers
Type Signature
Implementation Details
TheSwCanvas component:
- Initializes ThorVG: Loads the WebAssembly module and creates a ThorVG engine instance
- Creates Software Canvas: Sets up a software rendering context with ABGR8888 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 ThorVG resources on unmount
Performance Characteristics
- Rendering: CPU-based, single-threaded
- Compatibility: Works on all browsers supporting WebAssembly
- Memory: Lower memory usage compared to GlCanvas
- Best for: Simple to moderate complexity scenes with < 1000 shapes