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.
Demo
Playground
Install
npm i canvg
yarn add canvg
Usage
Basic module exports:
export default Canvg;
export {
presets
};
Description of all exports you can find in Documentation.
Example
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();
};
OffscreenCanvas
import Canvg, {
presets
} from 'canvg';
self.onmessage = async (event) => {
const {
width,
height,
svg
} = event.data;
const canvas = new OffscreenCanvas(width, height);
const ctx = canvas.getContext('2d');
const v = await Canvg.from(ctx, svg, presets.offscreen());
await v.render();
const blob = await canvas.convertToBlob();
const pngUrl = URL.createObjectURL(blob);
self.postMessage({
pngUrl
});
};
OffscreenCanvas
browsers compatibility.
NodeJS
import {
promises as fs
} from 'fs';
import {
DOMParser
} from 'xmldom';
import * as canvas from 'canvas';
import fetch from 'node-fetch';
import Canvg, {
presets
} from 'canvg';
const preset = presets.node({
DOMParser,
canvas,
fetch
});
(async (output, input) => {
const svg = await fs.readFile(input, 'utf8');
const canvas = preset.createCanvas(800, 600);
const ctx = canvas.getContext('2d');
const v = Canvg.fromString(ctx, svg, preset);
await v.render();
const png = canvas.toBuffer();
await fs.writeFile(output, png);
})(
process.argv.pop(),
process.argv.pop()
);
Resize
import Canvg, {
presets
} from 'canvg';
self.onmessage = async (event) => {
const {
width,
height,
svg
} = event.data;
const canvas = new OffscreenCanvas(width, height);
const ctx = canvas.getContext('2d');
const v = await Canvg.from(ctx, svg, presets.offscreen());
v.resize(width, height, 'xMidYMid meet');
await v.render();
const blob = await canvas.convertToBlob();
const pngUrl = URL.createObjectURL(blob);
self.postMessage({
pngUrl
});
};
Browser
<script type="text/javascript" src="https://unpkg.com/canvg@3.0.4/lib/umd.js"></script>
<script type="text/javascript">
window.onload = () => {
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
v = canvg.Canvg.fromString(ctx, '<svg width="600" height="600"><text x="50" y="50">Hello World!</text></svg>');
v.start();
};
</script>
<canvas />
Options
The third parameter of new Canvg(...)
, Canvg.from(...)
and Canvg.fromString(...)
is options:
interface IOptions {
fetch?: typeof fetch;
DOMParser?: typeof DOMParser;
window?: Window;
enableRedraw?: boolean;
ignoreMouse?: boolean;
ignoreAnimation?: boolean;
ignoreDimensions?: boolean;
ignoreClear?: boolean;
scaleWidth?: number;
scaleHeight?: number;
offsetX?: number;
offsetY?: number;
forceRedraw?(): boolean;
rootEmSize?: number;
emSize?: number;
createCanvas?: (width: number, height: number) => HTMLCanvasElement | OffscreenCanvas;
createImage?: (src: string, anonymousCrossOrigin?: boolean) => Promise<CanvasImageSource>;
anonymousCrossOrigin?: boolean;
}
Options presets
There are two options presets:
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 😎