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-viewbox-maximize

v1.1.2

Published

Resize SVG viewBox coordinate system to cover container element. Also includes a utility for translating between page coordinates and SVG coordinates.

Downloads

9

Readme

svg-viewbox-maximize

SVG ViewBox Maximize is a utility for resizing the viewbox coordinate system of an inline SVG so that it covers the entire browser viewbox (or a container element) without resizing the SVG's contents.

Why is this a problem?

Inline SVGs are a powerful native tool for imaging and animation. However, browsers and devices come in all shapes and sizes while SVGs require fixed aspect ratio coordinate systems to ensure consistent rendering of the components. This leaves us with a few obvious options when scaling an image to a different aspect ratio using the preserveAspectRatio attribute of an SVG:

| Stretched SVG | |:--:| | none - Stretch the image independently on each axis. This is rarely the intended outcome as the image completely changes. |

| Covered SVG | |:--:| | xMidYMid slice - Cover the container element, maintaining the SVG aspect ratio. This often results in clipping important information from our SVG. |

| Contained SVG | |:--:| | xMidYMid meet - Contain the SVG within the container element, maintaining the SVG aspect ratio. This is a pretty good solution in many cases, but it can leave unwanted blank margins around the SVG. Furthermore, since most SVGs contain a clipping mask around the viewbox, these margins aren't usable if you want to move (animate) the internal SVG elements into the blank regions. |

Note: Amelia Bellamy-Royds gives a great primer on SVG scaling over at CSS Tricks.

We can do better!

Since SVGs are image instructions rather than a rendered image, we can modify the SVG to better suit our modified aspect ratio by scaling specific objects in the SVG differently than the SVG viewbox.

| Contained SVG | |:--:| | xMidYMid slice is still being used here, but with the help of this library, the viewbox is resized along with the background and the clipping mask. |

Can't individual object scaling be accomplished by nesting SVGs with different preserveAspectRatio settings?

Yes! If you can appropriately break apart your SVG into sub-SVGs to control the element scaling and you have no need for complex animation, then that is a preferrable approach as no JavaScript is required to perform the resizing. Again, see the CSS Tricks primer on SVG scaling for more information on the nested SVG approach.

This library exists to allow you to scale the SVG viewbox while selectively scaling the internal SVG elements.

Installation

yarn add svg-viewbox-maximize

ES6

import SvgViewboxMaximize from 'svg-viewbox-maximize';

CommonJS

var SvgViewboxMaximize = require('svg-viewbox-maximize');

Global Script Include

<script src="svg-viewbox-maximize.js">

Usage

To begin using SvgViewboxMaximize, invoke the constructor with a config object containing a reference to the SVG to be managed and a callback which will update the internal elements of the SVG on resize:

var svg = new SvgViewboxMaximize({
  svg: $('svg#my-svg'),
  resized: function() {
    // Resize SVG internals as desired... access updated viewbox coordinates via this.current
  }
});

This will cause the viewbox to be recalculated anytime the SVG element is forced to resize due to the browser viewport changing. The updated viewbox coordinates can be accessed inside the resized callback via this.current or outside the callback using the saved instance - svg.current.

API

new SvgViewboxMaximize({ svg, resized, container })

Constructor - begins monitoring of SVG. Config parameters:

  • Element or String svg - SVG element in the DOM to monitor and resize. If a String is provided, it is treated as a CSS selector which finds an element with document.querySelector.
  • Function resized - Callback function which is invoked at page load and anytime window is resized.
  • Element container - If provided, SVG viewbox is sized to container instead of itself.

svg.containerRatio

Returns the current ratio of height / width of the SVG container.

svg.svgX(viewportX)

Converts window viewport x-coordinate to SVG viewbox x-coordinate.

svg.svgY(viewportY)

Converts window viewport y-coordinate to SVG viewbox y-coordinate.

svg.rectangle(element)

For a DOM element, returns the SVG coordinate rectangle (top, bottom, left, right, height, width).