Socket
Socket
Sign inDemoInstall

svg-pathstring-parser

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svg-pathstring-parser

Parse SVG pathstrings in an efficient manner.


Version published
Maintainers
1
Created
Source

svg-pathstring-parser

Parse SVG pathstrings in an efficient manner.

Installation

npm install --save svg-pathstring-parser

Motivation

There are already many SVG pathstring parsers. However, most simply return the different segments without normalize data, which quickly becomes annoying when you want to perform complex actions on a list of points.

The principle of this tool is to parse the pathstring and return an array of normalized objects.

Each point has a code (the SVG command), coordinates that represent the position, and an object parameters in which there are data describing the anchors of Bezier curves or data describing arcs.

The computed coordinates are always absolute, even for relative points. This way you always know very precisely where is the current point without having to do additional calculations and add exceptions in your code.

Usage

The parsePathstring function take a string as parameter and returns an array of objects. Each object represents a point with a code, coordinates and parameters.

Example

import parsePathstring from "svg-pathstring-parser"

const path = parsePathstring("M0 0l50,50 H100 v-50 Q150 150 200,200 t1.005e-15 2 A 50 50 0 1 0 300 300z")

/**
 * ==> [
 *   {
 *     code: "M",
 *     x: 0,
 *     y: 0,
 *     parameters: {},
 *   },
 *   {
 *     code: "l",
 *     x: 50,
 *     y: 50,
 *     parameters: {},
 *   },
 *   {
 *     code: "H",
 *     x: 100,
 *     y: 50,
 *     parameters: {},
 *   },
 *   {
 *     code: "v",
 *     x: 100,          <-- note the computed absolute position
 *     y: 0,
 *     parameters: {},
 *   },
 *   {
 *     code: "Q",
 *     x: 200,
 *     y: 200,
 *     parameters: {
 *       x1: 150,
 *       y1: 150,
 *     },
 *   },
 *   {
 *     code: "t",
 *     x: 200,
 *     y: 202,
 *     parameters: {    <-- the anchor has been automatically computed
 *       x1: 250,
 *       y1: 250,
 *     },
 *   },
 *   {
 *     code: "A",
 *     x: 300,
 *     y: 300,
 *     parameters: {
 *       rx: 50,
 *       ry: 50,
 *       rotation: 0,
 *       large: 1,
 *       sweep: 0,
 *     },
 *   },
 *   {
 *     code: "z",
 *     x: 0,
 *     y: 0,
 *     parameters: {},
 *   },
 * ]
 */

License

MIT

FAQs

Package last updated on 19 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc