简体   繁体   中英

parse d attribute in SVG path element

I'm trying to parse the d attribute in SVG Path element, and so far I found that fabric.js can parse SVG, but till now I still don't know how.

I need to parse the path in order to get the shapes in it (lines arcs) and draw squares over them, and most importantly return the attributes of these squares.

any idea how to do this using fabric.js?? or any other library?? or does anyone have a different approach?

the following image has a rectangle and a line both have squares that I drew on their boundaries, and I'm trying to do the same on the path element 矩形和带边界的线

I found this

var cmdRegEx = /[a-z][^a-z]*/ig;
var commands = d.match(cmdRegEx);

which can get each command with its parameters, but you need to trim each command from spaces

Stamped into the same problem. You can use the regep /-?\\d+/ig which produces just the numbers, striped from letters, white spaces. and commas.

Based on zeacuss answer and Mark K Cowan suggestion, I'm using:

var cmdRegEx = /([MLQTCSAZVH])([^MLQTCSAZVH]*)/gi
var commands = d.match(cmdRegEx);

Python's svgpathtools library might be useful for your needs. This is a list of its features (from the documentation ):

Some included tools:

 read, write, and display SVG files containing Path (and other) SVG elements convert Bézier path segments to numpy.poly1d (polynomial) objects convert polynomials (in standard form) to their Bézier form compute tangent vectors and (right-hand rule) normal vectors compute curvature break discontinuous paths into their continuous subpaths. efficiently compute intersections between paths and/or segments find a bounding box for a path or segment reverse segment/path orientation crop and split paths and segments smooth paths (ie smooth away kinks to make paths differentiable) transition maps from path domain to segment domain and back (T2t and t2T) compute area enclosed by a closed path compute arc length compute inverse arc length convert RGB color tuples to hexadecimal color strings and back 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM