Skip to content

Instantly share code, notes, and snippets.

@chunghe
Created May 31, 2016 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chunghe/8472c99f12beaf93fef41f30e7338d62 to your computer and use it in GitHub Desktop.
Save chunghe/8472c99f12beaf93fef41f30e7338d62 to your computer and use it in GitHub Desktop.
const parse = require('svg-path-parser');
const d = 'M198.314 131.725h-54.766c-1.458 0-1.988-1.32-1.148-2.51l16.032-21.349c.84-1.188 2.385-1.408 3.445-.352l3.136 3.125c1.06 1.057 2.65.969 3.577-.176l7.553-9.156c.927-1.144 2.385-1.056 3.224.132l20.096 27.777c.883 1.188.353 2.509-1.149 2.509zm-2.87-26.456c-3.136 0-5.654-2.553-5.654-5.634A5.627 5.627 0 0 1 195.443 94c3.136 0 5.654 2.553 5.654 5.635.044 3.08-2.518 5.634-5.654 5.634z';
const paths = parse(d);
const result = paths.map( path => {
switch (path.code.toLowerCase()) {
case 'm':
return `${path.code}${path.x} ${path.y}`;
case 'l':
return `${path.code}${path.x} ${path.y}`;
case 'h':
return `${path.code}${path.x}`;
case 'v':
return `${path.code}${path.y}`;
case 'c':
return `${path.code}${path.x1} ${path.y1} ${path.x2} ${path.y2} ${path.x} ${path.y}`;
case 's':
return `${path.code}${path.x2} ${path.y2} ${path.x2} ${path.y}`;
case 'q':
return `${path.code}${path.x1} ${path.y1} ${path.x} ${path.y}`;
case 't':
return `${path.code}${path.x} ${path.y}`;
case 'a':
return `${path.code}${path.rx} ${path.ry} ${path.xAxisRotation} ${+path.largeArc} ${+path.sweep} ${path.x} ${path.y}`;
case 'z':
return path.code;
default:
console.error('Error: path not able to handle:', path);
return `--- not handle ${path.code}---`;
}
});
console.log(result.join(' '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment