Installations
npm install scratchhw-save-svg-as-png
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
12.20.0
NPM Version
6.14.8
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
HTML (60.99%)
JavaScript (39.01%)
Developer
exupero
Download Statistics
Total Downloads
428
Last Day
1
Last Week
2
Last Month
4
Last Year
64
GitHub Statistics
1,089 Stars
181 Commits
358 Forks
34 Watching
4 Branches
35 Contributors
Bundle Size
8.56 kB
Minified
3.47 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.4.18
Package Id
scratchhw-save-svg-as-png@1.4.18
Unpacked Size
218.35 kB
Size
71.46 kB
File Count
14
NPM Version
6.14.8
Node Version
12.20.0
Total Downloads
Cumulative downloads
Total Downloads
428
Last day
0%
1
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
-20%
4
Compared to previous month
Last year
12.3%
64
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
saveSvgAsPng
Installation
npm install save-svg-as-png
Prerequisites
SaveSvgAsPng relies on JavaScript promises, so any browsers that don't natively support the standard Promise
object will need to have a polyfill.
Usage
To save a PNG, include the script saveSvgAsPng.js
in your page, then call the saveSvgAsPng
function with an SVG node and a filename:
1saveSvgAsPng(document.getElementById("diagram"), "diagram.png");
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.
If you want to scale the image up or down, you can pass a scale factor in an options object:
1saveSvgAsPng(document.getElementById("diagram"), "diagram.png", {scale: 0.5});
Other options are documented below.
If you just want a dataURI for an SVG, you can call svgAsDataUri
, which returns a promise:
1svgAsDataUri(document.getElementById("diagram"), options).then(uri => ...);
If you want a dataURI of a PNG generated from an SVG, you can call svgAsPngUri
, which also returns a promise:
1svgAsPngUri(document.getElementById("diagram"), options).then(uri => ...);
Compatible with browserify and requirejs.
If you want to use TypeScript, necessary type definitions are available in Typings public registry.
Options
backgroundColor
— Creates a PNG with the given background color. Defaults to transparent.canvg
- If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet ExplorerencoderOptions
- A Number between 0 and 1 indicating image quality. The default is 0.8encoderType
- A DOMString indicating the image format. The default type is image/png.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.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.left
- Specify the viewbox's left position. Defaults to 0.modifyCss
- A function that takes a CSS rule's selector and properties and returns a string of CSS. SupercedesselectorRemap
andmodifyStyle
. Useful for modifying properties only for certain CSS selectors.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.scale
— Changes the resolution of the output PNG. Defaults to1
, the same dimensions as the source SVG.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.top
- Specify the viewbox's top position. Defaults to 0.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.excludeUnusedCss
- Exclude CSS rules that don't match any elements in the SVG.excludeCss
- Exclude all CSS rules
Testing
run tests with tape
1npm test
Support
Chrome limits data URIs to 2MB, so you may have trouble generating PNGs beyod a certain size.
Internet Explorer will only work if canvg is passed in, otherwise it will throw a SecurityError
when calling toDataURL
on a canvas that's been written to. canvg may have it's own issues with SVG support, so make sure to test the output.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/29 approved changesets -- score normalized to 1
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'gh-pages'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 7 are checked with a SAST tool
Reason
27 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-x3m3-4wpv-5vgc
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
Score
1.9
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More