npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-crowbar

v0.7.0

Published

A library based on a Chrome-specific bookmarklet that extracts SVG nodes and accompanying styles from an HTML document and downloads them as an SVG file

Downloads

7,466

Readme

SVG Crowbar Library

NPM version code style: prettier Build status Copy README to gh-pages branch

A standalone 3.5Kb JS client library based on Chrome bookmarklet.

The library provides functionality to trigger a download of a given SVG file having all the styles inlined, to make it look the same when opened independently from the original HTML page.

It is also possible to use this library to convert an SVG to a PNG before downloading.

Usage

import downloadSvg from 'svg-crowbar';

downloadSvg(document.querySelector('svg'));

or

import { downloadPng } from 'svg-crowbar';

downloadPng(document.querySelector('svg'), 'my_svg', { css: 'internal' });

The downloadSVG/downloadPNG functions each have three arguments:

downloadSVG(svgElement, [filename], [options])
downloadPNG(svgElement, [filename], [options])

Options

  • svgElement (required)

    A DOM element selector for an SVG, e.g. document.querySelector('svg'). An error is thrown if no valid SVG element was provided.

  • filename (optional)

    A string to set the filename. This is determined by element id, class or page title, when not provided explicitly.

  • options (optional)

    An object literal. It presently has two configurable properties:

  • options.css (optional)

    This setting determines how the SVG will be styled:

    • 'inline'

      Default value. Inlines all computed styles on every element in the SVG. This setting best ensures that the exported SVG is accurate cross-browser.

    • 'internal'

      Adds an internal block of styles containing only explicitly declared style rules (from document.styleSheets). This can drastically reduce file-sizes and build time in exported SVGs, but could be less accurate as it does not include styles from the browser's user agent stylesheet, or from cross-origin stylesheets (e.g. external webfonts).

    • 'none'

      Doesn't add any CSS. This gives the smallest file-size, but you might need to manually add your own styles to exported SVGs to ensure an accurate output. You can do this by injecting a <style> block into the selected SVG before exporting.

    Example:

    const svg = document.querySelector('svg');
    
    // Add inline styles on SVG elements:
    downloadSvg(svg, 'my_svg'); 
    downloadSvg(svg, 'my_svg', { css: 'inline' });
    
    // Add a <style> block in the SVG:
    downloadSvg(svg, 'my_svg', { css: 'internal' });
    
    // Do not add CSS:
    downloadSvg(svg, 'my_svg', { css: 'none' });
  • options.downloadPNGOptions.scale (optional)

    This setting determines at which scale the final PNG image is created, for example when resolution is desired. The default scale is 1 (ie no scaling).

    Example:

    const svg = document.querySelector('svg');
    
    // Download a normal-scaled PNG
    downloadPng(svg, 'my_svg');
    downloadPng(svg, 'my_svg', {downloadPNGOptions:{ scale: 1 }});
    
    // Download a double-scaled PNG
    downloadPng(svg, 'my_svg', {downloadPNGOptions:{ scale: 2 }});
    

    UMD bundle

Thanks to @richardwestenra there's UMD bundle available in the package: simply add

<script src="node_modules/svg-crowbar/dist/main.js"></script>

to get downloadSvg and downloadPng global function.