-
-
Notifications
You must be signed in to change notification settings - Fork 20
JavaScript API
The SVGPathCommander class comes with most useful public methods, intuitive and easy to use:
Returns the path bounding box, equivalent to native path.getBBox()
.
Returns the total path length, equivalent to native path.getTotalLength()
.
Returns a {x,y}
point in path stroke at a given length, equivalent to native path.getPointAtLength()
.
Converts all path commands of a shape with or without sub-path to relative values.
Will convert all segments of a SVGPathElement to cubicBezier segments; this method will also remove unnecessary Z
segments.
Will reverse the shape draw direction by changing the order of all path segments and their coordinates; when the onlySubpath
option is true, it will only reverse the draw direction of subpath shapes
Converts path command values to absolute and convert shorthand S
, T
, H
, V
to C
, Q
and L
respectively.
Will execute an algorithm to find shorthand notation for eligible segments then will compute two pathArray
s one with absolute and the other with relative values, then update the pathArray
segments using the values that convert to shortest string.
Will normalize all path commands and apply a 2D transformation matrix to all path commands.
Will call the above transform()
method to apply a 180deg rotation on the X axis.
Will call the above transform()
method to apply a 180deg rotation on the Y axis.
Will return the pathString
of the current pathArray
stored in the instance.segments
object.
-
round
Number | Boolean - option to enable/disable value rounding for the processing output; the default value is 4 but you can disable rounding the values withround: false
-
origin
[Number, Number, Number] - option to set a transform origin for transformations; if not provided, the script will compute the shapepathBBox
and set this option to50% 50% 100%
values of the shapewidth height width
values of thepathBBox
When using the distribution files, type in "SVGPathCommander." in your browser console and have a look, there are a wide range of tools to play with. Here are some notable utilities:
- SVGPathCommander.isValidPath(string) - checks if a path string is valid;
-
SVGPathCommander.shapeToPath(element, replace) - returns a new
<path>
element from a given<line>
,<polyline>
,<polygon>
,<rect>
,<ellipse>
,<circle>
or<glyph>
element; the newly created<path>
element keeps all non-specific attributes likeclass
,fill
, etc.; when thereplace
parameter istrue
, it will replace the target element; you can also create<path>
elements on the fly by providing the minimum required specific attributes: EG:SVGPathCommander.shapeToPath({type: 'circle', r: 5, cx: 15, cy:15})
; - SVGPathCommander.parsePathString(string) - returns a pathArray which is used by all of SVGPathCommander processing tools;
- SVGPathCommander.pathToAbsolute(string | pathArray) - returns a new pathArray having all path commands as absolute coordinates;
- SVGPathCommander.pathToRelative(string | pathArray) - returns a new pathArray having all path commands as relative coordinates;
-
SVGPathCommander.pathToCurve(string | pathArray) - returns a new pathArray having all path commands converted to cubicBezierTo (
C
) and absolute values; - SVGPathCommander.pathToString(pathArray, round) - converts any pathArray to string and returns it, also allowing you to set the amount of decimals to round values to;
- SVGPathCommander.clonePath(pathArray) - returns a deep clone of a pathArray, which is the result of any of the above functions;
- SVGPathCommander.roundPath(pathArray, round) - returns a new pathArray with all float path command values rounded to 4 decimals by default, or provide a number to be used as the amount of decimals to round values to; the round parameter allows you to disable rounding by using the value: "off";
- SVGPathCommander.reversePath(string | pathArray) - returns a new pathArray with all path commands having absolute values and in reverse order, but only for a single M->Z shape, for paths having sub-path(s) you need to use the SVGPathCommander constructor itself;
-
SVGPathCommander.optimizePath(pathArray) - returns a new pathArray with all segments that have the shortest strings from either absolute or relative
pathArray
segments; -
SVGPathCommander.transformPath(pathArray | string, transformObject) - returns a new pathArray with all segments transformed according to the properties defined in the
transformObject
; -
SVGPathCommander.normalizePath(string | pathArray) - returns a new pathArray with all shorthand path command segments such as
S
,T
are converted toC
andQ
respectively,V
andH
toL
, all in absolute values; the utility is used bypathToCurve
andreversePath
; - SVGPathCommander.getDrawDirection(pathArray) - converts the pathArray to curve and returns TRUE if a shape draw direction is clockwise, it should work for shapes with sub-paths, but it might skew your results, so make sure you split your path and test each sub-path separately;
-
SVGPathCommander.getPathBBox(pathArray) - converts the pathArray to curve and returns the bounding box of a shape in the form of the following object:
{x1,y1, x2,y2, width,height, cx,cy,cz}
, where cx & cy are the shape's center point, cz is more of atransformOriginZ
for 3D projections; for faster processing, you might want to split the path with sub-paths and return the bounding box you know is the largest; -
SVGPathCommander.getTotalLength(pathArray | string) - will normalize the pathArray and return the total length of the shape; this is equivalent to SVGPathElement.prototype.getTotalLength() and should work in Node.js; this is a new addition to our library and should be more accurate than the old one
getPathLength
mentioned below as it uses less amount of code and should be more accurate especially with line segments; -
SVGPathCommander.getPointAtLength(pathArray | string, distance) - normalizes the pathArray and looks into each segment to return an
{x,y}
object which represents a point at the given distance; - SVGPathCommander.splitPath(string) - returns an Array of sub-path strings;
-
SVGPathCommander.fixPath(string | pathArray) - checks if a pathArray has an unnecessary
Z
segment and returns a new pathArray without it; - SVGPathCommander.getPropertiesAtLength(pathArray | string, distance) - returns an object with the following properties: the segment in which the distance spans, the index of the segment, the length of the segment and length to the segment;
- SVGPathCommander.getPropertiesAtPoint(pathArray | string, {x ,y}) - returns an object with the following properties: the closest point in stroke, the distance to closest point and the segment which contains the closest point;
- SVGPathCommander.getClosestPoint(pathArray | string, {x ,y}) - returns an {x,y} object with the coordinates of a point that is closest to a given {x,y} point;
- SVGPathCommander.isPointInStroke(pathArray | string, {x ,y}) - checks if a given {x,y} point is along the stroke of a path;
- SVGPathCommander.getSegmentOfPoint(pathArray | string, {x ,y}) - returns the segment that contains a given {x,y} point;
- SVGPathCommander.getSegmentAtLength(pathArray | string, distance) - returns the segment reached by a given distance.