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-smoother

v2.1.0

Published

Smooth SVG paths

Downloads

110

Readme

SVG Smoother Tests

A utility for adding rounded corners to SVG paths.

Currently only supports hard corners, any pre-existing curve commands are left as is, including corners leading into curve commands. See examples file for a demonstration.

Installation

npm i svg-smoother

Syntax

smoothPath(path, config?)

Smooths a SVG path string.

Parameters

Return value

A string matching the SVG d spec.

Example

import { smoothPath } from "svg-smoother";

const path = smoothPath("M 10 10 L 40 10 L 40 40", { radius: 10 });

This function also integrates nicely with React or other frameworks like so:

function Path() {
    return <path d={smoothPath("M 10 10 L 40 10 L 40 40")} />;
}

smoothPathElement(element, config?)

A helper function when working with existing DOM elements.

Parameters

Return value

An SVG Path DOM Element.

Example

import { smoothPathElement } from "svg-smoother";

const path = document.querySelector("path");
smoothPathElement(path);

smoothPolygon(polygon, config?)

Takes an array of number pairs and converts it into a smoothed SVG path shape.

This function is particularly helpful if you are using some other JS to generate a path dynamically as a list of x,y pairs that you want to smooth.

Parameters

  • polygon An array of number pairs (e.g. [[0, 0], [10, 20], [30, 40]]) that represent absolute values of a polygon shape. Similar syntax to the CSS polygon command. Note that the values are not treated as percentages like in CSS but if you provide numbers between 0-100 it does work just the same.
  • config An optional configuration object

Return value

A string matching the SVG d spec.

Example

import { smoothPolygon } from "svg-smoother";

const path = smoothPolygon([
    [10, 10],
    [40, 10],
    [40, 40],
]);
const path = smoothPolygon(
    [
        [10, 10],
        [40, 10],
        [40, 40],
    ],
    {
        closePath: false,
    }
);

Configuration object

  • radius A number that represents the amount of coordinate points to round the corners by. These values work much like setting CSSs border-radius property. Defaults to 10 if not provided

  • reduceCommands A boolean that if set to true will attempt to replace any Line commands in the resulting path with Horizontal and Vertical commands to reduce the resulting path length. Defaults to true if not provided.

    Turing this off can be helpful if you need a stable set of returned commands for animation states with CSS transitions.

  • numberAccuracy Set the number of decimal places to round values to when outputting the new path. Defaults to 3 decimal places if not provided.

  • closePath Only applies to the smoothPolygon command. A boolean that when set to true closes the provided path into a complete shape. When false it is left open as a line. Defaults to true if not provided.

  • preventOverflow A boolean that controls if your radius value should be constrained to not be larger than the line it is smoothing. Defaults to true, preventing overflow. See examples file for an example of this in use.

  • allowEllipse A boolean to control if smoothing should stick to perfect circles or create ellipses. Defaults to true so smoothing will create mismatched radius values. If it helps with understanding it is similar to having a dual value CSS border-radius vs a single value, border-radius: 10px; vs border-radius: 10px / 20px;. Also check the examples file for a visual example of this in practice.


Planned features

  • ~~Deal with radius values that are larger than the preceding line~~
  • Investigate support for smoothing into and out of curve commands
  • ~~Add more examples~~
  • Measure and improve performance
  • ~~Add an optimization step to remove large floats, and restore usage of H and V commands~~

Support

Versions of Node >= 14 are tested to work, but testing lower versions is limited because of a testing dependency on JSDOM which has a minimum Node version of 14. SVG Smoother itself is likely to work on older versions than that.

Browser support requires support of Object.values

License

GNU General Public License