API
The classes, enums and their methods are the @thorvg/webcanvas API. This page covers Node.js specific APIs.
This package follows @thorvg/webcanvas 1.1.4. The WebCanvas reference describes the latest release, which can include APIs that 1.1.4 does not have yet.
init(options?)
function init(options?: InitOptions): Promise<ThorVG>;
Loads the native addon, starts ThorVG and returns the ThorVG namespace. Later calls return the same namespace.
initSync(options?)
function initSync(options?: InitOptions): ThorVG;
init() without the promise, for scripts and workers.
InitOptions
| Option | Description |
|---|---|
renderer | Only 'sw'. Other values throw |
onError | Receives errors instead of throwing them. See Errors |
threads | ThorVG thread count. Defaults to 0. See Threads |
locateFile | Ignored |
ThorVG
The namespace returned by init(). It is the WebCanvas namespace without Video. TVG.Canvas is the Node.js canvas.
| Member | Reference |
|---|---|
Shape, Scene, Picture, Text | Shape, Scene, Picture, Text |
Animation, LottieAnimation | Animation, LottieAnimation |
LinearGradient, RadialGradient, ConicGradient | LinearGradient, RadialGradient, ConicGradient |
Font, Accessor, Paint | Font, Accessor, Paint |
BlendMethod, MaskMethod, StrokeCap, StrokeJoin, FillRule, PathCommand, GradientSpread, SceneEffect, TextWrapMode, ColorSpace, FilterMethod, EngineOption | Enums |
Canvas | TVG.Canvas |
version | ThorVG engine version |
term() | Resets the initialization |
The classes are also exported from the package, so import { Shape } from 'thorvg-canvas' works after init().
TVG.Canvas
const canvas = new TVG.Canvas({ width: 600, height: 400 });
The WebCanvas Canvas class, rendering into memory. See Canvas.
| Option | Description |
|---|---|
width, height | Drawing size. Default 800 × 600 |
pixelRatio | Output scale. Default 1 |
engineOption | TVG.EngineOption value for the engine |
enableDevicePixelRatio | Ignored. Use pixelRatio |
Added members
| Member | Description |
|---|---|
renderAsync() | Renders on the libuv thread pool. See Threads |
toFile(path, format?), toFileSync(path, format?) | Saves the last rendered frame. See Output |
toBuffer(format?), toBufferSync(format?) | Encodes the last rendered frame. See Output |
getImageData() | A copy of the last rendered frame as RGBA. See Output |
width, height | Output size in pixels |
[Symbol.dispose]() | Calls destroy(), for using |
format is 'png' or 'raw'.
Errors
ThorVG calls that fail throw a ThorVGError with a ThorVGResultCode. With onError, the error is passed to the handler instead:
const TVG = await init({
onError: (error, context) => console.warn(context.operation, error.message),
});
ThorVGError and ThorVGResultCode are exported from the package.
Types
The package re-exports the WebCanvas types, so you can import them from thorvg-canvas:
import type { Canvas, Shape, Picture, LottieAnimation, ColorStop } from 'thorvg-canvas';
It also exports ThorVGNamespace, NodeCanvasOptions, ImageFormat and RawImage.