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

parse-svg-path-data

v0.2.1

Published

SVG Path Data Micro-Parser targeting custom interface objects.

Downloads

6

Readme

License npm npm no dependencies

parseSvgPathData

parseSvgPathData is a SVG path data micro-parser for parsing the path data attribute of the SVG <path> element as a single JavaScript function.

function parseSvgPathData(data,[ifc],[ctx])
  • data is the SVG path data string (e.g. "M 100 100 L 300 100 L 200 300 z").
  • ifc is an interface object [optional] (see below)
  • ctx is a context object the interface methods will operate on [optional].
  • return's the context object.

image

Try it by yourself

The fact that a user defined interface object should be supported, was considered a critical feature here. Similar libraries, which do not support this, are:

Interface Object

Interface objects need to have the following interface structure:

 {
    init(ctx),

    A(rx,ry,rot,fA,fS,x,y),
    C(x1,y1,x2,y2,x,y),
    H(x),
    L(x,y),
    M(x,y),
    Q(x1,y1,x,y),
    S(x2,y2,x,y),
    T(x,y),
    V(y),
    Z(),

    a(rx,ry,rot,fA,fS,x,y),
    c(x1,y1,x2,y2,x,y),
    h(x),
    l(x,y),
    m(x,y),
    q(x1,y1,x,y),
    s(x2,y2,x,y),
    t(x,y),
    v(y),
    z()
}

parseSvgPathData comes with two implemented Interfaces as members of the parseSvgPathData function object itself.

  • parseSvgPathData.defaultIfc
  • parseSvgPathData.canvasIfc

Have a look into the source how these interface objects are implemented and start to implement your own custom interface object.

defaultIfc

When calling the parseSvgPathData function without interface object ifc and context ctx parameters (e.g. parseSvgPathData('M0,0 L100,0')), the default interface object parseSvgPathData.defaultIfc will be taken.

By using this interface object the command segments are simplified and mapped to a minimal set of absolute path commands [A,L,M,C] in object notation.

So parsing the path data M37,17v15H14V17z M50,0H0v50h50z using this interface object will result in the object structure:

[ {"type":"M","x":37,"y":17},
  {"type":"L","x":37,"y":32},
  {"type":"L","x":14,"y":32},
  {"type":"L","x":14,"y":17},
  {"type":"L","x":37,"y":17},
  {"type":"M","x":50,"y":0},
  {"type":"L","x":0,"y":0},
  {"type":"L","x":0,"y":50},
  {"type":"L","x":50,"y":50},
  {"type":"L","x":50,"y":0}
]

canvasIfc

canvasIfc is another interface object, which is supplied with parseSvgPathData. Here every path command will invoke corresponding HTML canvas 2D methods.

Please notice my paper A Generalized Approach to Parameterizing Planar Elliptical Arcs in this context.

The following will render the path data in a given canvas element:

const ctx = document.getElementById('canvas').getContext('2d')
// ...
parseSvgPathData('M10,10 L100,10',parseSvgPathData.canvasIfc,ctx);
ctx.stroke()
ctx.fill()

You might know, that CanvasRenderingContext2D supports the Path2D interface. So we can also use it with parseSvgPathData

const path = new Path2D();
// ...
parseSvgPathData('M10,10 L100,10',parseSvgPathData.canvasIfc,path);

or even simpler

const path = parseSvgPathData('M10,10 L100,10',
                              parseSvgPathData.canvasIfc,
                              new Path2D());

Please note, that parseSvgPathData might be useful despite the fact, that Path2D natively understands and supports SVG path data.

const path = new Path2D('M10,10 L100,10');

You can see the results of both the native and parseSvgPathData output side by side here.

License

parseSvgPathData.js is licensed under the terms of the MIT License. See License for details.

FAQ

--

Change Log

All notable changes to this project will be documented here. This project adheres to Semantic Versioning.

0.2.0 - 2018-04-15

First Commit to Github