0.1.2 • Published 1 year ago

svgtracer v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Description

svgtracer is a package to trace SVG paths into a list of points. It is intended to be used with typescript and is compatible with node.js and the browser.

Installation

npm install svgtracer

or

yarn add svgtracer

Usage

import traceSVG from 'svgtracer';

const svg = traceSVG('<file content or data uri>');
svg.forEach((path) => {
	path.forEach((point) => {
		console.log(point.position.x, point.position.y);
	});
});

Functions and classes

traceSVG

traceSVG(svg: string, options?: TraceOptions): SVG;

Returns an SVG object containing the traced paths and groups in the original hierarchy. | Parameter | Description | |---------------|---------------------------------------------------| | svg | contents of the svg in text form or data uri form | | options? | options for the svg tracer, see below |

TraceOptions

class TraceOptions {
	resolution: number;
	colors: boolean;
	subpaths: boolean;
	transform: TransformMatrix;
}
FieldDefault valueDescription
resolution1resolution of curves, higher is more accurate.
colorstruewhether to include colors.
subpathstruewhether to split paths into subpaths (a new subpath starts with a move command).
transformidentitytransformation matrix to apply to the svg.

SVG

class SVG {
	public children: (SVGPath | SVGGroup)[];
	public getAllPaths(): SVGPath[];
}
Field / FunctionDescription
childrenArray containing all direct children of the root element.
getAllPaths()Returns an array of all paths in the svg.

SVGGroup

class SVGGroup {
	public children: (SVGPath | SVGGroup)[];
}
FieldDescription
childrenArray containing all direct children of this element.

SVGPath

class SVGPath {
	public points: Point[];
	public boundingBox?: BoundingBox;
	public style: PathStyle;
	public subpaths: SVGSubpath[];
}
FieldDescription
pointsArray containing all points of the path. Empty, if subpaths are enabled.
boundingBoxBounding box of the path. undefined if subpaths are enabled.
styleStyle element of the path.
subpathsArray containing all subpaths of the path. Empty, if subpaths are disabled.

SVGSubpath

class SVGSubpath {
	public points: Point[];
	public boundingBox: BoundingBox;
}
FieldDescription
pointsArray containing all points of the path
boundingBoxBounding box of the path.

Point

class Point {
	public position: Vector2D;
	public normal: Vector2D;
}
FieldDescription
positionPosition of the point.
normalNormal vector of the point.

PathStyle

class PathStyle {
	public fill?: Color;
	public stroke?: Color;
	public strokeWidth?: number;
}
FieldDescription
fill?Fill color of the path. White if the path is not filled.
stroke?Stroke color of the path. White if the path is not filled.
strokeWidth?Stroke width of the path. Defaults to 1 if not specified.

Color

class Color {
	public r: number;
	public g: number;
	public b: number;
	public a: number;
}
FieldDescription
rRed channel (0-255)
gGreen channel (0-255)
bBlue channel (0-255)
aAlpha channel (0-255)
0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago