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

fix-path-directions

v1.0.3

Published

Correct sub path directions in compound path for apps that don't support fill-rules

Downloads

62

Readme

npm version

fix-path-directions

Correct sub path directions in compound path for apps that don't support fill-rules or just reverse path directions (e.g for path animations)

Install

Browser

<script src="https://www.unpkg.com/fix-path-directions@latest/js/fixPathDirections.js"></script>

or

<script src="https://cdn.jsdelivr.net/npm/fix-path-directions@latest/js/fixPathDirections.min.js"></script>

Node

npm install fix-path-directions
const fixPathDirections = require('fix-path-directions');
const {getFixedPathData, getFixedPathDataString, fixPathDataDirections, reversePathData, parsePathDataNormalized, pathDataToD} = fixPathDirections;

Fix sub path directions

If you can't use SVG's fill-rule attribute e.g because you need to convert your path for usage in a font you can autofix path directions like so:

Example 1: auto-fix from stringified pathdata

Stringified path data is automatically normalized to absolute and unshortened commands.

let d = "M50 0a50 50 0 110 100 50 50 0 110-100m0 30a20 20 0 110 40 20 20 0 110-40m0-10q12.42 0 21.21 8.79t8.79 21.21-8.79 21.21-21.21 8.79-21.21-8.79-8.79-21.21 8.79-21.21 21.21-8.79m-40-20a5 5 0 110 10 5 5 0 110-10m80 0a5 5 0 110 10 5 5 0 110-10m0 2.5a2.5 2.5 0 110 5 2.5 2.5 0 110-5m-45 42.5h10v10h-10zm-5-5h20v20h-20zm10-30c22.07 0 40 17.93 40 40s-17.93 40-40 40-40-17.93-40-40 17.93-40 40-40";

// return d path data string
let pathDataFixed = getFixedPathDataString(d);

// apply to path element
path.setAttribute("d", pathDataFixed);

See examples/simple.html

Example 2: auto-fix from stringified pathdata - with options

... However, you may also want to apply more fine-grained normalization options. You don't need to apply heavy command type conversions like arc to cubic bezier or conversions between quadratic and cubic Béziers.

// define normalization options
let options = {
  //convert arcs to cubics
  arcToCubic: false,
  //convert quadratic béziers to cubics
  quadraticToCubic: false,
  // outer shapes should be in clockwise direction
  toClockwise: false,
  returnD: true
};
// auto-fix path directions
let pathDataFixed = getFixedPathData(d, options);

// apply to path element
path.setAttribute("d", pathDataFixed);

Example 3: reverse path direction

let d = "M 0 100 Q 50 0 100 100";

let options = {
  arcToCubic: false,
  quadraticToCubic: true,
  toClockwise: false,
  returnD: true
};
let pathDataReversed = reversePathData(d, options);
path.setAttribute("d", pathDataReversed);

Example 4: optimize path data

You may also optimize the pathdata to get a more compact path data output by adding a pathdata compatible helper script like pathDataConvert.js (from svg-parse-path-normalized ).

<!-- optional conversions -->
<script src="https://cdn.jsdelivr.net/npm/svg-parse-path-normalized@latest/js/pathDataConvert.min.js"></script>
let options = {toShorthands:true, toRelative:true, decimals:1}
let pathDataFixed = getFixedPathData(d).convert(options).toD(1, true)
path.setAttribute("d", pathDataFixed);

See examples/simple_optimized.html

Demos

Credits

Related projects