Skip to main content

API

The classes, enums and their methods are the @thorvg/webcanvas API. This page covers Node.js specific APIs.

note

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​

OptionDescription
rendererOnly 'sw'. Other values throw
onErrorReceives errors instead of throwing them. See Errors
threadsThorVG thread count. Defaults to 0. See Threads
locateFileIgnored

ThorVG​

The namespace returned by init(). It is the WebCanvas namespace without Video. TVG.Canvas is the Node.js canvas.

MemberReference
Shape, Scene, Picture, TextShape, Scene, Picture, Text
Animation, LottieAnimationAnimation, LottieAnimation
LinearGradient, RadialGradient, ConicGradientLinearGradient, RadialGradient, ConicGradient
Font, Accessor, PaintFont, Accessor, Paint
BlendMethod, MaskMethod, StrokeCap, StrokeJoin, FillRule, PathCommand, GradientSpread, SceneEffect, TextWrapMode, ColorSpace, FilterMethod, EngineOptionEnums
CanvasTVG.Canvas
versionThorVG 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.

OptionDescription
width, heightDrawing size. Default 800 × 600
pixelRatioOutput scale. Default 1
engineOptionTVG.EngineOption value for the engine
enableDevicePixelRatioIgnored. Use pixelRatio

Added members​

MemberDescription
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, heightOutput 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.