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

@zero41120/svg-dimension-from-viewbox

v1.0.1

Published

This CLI utility reads provided SVG files, extract width and height from ViewBox attribute, scale the value, and writes them back as width and height.

Downloads

1

Readme

SVG-Dimension-from-ViewBox

A small node utility that adds/replaces dimension using SVG's ViewBox attribute

CLI

Installation

npm install [-g] @zero41120/svg-dimension-from-viewbox

Usage

Use the command line interface:

svgvbdimension [--overwrite] [--scale N] <source-svg-filepath> <destination-filepath>';
    
    --overwrite : optional, if destination filepath exists, use this flag to overwrite file.
    --scale N   : optional, multiply the ViewBox's width/height value by given scale, default to 1.

Use in package.json, this finds all the .svg files in your ./src, and run the CLI with scale 10 writting to file itself.

# package.json
"scripts": {
  "svg-fix": "find ./src -type f -name \"*.svg\" -exec svgvbdimension {} {} --overwrite --scale 10 \\;"
}

Examples:

# Make a simple SVG using echo
echo '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 10 10"><path d="M0 0 L5 2 L2 5 Z" fill="#0066cc"/></svg>' > triangle.svg

# Write to file 2
# Output: >> Adding width="10" height="10"
# Result: ...  viewBox="0 0 10 10" width="10" height="10" ...
svgvbdimension triangle.svg triangle2.svg 

# Write to file 2 again, fails
# Output: >X  Destination: triangle2.svg already exists. Use --overwrite to overwrite to the destination path.
svgvbdimension triangle.svg triangle2.svg 

# Write to file 2 with --overwrite
# Output: >> Adding width="10" height="10"
# Result: ...  viewBox="0 0 10 10" width="10" height="10" ...
svgvbdimension triangle.svg triangle2.svg --overwrite

# Write to file 2 with scale x3
# Output: >> Adding width="30" height="30"
# Result: ... viewBox="0 0 10 10" width="30" height="30" ...
svgvbdimension triangle.svg triangle2.svg --overwrite --scale 3

# Write to self with scale x7
# Output: >> Replacing existing width="30", height="30" in triangle2.svg. Using new dimension width="70" height="70".
# Result: ... viewBox="0 0 10 10" width="70" height="70" ...
svgvbdimension triangle2.svg triangle2.svg --overwrite --scale 7

# Write to self again with scale x7
# Output: >> Same dimension defined width="70" height="70" in triangle2.svg. Making no changes.
# Result: ... viewBox="0 0 10 10" width="70" height="70" ...
svgvbdimension triangle2.svg triangle2.svg --overwrite --scale 7

Why?

I'm working on a PixiJS project with SVG with only viewBox specification, and noticed that PixiJS is unable to load the SVG in Firefox.

This is an issue logged in Mozilla 10 years ago.

I intended to fix this on PixiJS side with this Pull Request. Pixi's decision is not address this with following justification:

This is to workaround a browser specific issue, which is to infer width/height from viewBox on Firefox. 

Firefox already helpfully provides a warning for this case. The easiest fix, that doesn't rely on any code changes, 
is to add width and height to your SVG to prevent the warning. We don't think Pixi should be handling invalid data 
in this case, especially when it adds a lot of complexity to the SVGResource. 

It's better to nudge developers to do the right thing with their assets and make sure they are valid (even if one
browser has more strict requirements).

License

Released under the MIT license