GithubHelp home page GithubHelp logo

anvaka / savesvgaspng Goto Github PK

View Code? Open in Web Editor NEW

This project forked from exupero/savesvgaspng

0.0 3.0 2.0 184 KB

Save SVGs as PNGs from the browser.

License: MIT License

HTML 66.91% JavaScript 33.09%

savesvgaspng's Introduction

saveSvgAsPng

Installation

npm install save-svg-as-png

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:

saveSvgAsPng(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:

saveSvgAsPng(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 with an SVG node, options, and a callback:

svgAsDataUri(document.getElementById("diagram"), {}, function(uri) {
  ...
});

If you want a dataURI of a PNG generated from an SVG, you can call svgAsPngUri with an SVG node, options, and a callback:

svgAsPngUri(document.getElementById("diagram"), {}, function(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.
  • left - Specify the viewbox's left position. Defaults to 0.
  • 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.
  • scale — Changes the resolution of the output PNG. Defaults to 1, 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.
  • modifyStyle - A function that takes CSS styles and returns replacement styles.
  • 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.
  • encoderType - A DOMString indicating the image format. The default type is image/png.
  • encoderOptions - A Number between 0 and 1 indicating image quality. The default is 0.8
  • canvg - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer

Testing

run tests with tape

npm test

Support

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 ensure you test the output after switching.

savesvgaspng's People

Contributors

exupero avatar jephuff avatar anvaka avatar devsli avatar recallfx avatar balmma avatar thaggie avatar yanofsky avatar arunkjn avatar wangyuheng avatar guilhermesimoes avatar linuxbozo avatar mvalipour avatar soumith avatar thibaudcolas avatar trhall avatar bastrapp avatar guaizi149 avatar nsonnad avatar stryju avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.