Socket
Socket
Sign inDemoInstall

svg-pathdata

Package Overview
Dependencies
5
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    svg-pathdata

SVG Path Datas parsing


Version published
Weekly downloads
1.4M
increased by0.74%
Maintainers
1
Created
Weekly downloads
 

Package description

What is svg-pathdata?

The svg-pathdata npm package is a powerful tool for parsing, manipulating, and serializing SVG path data. It allows developers to easily work with SVG path strings, converting them into a more manageable format, modifying their properties, and converting them back into strings. This package is particularly useful for applications that need to dynamically generate or alter SVG graphics.

What are svg-pathdata's main functionalities?

Parsing SVG Path Data

This feature allows you to parse a string containing SVG path data into a more manageable object format. This is useful for reading and manipulating individual commands within an SVG path.

"const {SVGPathData, SVGPathDataParser} = require('svg-pathdata');\nconst pathData = new SVGPathData('M10 10L90 90');"

Serializing SVG Path Data

After manipulating or generating SVG path data, this feature enables you to convert the path data back into a string format. This is essential for rendering the modified SVG graphics on the web.

"const {SVGPathData} = require('svg-pathdata');\nconst pathData = new SVGPathData('M10 10L90 90');\nconst serializedPath = pathData.encode();"

Transforming SVG Path Data

This feature provides a set of transformation functions (such as translate, rotate, scale) that can be applied to the path data. It's useful for dynamically altering the appearance of SVG graphics without manually recalculating path commands.

"const {SVGPathData, SVGPathDataTransformer} = require('svg-pathdata');\nconst pathData = new SVGPathData('M10 10L90 90').transform(SVGPathDataTransformer.translate(10, 20));"

Other packages similar to svg-pathdata

Changelog

Source

v1.0.0 (2014/11/16 16:05 +00:00)

  • d043a52 1.0.0 (@nfroidure)
  • da9671b Fixing rel 2 abs for multipath pathdatas (@nfroidure)
  • 68d0e70 Adding the forgotten licence file closes #7 (@nfroidure)
  • 2b7bd20 Updating version number (@nfroidure)
  • 2b1e277 Removing unecessary dependency (@nfroidure)
  • cf3f3e4 Do not use data event anymore (@nfroidure)
  • 5cb5c80 Improved transform functions (@nfroidure)
  • 1f567e1 Improved transform function (@nfroidure)
  • 4724965 New version (@nfroidure)
  • 2d726ec Fucking grunt-browserify (@nfroidure)
  • 3587d82 Adding forgottent tests (@nfroidure)
  • 5a29acb Removing errors that cannot happen (@nfroidure)
  • 3425078 Adding forgotten new operator for errors (@nfroidure)
  • 54b7538 No more EOF and STATE_ENDED for the parser (@nfroidure)
  • f2609e3 Fixing encoder output to buffer modewq (@nfroidure)
  • 2c03487 Improving code coverage, allowing constructors omission (@nfroidure)
  • 237898e Updating dependencies (@nfroidure)
  • d35369c Fix matrix transformation formula closes #2 (@nfroidure)
  • e218385 Fixing tests, improving A to C transformer (@nfroidure)
  • 49fe80a New version 0.0.4 (@nfroidure)
  • a0e8a63 Added stats (@nfroidure)
  • #3 Depend on readable-stream from npm (@Raynos)
  • #4 clean up the scripts (@calvinmetcalf)
  • 0c2cfde change | to && (@calvinmetcalf)
  • 071215f clean up the scripts (@calvinmetcalf)
  • 19a4daa convert other files to readable-stream too (@Raynos)
  • 50dd97a depend on readable-stream in p.json (@Raynos)
  • 181f63b Use readable-stream instead (@Raynos)
  • ce11d07 Readme fix (@nfroidure)
  • c521987 Added coverage tests (@nfroidure)
  • 11d22fc Added a round transformation (@nfroidure)
  • 0bf6fa4 Do not parse incomplete L,M,S incomplete commands anymore (@nfroidure)
  • 653a4d3 Improved sapegin test (@nfroidure)
  • 60ee34e Added sapegin test (@nfroidure)
  • 908da2a Added arc to curve conversionwq (@nfroidure)
  • b341fde Main file is the src not the build (@nfroidure)
  • 0d8608e Version updated (@nfroidure)
  • 28a1cd1 Tests updated (@nfroidure)
  • 63ac182 Fix for X/Y transforms (@nfroidure)
  • b4196ce Removed unnecessary absolute conversions (@nfroidure)
  • 4a38ad0 Added x symetry (@nfroidure)
  • e34a0e3 Based y symetry on other transformations (@nfroidure)
  • 392152d Checking transformations arguments (@nfroidure)
  • 177f586 Updated REAMDE and package.json (@nfroidure)
  • 60a2819 Some transformations converted to their idioins matrixes (@nfroidure)
  • a24a2a5 Skew transformations added2 (@nfroidure)
  • 930b9c4 Skew transformations added (@nfroidure)
  • 7bc0390 Rotation transformation added (@nfroidure)
  • 3191815 Added scale transformation (@nfroidure)
  • 0e15916 Added translation transformation (@nfroidure)
  • 6cfd826 Fix for y symetry problems (@nfroidure)
  • 7d0576c Support decpoint separated numbers 2 (@nfroidure)
  • 92391ec Support sign separated numbers 2 (@nfroidure)
  • 5b536c1 Support sign separated numbers (@nfroidure)
  • d37ea0a Added ySymetry transformer2 (@nfroidure)
  • 647fdf4 Added ySymetry transformer (@nfroidure)
  • 377518f Added toRel transformer2 (@nfroidure)
  • dd00037 Added toAbs transformer2 (@nfroidure)
  • d0d8332 Added toAbs transformer (@nfroidure)
  • b1e24a6 Now based on Node stream module (@nfroidure)
  • 1bfbeea Updated dependencies (@nfroidure)
  • a267550 Added the distribution file (@nfroidure)
  • b8f0c7e Added encoding use samples (@nfroidure)
  • bbd8955 Switch to line to n+1 move to declarated commands (@nfroidure)
  • 667f390 Added an encoder (@nfroidure)
  • 78ef5ce Contributing (@nfroidure)
  • c6d4db4 Added usage in the README file (@nfroidure)
  • bd69b1c Const naming uniformized (@nfroidure)
  • 3b21f06 New Grunt/Package files (@nfroidure)
  • f84959e Small refactoring (@nfroidure)
  • 726e85f Added eliptic arc commands error detection tests (@nfroidure)
  • 7bb6671 Added eliptic arc commands (@nfroidure)
  • 7099de1 Added smooth quadratic curve to commands (@nfroidure)
  • 97c7575 Added quadratic curve to commands (@nfroidure)
  • 0499dcb Added curve to commands2 (@nfroidure)
  • fdb154e Added curve to commands (@nfroidure)
  • 111c42c Better syntax error handling (@nfroidure)
  • 52727ae Added smooth curveto commands (@nfroidure)
  • 4fae609 Added line to and closepath commands parsing (@nfroidure)
  • d7cdfad Move to commands parsing added (@nfroidure)
  • d10264e parse mthod returns commands (@nfroidure)
  • 280ffe0 Build info (@nfroidure)
  • ac6b856 Added vertical/horizontal commands2 (@nfroidure)
  • f785f6b Added vertical/horizontal commands (@nfroidure)
  • 2f9b8da Full support for numbers parsing (@nfroidure)
  • 699bf87 Fixed tests (@nfroidure)
  • 04450cc First Commit (@nfroidure)

Readme

Source

SVGPathData

Manipulating SVG path datas (path[d] attribute content) simply and efficiently.

NPM version Build status Dependency Status devDependency Status Coverage Status

Including the library

This library is fully node based (based on current stream implementation) but you can also use it in modern browsers with the browserified build or in your own build using Browserify.

## Reading PathDatas

var pathData = new SVGPathData ('\
  M 10 10 \
  H 60 \
  V 60 \
  L 10 60 \
  Z \
');

console.log(pathData.commands);

// {"commands":[{
//    "type": SVGPathData.MOVE_TO,
//    "relative": false,
//    "x": 10, "y": 10
//  },{
//    "type": SVGPathData.HORIZ_LINE_TO,
//    "relative": false,
//    "x": 60
//  },{
//    "type": SVGPathData.VERT_LINE_TO,
//    "relative":false,
//    "y": 60
//  },{
//    "type": SVGPathData.LINE_TO,
//    "relative": false,
//    "x": 10,
//    "y": 60
//  },{
//    "type": SVGPathData.CLOSE_PATH
//  }
// ]}

## Reading streamed PathDatas

var parser = new SVGPathData.Parser();
parser.on('data', function(cmd) {
  console.log(cmd);
});

parser.write('   ');
parser.write('M 10');
parser.write(' 10');

// {
//   "type": SVGPathData.MOVE_TO,
//   "relative": false,
//   "x": 10, "y": 10
// }


parser.write('H 60');

// {
//   "type": SVGPathData.HORIZ_LINE_TO,
//   "relative": false,
//   "x": 60
// }


parser.write('V');
parser.write('60');

// {
//   "type": SVGPathData.VERT_LINE_TO,
//   "relative": false,
//   "y": 60
// }


parser.write('L 10 60 \
  Z');

// {
//   "type": SVGPathData.LINE_TO,
//   "relative": false,
//   "x": 10,
//   "y": 60
// }
  
// {
//   "type": SVGPathData.CLOSE_PATH
// }

parser.end();

## Outputting PathDatas

var pathData = new SVGPathData ('\
  M 10 10 \
  H 60 \
  V 60 \
  L 10 60 \
  Z \
');

console.log(pathData.encode());
// "M10 10H60V60L10 60Z"

## Streaming PathDatas out

var encoder = new SVGPathData.Encoder();
encoder.setEncoding('utf8');

encode.on('data', function(str) {
  console.log(str);
});

encoder.write({
   "type": SVGPathData.MOVE_TO,
   "relative": false,
   "x": 10, "y": 10
 });
// "M10 10"

encoder.write({
   "type": SVGPathData.HORIZ_LINE_TO,
   "relative": false,
   "x": 60
});
// "H60"

encoder.write({
   "type": SVGPathData.VERT_LINE_TO,
   "relative": false,
   "y": 60
});
// "V60"

encoder.write({
   "type": SVGPathData.LINE_TO,
   "relative": false,
   "x": 10,
   "y": 60
});
// "L10 60"
  
encoder.write({"type": SVGPathData.CLOSE_PATH});
// "Z"

encode.end();

## Transforming PathDatas This library was made to live decoding/transform/encoding SVG PathDatas. Here is an example of that kind of use.

The synchronous way

console.log(
  new SVGPathData ('\
   m 10,10 \
   h 60 \
   v 60 \
   l 10,60 \
   z'
  )
  .toAbs()
  .encode()
);
// "M10,10 H70 V70 L80,130 Z"

The streaming/asynchronous way

Here, we take SVGPathDatas from stdin and output it transformed to stdout.

// stdin to parser
process.stdin.pipe(new SVGPathData.Parser())
// parser to transformer to absolute
  .pipe(new SVGPathData.Transformer(SVGPathData.Transformer.TO_ABS))
// transformer to encoder
  .pipe(new SVGPathData.Encoder())
// encoder to stdout
  .pipe(process.stdout);

Supported transformations

You can find every supported transformations in this file of course, you can create yours by using this format:

function SET_X_TO(xValue) {
  xValue = xValue || 10; // Provide default values or throw errors for options
  function(command) {
    command.x = xValue; // transform command objects and return them
    return command;
  };
};

// Synchronous usage
new SVGPathData('...')
  .transform(SET_X_TO, 25)
  .encode();

// Streaming usage
process.stdin.pipe(new SVGPathData.Parser())
  .pipe(new SVGPathData.Transformer(SET_X_TO, 25))
  .pipe(new SVGPathData.Encoder())
  .pipe(process.stdout);

Stats

NPM NPM

Contributing

Clone this project, run:

npm install; grunt test&

Keywords

FAQs

Last updated on 16 Nov 2014

Did you know?

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc