XuelesszzZ 94af2b8e7b 6-30-5 | há 1 ano atrás | |
---|---|---|
.. | ||
lib | há 1 ano atrás | |
LICENSE | há 1 ano atrás | |
README.md | há 1 ano atrás | |
package.json | há 1 ano atrás |
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.
npm i canvg
# or
yarn add canvg
Basic module exports:
export default Canvg;
export {
presets
};
Description of all exports you can find in Documentation.
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');
// Start SVG rendering with animations and mouse handling.
v.start();
};
window.onbeforeunload = () => {
v.stop();
};
The third parameter of new Canvg(...)
, Canvg.from(...)
and Canvg.fromString(...)
is options:
interface IOptions {
/**
* WHATWG-compatible `fetch` function.
*/
fetch?: typeof fetch;
/**
* XML/HTML parser from string into DOM Document.
*/
DOMParser?: typeof DOMParser;
/**
* Window object.
*/
window?: Window;
/**
* Whether enable the redraw.
*/
enableRedraw?: boolean;
/**
* Ignore mouse events.
*/
ignoreMouse?: boolean;
/**
* Ignore animations.
*/
ignoreAnimation?: boolean;
/**
* Does not try to resize canvas.
*/
ignoreDimensions?: boolean;
/**
* Does not clear canvas.
*/
ignoreClear?: boolean;
/**
* Scales horizontally to width.
*/
scaleWidth?: number;
/**
* Scales vertically to height.
*/
scaleHeight?: number;
/**
* Draws at a x offset.
*/
offsetX?: number;
/**
* Draws at a y offset.
*/
offsetY?: number;
/**
* Will call the function on every frame, if it returns true, will redraw.
*/
forceRedraw?(): boolean;
/**
* Default `rem` size.
*/
rootEmSize?: number;
/**
* Default `em` size.
*/
emSize?: number;
/**
* Function to create new canvas.
*/
createCanvas?: (width: number, height: number) => HTMLCanvasElement | OffscreenCanvas;
/**
* Function to create new image.
*/
createImage?: (src: string, anonymousCrossOrigin?: boolean) => Promise<CanvasImageSource>;
/**
* Load images anonymously.
*/
anonymousCrossOrigin?: boolean;
}
There are two options presets:
presets.offscreen()
: options for OffscreenCanvas
;presets.node({ DOMParser, canvas, fetch })
: options for NodeJS with node-canvas
.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 😎