@rob10e/svg-path-js
v3.0.0
Published
A declarative SVG path builder
Downloads
9
Readme
svg-path-js
A declaritive SVG path builder
Installation
$ npm i svg-path-js
Usage
import Builder from 'svg-path-js';
function renderSvgPath() {
const builder = new Builder();
const path = builder
.moveTo(10, 10)
.lineToRel(10, 10)
.cubicToRel(20, 20, 40, 20, 50, 10)
.smoothToRel(150, 150, 180, 80)
.arcTo(30, 50, 0, false, true, 165.55, 162.45)
.close();
return <Path d={path}/>
// => 'M 10 10 l 10 10 c 70 20, 120 20, 120 10 s 150 150, 180 80 A 30 50 0 0 1 165.55 162.45 z'
}
API
new Builder()
create an instance of the path builder
moveTo(x: number, y: number)
move to position without drawing a line absolutemoveToRel(x: number, y: number)
move to position without drawing a line relativelineTo(x: number, y: number)
draw a line from current position to point absolutelineToRel(x: number, y: number)
draw a line from current position to point relativehorizontalTo(x: number)
draw line from current position to point absolutehorizontalToRel(x: number)
draw line from current position to point relativeverticalTo(y: number)
draw a line from current position to point absoluteverticalToRel(y: number)
draw a line from current position to point relativecubicTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number)
draw a cubic bezier curve from current position to point absolutecubicToRel(y)
draw a cubic bezier curve from current position to point relativesmoothTo(x2: number, y2: number, x: number, y: number)
draw a smooth curve from previous curve to point absolutesmoothToRel(x2: number, y2: number, x: number, y: number)
draw a smooth curve from previous curve to point relativequadTo(x1: number, y1: number, x: number, y: number)
draw a quadradic curve from current position to point absolutequadToRel(x1: number, y1: number, x: number, y: number)
draw a quadradic curve from current position to point relativequadStringTo(x: number, y: number)
draw a quadradic curve from previous curve to point absolutequadStringToRel(x: number, y: number)
draw a quadradic curve from previous curve to point relativearcTo(rx: number, ry: number, xAxisRotation: number, largeArc: boolean, sweep: boolean, x: number, y: number )
draw arc from current position to point absolutearcToRel(rx: number, ry: number, xAxisRotation: number, largeArc: boolean, sweep: boolean, x: number, y: number )
draw arc from current position to point relativeclose()
return the path as an Svg formatted string, closing the pathend()
return the path as an Svg formatted string
Note: I was inpired to write this while building an application requiring a lot of fancy SVG path drawing. I hope this will help you as it helped me.