Creates a new Canvas rendering context.
The renderer is determined by the global setting from ThorVG.init().
CSS selector for the target HTML canvas element (e.g., '#canvas', '.my-canvas'),
the HTMLCanvasElement itself, or an OffscreenCanvas
Configuration options for the canvas
// Initialize with renderer
const TVG = await ThorVG.init({ renderer: 'gl' });
// Basic canvas with default options (DPR enabled by default)
const canvas = new TVG.Canvas('#canvas');
// Canvas with custom size
const TVG = await ThorVG.init({ renderer: 'wg' });
const canvas = new TVG.Canvas('#myCanvas', {
width: 1920,
height: 1080
});
Gets the current device pixel ratio applied to this canvas.
ThorVG uses an optimized DPR formula for best performance:
1 + ((window.devicePixelRatio - 1) * 0.75)
This provides a balance between visual quality and rendering performance, especially on high-DPI displays.
The current effective DPR value, or 1.0 if DPR scaling is disabled
// Getting the current DPR
const canvas = new TVG.Canvas('#canvas', {
enableDevicePixelRatio: true
});
console.log(canvas.dpr); // e.g., 1.75 on a 2x display
console.log(window.devicePixelRatio); // e.g., 2.0
// Using DPR for responsive calculations
const canvas = new TVG.Canvas('#canvas');
const shape = new TVG.Shape();
// Adjust stroke width based on DPR for consistent appearance
const strokeWidth = 2 / canvas.dpr;
shape.appendCircle(100, 100, 50)
.stroke(255, 0, 0, 255)
.strokeWidth(strokeWidth);
CanvasOptions.enableDevicePixelRatio for controlling DPR scaling
Adds a Paint object to the canvas for rendering.
Paint objects include Shape, Scene, Picture, Text, and Animation.picture. Objects are rendered in the order they are added (painter's algorithm).
The canvas instance for method chaining
Clears all Paint objects from the canvas and renders an empty frame.
This is equivalent to remove() without arguments, but also immediately renders the cleared canvas.
The canvas instance for method chaining
Updates the canvas state before rendering.
This method should be called before render when working with animations or when Paint objects have been modified. It ensures all transformations and changes are processed.
The canvas instance for method chaining
// Animation loop pattern
function animate() {
animation.frame(currentFrame++);
canvas.update().render();
requestAnimationFrame(animate);
}
For static scenes, calling render alone is sufficient. For animated content, always call update() before render().
Renders all Paint objects to the canvas.
This method draws all added Paint objects to the canvas using the configured rendering backend (Software, WebGL, or WebGPU).
The canvas instance for method chaining
// Animation loop
function animate() {
canvas.update().render();
requestAnimationFrame(animate);
}
For animated content, call update before render(). For static scenes, render() can be called directly.
Resizes the canvas to new dimensions.
New width in pixels
New height in pixels
The canvas instance for method chaining
Sets the viewport for rendering a specific region of the canvas.
The viewport defines the rectangular region where rendering occurs. Useful for rendering to a portion of the canvas or implementing split-screen views.
X coordinate of the viewport origin
Y coordinate of the viewport origin
Viewport width
Viewport height
The canvas instance for method chaining
Destroys the canvas and frees its WASM memory.
After calling destroy(), this canvas instance cannot be used. The ThorVG module remains loaded and new canvases can be created.
Canvas rendering context for ThorVG vector graphics.
Manages the rendering pipeline and provides methods for adding/removing Paint objects and controlling the render loop.
Example
Example