GithubHelp home page GithubHelp logo

svg-path-parse's Introduction

svg-path-parse

This is a small library to normalise SVG paths based on those normalised paths.

Note: this package works with path data strings and Paths, not with full svg xml sources.

Install

npm install svg-path-parse

Usage

Takes an SVG path string. The following code…


const { pathParse, serializePath } = require('svg-path-parse')

const pathDatas = pathParse(__your_path__).getSegments()
// const pathDatas = pathParse(__your_path__).normalize({round: 2, transform: 'translate(10, 10) scale(0.5)'})
// const pathDatas = pathParse(__your_path__).relNormalize({round: 2,transform: 'translate(10, 10) scale(0.5)'})
// const pathDatas = pathParse(__your_path__).absNormalize({round: 2,transform: 'translate(10, 10) scale(0.5)'})
// const pathDatas = pathParse(__your_path__).absCairo({round: 2,transform: 'translate(10, 10) scale(0.5)'})
// const pathDatas = pathParse(__your_path__).relCairo({round: 2,transform: 'translate(10, 10) scale(0.5)'})

// out put
// { err: '',  segments:[ { 'type': 'M', 'args' : [230, 230] }] }

serializePath(pathDatas)

console.log('pathDatas', serializePath(pathDatas))

// out put
// M896 480C894.656 480 893.536 480.608 892.256 480.768C894.72 479.84 893.568 479.232 892.256 479.232z

API

getSegments

Convert all path commands,data format as follows:

// example
{ 
  err: '',
  segments:[ 
    [ 'M', 230, 230 ],
    [ 'A', 45, 45, 0, 1, 1, 275, 275 ],
    [ 'L', 275, 230 ],
    [ 'Z' ] 
  ] 
}

normalize

Convert all path commands,data format as follows:

// example
{ 
  err: '',
  segments:[ 
    { 'type': 'M', 'args' : [230, 230] },
    { 'type': 'A', 'args' : [45, 45, 0, 1, 1, 275, 275] },    
    { 'type': 'l', 'args' : [0, -45] },   
    { 'type': 'z', 'args' : [] }
  ] 
}

absNormalize

Converts all path commands to absolute, data format as follows:

// example
{ 
  err: '',
  segments:[ 
    { 'type': 'M', 'args' : [230, 230] },
    { 'type': 'A', 'args' : [45, 45, 0, 1, 1, 275, 275] },    
    { 'type': 'L', 'args' : [275, 230] },   
    { 'type': 'Z', 'args' : [] }
  ] 
}

relNormalize

Converts all path commands to relative. Useful to reduce output size, data format as follows:

// example
{ 
  err: '',
  segments:[ 
    { type: 'M', args: [ 230, 230 ] },
    { type: 'a', args: [ 45, 45, 0, 1, 1, 45, 45 ] },
    { type: 'l', args: [ 0, -45 ] },
    { type: 'z', args: [] } 
  ]
}

absCairo

Converts all path commands to absolute based on cairo and CairoSVG. Converts smooth curves Q/q/T/t/S/s with "missed" control point to generic curves (C), Converts V/v/H/h to lineto (L), data format as follows:

// example
{ 
  err: '',
  segments:[ 
    { type: 'M', args: [ 230, 230 ] },
    { type: 'A',
      args: [ 230, 230, 0, 1, 1, 45, 0, 45, 3.14, 1.57 ] },
    { type: 'L', args: [ 275, 230 ] },
    { type: 'Z', args: [] } 
  ]
}

relCairo

Converts all path commands to relative on cairo and CairoSVG. Converts smooth curves Q/q/T/t/S/s with "missed" control point to generic curves (c), Converts V/v/H/h to lineto (l), data format as follows:

// example
{ 
  err: '',
  segments:[ 
    { type: 'M', args: [ 230, 230 ] },
    { type: 'a',
      args: [ 230, 230, 0, 1, 1, 45, 0, 45, 3.14, 1.57 ] },
    { type: 'l', args: [ 0, -45 ] },
    { type: 'z', args: [] } 
  ]
}

serializePath

Returns final path string.

License

MIT

svg-path-parse's People

Contributors

pfan123 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.