UNPKG

3.8 kBMarkdownView Raw
1# saveSvgAsPng
2
3## Installation
4
5```
6npm install save-svg-as-png
7```
8
9## Usage
10
11To save a PNG, include the script `saveSvgAsPng.js` in your page, then call the `saveSvgAsPng` function with an SVG node and a filename:
12
13```javascript
14saveSvgAsPng(document.getElementById("diagram"), "diagram.png");
15```
16
17The filename is the preferred filename when saving the image to the file system. The browser may change the name of the file if there is already a file by that name in the target directory.
18
19If you want to scale the image up or down, you can pass a scale factor in an options object:
20
21```javascript
22saveSvgAsPng(document.getElementById("diagram"), "diagram.png", {scale: 0.5});
23```
24
25Other options are documented below.
26
27If you just want a dataURI for an SVG, you can call `svgAsDataUri` with an SVG node, options, and a callback:
28
29```javascript
30svgAsDataUri(document.getElementById("diagram"), {}, function(uri) {
31 ...
32});
33```
34
35If you want a dataURI of a PNG generated from an SVG, you can call `svgAsPngUri` with an SVG node, options, and a callback:
36
37```javascript
38svgAsPngUri(document.getElementById("diagram"), {}, function(uri) {
39 ...
40});
41```
42
43Compatible with [browserify](http://browserify.org/) and [requirejs](http://requirejs.org).
44
45If you want to use TypeScript, necessary [type definitions](https://github.com/martianov/typed-save-svg-as-png) are available in [Typings](https://github.com/typings/typings) [public registry](https://github.com/typings/registry).
46
47### Options
48
49- `backgroundColor` — Creates a PNG with the given background color. Defaults to transparent.
50- `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
51- `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
52- `encoderType` - A DOMString indicating the image format. The default type is image/png.
53- `height` - Specify the image's height. Defaults to the viewbox's height if given, or the element's non-percentage height, or the element's bounding box's height, or the element's CSS height, or the computed style's height, or 0.
54- `left` - Specify the viewbox's left position. Defaults to 0.
55- `modifyCss` - A function that takes a CSS rule's selector and properties and returns a string of CSS. Supercedes `selectorRemap` and `modifyStyle`. Useful for modifying properties only for certain CSS selectors.
56- `modifyStyle` - A function that takes a CSS rule's properties and returns a string of CSS. Useful for modifying properties before they're inlined into the SVG.
57- `scale` — Changes the resolution of the output PNG. Defaults to `1`, the same dimensions as the source SVG.
58- `selectorRemap` — A function that takes a CSS selector and produces its replacement in the CSS that's inlined into the SVG. Useful if your SVG style selectors are scoped by ancestor elements in your HTML document.
59- `top` - Specify the viewbox's top position. Defaults to 0.
60- `width` - Specify the image's width. Defaults to the viewbox's width if given, or the element's non-percentage width, or the element's bounding box's width, or the element's CSS width, or the computed style's width, or 0.
61
62### Testing
63
64run tests with [tape](https://www.npmjs.com/package/tape)
65```bash
66npm test
67```
68
69## Support
70
71[Chrome limits data URIs to 2MB](http://stackoverflow.com/questions/695151/data-protocol-url-size-limitations/41755526#41755526), so you may have trouble generating PNGs beyod a certain size.
72
73Internet Explorer will only work if [canvg](https://github.com/canvg/canvg) is passed in, otherwise it will throw a `SecurityError` when calling `toDataURL` on a canvas that's been written to. [canvg](https://github.com/canvg/canvg) may have it's own issues with SVG support, so ensure you test the output after switching.