What is canvg?
The canvg npm package is a versatile library that allows users to parse and render SVG (Scalable Vector Graphics) to a Canvas element in a browser or on the server-side using Node.js. It is particularly useful for converting SVGs into Canvas for manipulation, rendering, or exporting purposes.
What are canvg's main functionalities?
Rendering SVG to Canvas
This code sample demonstrates how to load an SVG file, render it onto a Canvas, and then save the Canvas output as a PNG file. This is useful for server-side generation of images from SVG files.
const canvg = require('canvg');
const Canvas = require('canvas');
const fs = require('fs');
const canvas = Canvas.createCanvas(800, 600);
const ctx = canvas.getContext('2d');
const svg = fs.readFileSync('path/to/your/svgfile.svg', 'utf-8');
canvg(ctx, svg);
const out = fs.createWriteStream('path/to/output.png');
const stream = canvas.createPNGStream();
stream.pipe(out);
Manipulating SVG before rendering
This example shows how to manipulate SVG data by changing its color before rendering it to a Canvas. This is useful for dynamically altering the appearance of SVGs based on runtime conditions.
const canvg = require('canvg');
const Canvas = require('canvas');
const fs = require('fs');
const canvas = Canvas.createCanvas(800, 600);
const ctx = canvas.getContext('2d');
let svg = fs.readFileSync('path/to/your/svgfile.svg', 'utf-8');
// Modify SVG data
svg = svg.replace('fill:#000000', 'fill:#123456');
// Render modified SVG to canvas
canvg(ctx, svg);
Other packages similar to canvg
fabric
Fabric.js is a powerful and rich graphics library, allowing you to manipulate and render both SVG and Canvas elements. Compared to canvg, Fabric.js offers a broader set of features for interactive object model on canvas, including a full suite of interactive capabilities such as drag and drop, object manipulation, and events.
svg2canvas
svg2canvas is another library that focuses on converting SVG documents into Canvas elements. While similar in purpose to canvg, svg2canvas might have different implementation details or performance characteristics, making it a viable alternative depending on specific project requirements.
canvg
JavaScript SVG parser and renderer on Canvas. It takes the URL to the SVG file or the text of the SVG file, parses it in JavaScript and renders the result on Canvas. It also can be used to rasterize SVG images.
Quickstart
•
Docs
•
Demo
Quickstart
Install this library using your favorite package manager:
pnpm add canvg
yarn add canvg
npm i canvg
Then, just import Canvg
and use it:
import { Canvg } from 'canvg';
let v = null;
window.onload = async () => {
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
v = await Canvg.from(ctx, './svgs/1.svg');
v.start();
};
window.onbeforeunload = () => {
v.stop();
};
Description of all exports you can find in Documentation.
Docs
What's implemented?
The end goal is everything from the SVG spec. The majority of the rendering and animation is working. If you would like to see a feature implemented, don't hesitate to add it to the issues list, or better is to create pull request 😎