Giter Site home page Giter Site logo

savesvg's Introduction

SaveSVG.js

Have you ever wanted an easy way to allow users to save an SVG file (e.g. D3.js chart) to their computer. SaveSVG is a JavaScript library which allows you to help your users download SVG content from your webpage. SaveSVG goes beyond just copying your SVG element to a file. It allows you to specify CSS files and SVG <use> element files to recompile a complete SVG with all styles and hrefs for saving.

Getting started

Requirements

  • This library requires D3.js v5. Remember to add this to your HTML <head> using:
<script src="https://d3js.org/d3.v5.min.js"></script>
  • You must have all your CSS styles in one file. For e.g. in the dist example it is all located in the dist/css/styles.css file.
  • You must have all your SVG <use> definitions in one file. For e.g. in the dist example it is all located in the dist/images/monos.svg file. This file must have <symbol> defined for each component being fetched using use with id attributes which would match your SVG in your HTML.

Usage

To add the library to your project, simply copy the SaveSVG.min.js located in the dist folder to your project folder. Then simply add it to your HTML using the <script> tags like

<script src='SaveSVG.min.js'></script>

Once you have added this to your HTML, you can trigger the save function by calling

SaveSVG.save(svgID, cssFile, useFile);

where:

  • svgID is the ID of your SVG element in your HTML as a string. For e.g. 'mySVG'.
  • cssFile is the relative path to your cssFile from your HTML file as a string. For e.g. 'css/styles.css'.
  • useFile is the relative path to your file with the <use> <defs> from your HTML file as a string. For e.g. 'images/monos.svg'.

License

MIT

savesvg's People

Contributors

akulmehta avatar

Watchers

James Cloos 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.