dc8632b14a4e0b55018571ddd2f1ca16f1a70aa8ec7f151d7d49f4e8ab43f8ab36b5e8e6fd000ba022695a76c5899584e112a96828f9d802deab6437cc2e16 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # saveSvgAsPng
  2. ## Installation
  3. ```
  4. npm install save-svg-as-png
  5. ```
  6. ## Prerequisites
  7. SaveSvgAsPng relies on JavaScript promises, so any browsers that don't natively support the standard `Promise` object will need to have a polyfill.
  8. ## Usage
  9. To save a PNG, include the script `saveSvgAsPng.js` in your page, then call the `saveSvgAsPng` function with an SVG node and a filename:
  10. ```javascript
  11. saveSvgAsPng(document.getElementById("diagram"), "diagram.png");
  12. ```
  13. The 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.
  14. If you want to scale the image up or down, you can pass a scale factor in an options object:
  15. ```javascript
  16. saveSvgAsPng(document.getElementById("diagram"), "diagram.png", {scale: 0.5});
  17. ```
  18. Other options are documented below.
  19. If you just want a dataURI for an SVG, you can call `svgAsDataUri`, which returns a promise:
  20. ```javascript
  21. svgAsDataUri(document.getElementById("diagram"), options).then(uri => ...);
  22. ```
  23. If you want a dataURI of a PNG generated from an SVG, you can call `svgAsPngUri`, which also returns a promise:
  24. ```javascript
  25. svgAsPngUri(document.getElementById("diagram"), options).then(uri => ...);
  26. ```
  27. Compatible with [browserify](http://browserify.org/) and [requirejs](http://requirejs.org).
  28. If 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).
  29. ### Options
  30. - `backgroundColor` — Creates a PNG with the given background color. Defaults to transparent.
  31. - `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
  32. - `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
  33. - `encoderType` - A DOMString indicating the image format. The default type is image/png.
  34. - `fonts` - A list of `{text, url, format}` objects the specify what fonts to inline in the SVG. Omitting this option defaults to auto-detecting font rules.
  35. - `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.
  36. - `left` - Specify the viewbox's left position. Defaults to 0.
  37. - `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.
  38. - `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.
  39. - `scale` — Changes the resolution of the output PNG. Defaults to `1`, the same dimensions as the source SVG.
  40. - `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.
  41. - `top` - Specify the viewbox's top position. Defaults to 0.
  42. - `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.
  43. - `excludeUnusedCss` - Exclude CSS rules that don't match any elements in the SVG.
  44. - `excludeCss` - Exclude all CSS rules
  45. ### Testing
  46. run tests with [tape](https://www.npmjs.com/package/tape)
  47. ```bash
  48. npm test
  49. ```
  50. ## Support
  51. [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.
  52. Internet 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 make sure to test the output.