Examples
Each example is a complete script. The images on this page are the files they save, rendered by thorvg-canvas when this site is built. The sources and their assets are in examples/.
Shapes and gradients

examples/shapes.mjs
import { init } from 'thorvg-canvas';
const TVG = await init();
const canvas = new TVG.Canvas({ width: 480, height: 240 });
const background = new TVG.Shape()
.appendRect(0, 0, 480, 240)
.fill(15, 23, 42);
const card = new TVG.Shape()
.appendRect(30, 30, 180, 180, { rx: 28, ry: 28 })
.fill(
new TVG.LinearGradient(30, 30, 210, 210)
.addStop(0, [37, 99, 235, 255])
.addStop(1, [147, 51, 234, 255])
);
const ring = new TVG.Shape()
.appendCircle(345, 120, 80, 80)
.stroke({
width: 18,
color: [250, 204, 21, 255],
cap: TVG.StrokeCap.Round,
})
.trimPath(0.05, 0.8);
const dot = new TVG.Shape()
.appendCircle(345, 120, 36, 36)
.fill(244, 63, 94, 255);
canvas.add(background).add(card).add(ring).add(dot).render();
await canvas.toFile('shapes.png');
console.log('Saved shapes.png');
canvas.destroy();
SVG to PNG

examples/svg.mjs
import { readFileSync } from 'node:fs';
import { init } from 'thorvg-canvas';
const TVG = await init();
const canvas = new TVG.Canvas({ width: 240, height: 240, pixelRatio: 2 });
const svg = readFileSync(new URL('./assets/logo.svg', import.meta.url));
const picture = new TVG.Picture().load(svg);
const { width, height } = picture.size();
const scale = Math.min(200 / width, 200 / height);
picture.size(width * scale, height * scale);
picture.translate((240 - width * scale) / 2, (240 - height * scale) / 2);
canvas.add(picture).render();
await canvas.toFile('svg.png');
console.log(`Saved svg.png (${canvas.width} x ${canvas.height})`);
canvas.destroy();
Lottie frames

examples/lottie-frames.mjs
import { readFileSync } from 'node:fs';
import { init } from 'thorvg-canvas';
const TVG = await init();
const file = new URL('./assets/animation.json', import.meta.url);
const lottie = readFileSync(file);
const COLUMNS = 4;
const ROWS = 2;
const CELL = 120;
const WIDTH = COLUMNS * CELL;
const HEIGHT = ROWS * CELL;
const canvas = new TVG.Canvas({ width: WIDTH, height: HEIGHT });
canvas.add(new TVG.Shape().appendRect(0, 0, WIDTH, HEIGHT).fill(255, 255, 255));
const { totalFrames } = new TVG.LottieAnimation().load(lottie).info();
for (let i = 0; i < COLUMNS * ROWS; i++) {
const animation = new TVG.LottieAnimation().load(lottie);
animation.frame(((totalFrames - 1) * (i + 1)) / (COLUMNS * ROWS));
const picture = animation.picture;
picture.size(CELL, CELL);
const column = i % COLUMNS;
const row = Math.floor(i / COLUMNS);
picture.translate(column * CELL, row * CELL);
canvas.add(picture);
}
await canvas.renderAsync();
await canvas.toFile('lottie-frames.png');
console.log('Saved lottie-frames.png');
canvas.destroy();
Text

examples/text.mjs
import { init } from 'thorvg-canvas';
const TVG = await init();
const canvas = new TVG.Canvas({ width: 480, height: 160 });
canvas.add(new TVG.Shape().appendRect(0, 0, 480, 160).fill(15, 23, 42));
const title = new TVG.Text()
.font('default')
.fontSize(44)
.text('Hello ThorVG')
.fill(
new TVG.LinearGradient(0, 0, 300, 0)
.addStop(0, [79, 193, 255, 255])
.addStop(1, [250, 204, 21, 255])
);
title.translate(32, 22);
const subtitle = new TVG.Text()
.font('default')
.fontSize(20)
.text('Rendered in Node.js')
.fill(203, 213, 225);
subtitle.translate(34, 92);
canvas.add(title).add(subtitle).render();
await canvas.toFile('text.png');
console.log('Saved text.png');
canvas.destroy();
Desktop window
thorvg-canvas is another way to build native applications. With SDL3, a Node.js script becomes a desktop app on macOS, Windows and Linux.
npm install thorvg-canvas @kmamal/sdl@next
import { setTimeout as sleep } from 'node:timers/promises';
import sdl from '@kmamal/sdl';
import { init } from 'thorvg-canvas';
const TVG = await init();
const window = sdl.video.createWindow({ title: 'ThorVG', width: 640, height: 480 });
const canvas = new TVG.Canvas({
width: 640,
height: 480,
pixelRatio: window.pixelWidth / window.width,
});
const dot = new TVG.Shape();
canvas.add(dot);
let pointer = { x: 320, y: 240 };
window.on('mouseMove', ({ x, y }) => {
pointer = { x, y };
});
while (!window.destroyed) {
dot.reset().appendCircle(pointer.x, pointer.y, 24, 24).fill(59, 130, 246);
await canvas.update().renderAsync();
if (window.destroyed) break;
const { data } = canvas.getImageData();
const pixels = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
window.render(canvas.width, canvas.height, canvas.width * 4, 'rgba32', pixels);
await sleep(16);
}
canvas.destroy();
examples/sdl-window builds a small sketch app on this loop, with a toolbar, color swatches, brush size, undo and redo, all drawn by ThorVG:
