canvg is a JavaScript library designed to parse SVG files and render them onto an HTML Canvas element. It handles various SVG features, animations, and interactions, making it suitable for displaying vector graphics in environments where direct SVG rendering might not be feasible or desired. The current stable version is 4.0.3, with recent releases addressing bug fixes and dependency updates. The project appears to have an active release cadence, with multiple minor and patch releases within major versions. A key differentiator is its versatility, supporting rendering in standard browser environments, Web Workers via OffscreenCanvas, and Node.js environments, requiring appropriate polyfills or peer dependencies for server-side rendering. It provides a programmatic API to load SVG content from URLs or strings and control its rendering lifecycle.
npm install canvgVerified import paths — ran on the pinned version, not inferred.
This Node.js example demonstrates how to parse an SVG string and render it to a PNG file using `canvg` with the `node` preset.
Change `import Canvg from 'canvg'` to `import { Canvg } from 'canvg'`.Ensure your Node.js runtime is version 12 or newer. For browser usage, ensure you are using a modern browser that supports ES modules.
Update your TypeScript version to 4 or higher in your project's `devDependencies`.
Install required peer dependencies manually: `npm i canvas @xmldom/xmldom node-fetch`.
Upgrade to `canvg@4.0.3` or `canvg@3.0.11` (or newer patch versions) immediately.
If providing `DOMParser` for Node.js, ensure you import it from `'@xmldom/xmldom'` instead of `'xmldom'`.
Change your import statement from `import Canvg from 'canvg'` to `import { Canvg } from 'canvg'`.For Node.js, pass a `DOMParser` instance (e.g., from `@xmldom/xmldom`) via the `presets.node` configuration: `presets.node({ DOMParser })`.For Node.js, pass the `canvas` library (e.g., from `canvas` npm package) via `presets.node` configuration: `presets.node({ canvas })`.For Node.js, provide a `fetch` polyfill (e.g., `node-fetch`) via the `presets.node` configuration: `presets.node({ fetch })`.