1.14.0 • Published 1 year ago

react-svg-path v1.14.0

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

react-svg-path

react-svg-path makes composing svg elements dead simple. Everything is a path. Have fun.

There are more docs and interactive demos at https://joemaddalone.github.io/react-svg-path-docs/

Table of contents

Install

npm install --save react-svg-path

Path

import Path from 'react-svg-path'

react-svg-path is mostly helpful for building the commands needed for the "d" attribute of an svg path.

Most methods are direct translations from the SVG Path specification.

<path d="M0 0, L0 100"></path>

This path can be produced with:

const path = new Path().M(0,0).L(0,100);
console.log(path.toString()) // M0 0, L0 100

This library wraps https://github.com/joemaddalone/path and further documentation can be found there. All of the functionality of path is always available via import Path from 'react-svg-path' Please read the docs on path

toComponent

This library adds one additional rendering method to path called toComponent.

  • toComponent(props)
    • returns a jsx function including attributes and props.
import React from 'react';
import Path, {Svg}  from 'react-svg-path';

const CustomSquare = ({ x, y, size }) => {
  const path = new Path()
    .moveTo(x, y) // move pen to x, y
    .right(size) // draw line right to relative "size" px
    .down(size) // draw line down to relative "size" px
    .left(size) // draw line left to relative "size" px
    .close() // draw line back to first point
  return path.toComponent({ fill: 'green'});
};

const App = () => {
  const width = 800;
  const height = 800;
  return (
    <Svg
      width={width}
      height={height}
    >
      <CustomSquare x={50} y={50} size={50} />
    </svg>
  );
};

Components

Writing path commands via the library makes creating paths super simple and intuitive. However it can seem like overkill for really common patterns you may need. react-svg-path includes a number of components to allow for a declarative interface for generating the paths you need.

Note: The following shorthand version of some props are available where applicable:

  • sxy: sx & sy = sxy
  • exy: ex & ey = exy
  • cxy: cx & cy = cxy
  • rxy: rx & cy = rxy

The following components are available

Shapes

Circle

npm.io

→ Interactive demo of Circle

<Circle 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  size={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberCircumference of the Circle.true
cxnumberCenter x coordinate of the Circle.true
cynumberCenter x coordinate of the Circle.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Cross

npm.io

→ Interactive demo of Cross

<Cross 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Cross.true
heightnumberHeight of the Cross.true
cxnumberCenter x coordinate of the Cross.true
cynumberCenter x coordinate of the Cross.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Ellipse

npm.io

→ Interactive demo of Ellipse

<Ellipse 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Ellipse.true
heightnumberHeight of the Ellipse.true
cxnumberCenter x coordinate of the Ellipse.true
cynumberCenter x coordinate of the Ellipse.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Kite

npm.io

→ Interactive demo of Kite

<Kite 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  dh={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Kite.true
heightnumberHeight of the Kite.true
dhnumberVertical position of the left and right points from the top.true
cxnumberCenter x coordinate of the Kite.true
cynumberCenter x coordinate of the Kite.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Lens

npm.io

→ Interactive demo of Lens

<Lens 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Lens.true
heightnumberHeight of the Lens.true
cxnumberCenter x coordinate of the Lens.true
cynumberCenter x coordinate of the Lens.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Line

npm.io

→ Interactive demo of Line

<Line 
  ex={number} 
  ey={number} 
  relative={boolean|default = false} 
  sx={number} 
  sy={number} 
/>
PropTypeDescriptionRequiredDefault
sxnumberStarting x coordinate for the Line.true
synumberStarting y coordinate for the Line.true
exnumberEnding x coordinate for the Line.true
eynumberEnding y coordinate for the Line.true
relativebooleanIf set to true ex & ey will become relative to sx & sy.truefalse

Omino

npm.io

→ Interactive demo of Omino

<Omino 
  lined={boolean|default = false} 
  shape={2d-array} 
  size={number} 
  sx={number} 
  sy={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberSize of the squares.true
shape2d-array2d array of the Omino.true
sxnumberStarting x coordinate for left.true
synumberStarting y coordinate for top.true
linedbooleanDraw inner lines or not.falsefalse

Polygon

npm.io

→ Interactive demo of Polygon

<Polygon 
  points={point-array} 
/>
PropTypeDescriptionRequiredDefault
pointspoint-arrayx, y, points of the polygon.true

Polygram

npm.io

→ Interactive demo of Polygram

<Polygram 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  points={number} 
  size={number} 
  vertexSkip={number|default = 2} 
/>
PropTypeDescriptionRequiredDefault
sizenumberSize of the underlying polygon.true
pointsnumberNumber of points to create.true
cxnumberCenter x coordinate of the Polygram.true
cynumberCenter x coordinate of the Polygram.true
vertexSkipnumberInteger representing which vertex to go to next relative to current.false2
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Polyline

npm.io

→ Interactive demo of Polyline

<Polyline 
  points={point-array} 
  relative={boolean|default = false} 
/>
PropTypeDescriptionRequiredDefault
pointspoint-arrayx, y, points of the Polyline.true
relativebooleanIf set to true all points will be relative.falsefalse

RadialLines

npm.io

→ Interactive demo of RadialLines

<RadialLines 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  innerSize={number} 
  outerSize={number} 
  points={number} 
/>
PropTypeDescriptionRequiredDefault
outerSizenumberCircumference of the outer circle.true
innerSizenumberCircumference of the inner circle.true
pointsnumberNumber of lines to draw.true
cxnumberCenter x coordinate of the RadialLines.true
cynumberCenter x coordinate of the RadialLines.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Rect

npm.io

→ Interactive demo of Rect

<Rect 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Rect.true
heightnumberHeight of the Rect.true
cxnumberCenter x coordinate of the Rect.true
cynumberCenter x coordinate of the Rect.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

RegPolygon

npm.io

→ Interactive demo of RegPolygon

<RegPolygon 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  sides={number} 
  size={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberSize of the RegPolygon.true
sidesnumberNumber of sides of the RegPolygon.true
cxnumberCenter x coordinate of the RegPolygon.true
cynumberCenter x coordinate of the RegPolygon.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

RoundedRect

npm.io

→ Interactive demo of RoundedRect

<RoundedRect 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  radius={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the Rect.true
heightnumberHeight of the Rect.true
radiusnumberRadius for the corners.true
cxnumberCenter x coordinate of the RoundedRect.true
cynumberCenter x coordinate of the RoundedRect.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

RoundedSquare

npm.io

→ Interactive demo of RoundedSquare

<RoundedSquare 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  radius={number} 
  size={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberWidth & height of the Square.true
radiusnumberRadius for the corners.true
cxnumberCenter x coordinate of the RoundedSquare.true
cynumberCenter x coordinate of the RoundedSquare.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Sector

npm.io

→ Interactive demo of Sector

<Sector 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  endAngle={number} 
  size={number} 
  startAngle={number} 
/>
PropTypeDescriptionRequiredDefault
cxnumberCenter x coordinate of the Sector.true
cynumberCenter x coordinate of the Sector.true
sizenumberCircumference of the Sector.true
startAnglenumberStart angle of the Sector. 0 = top center.true
endAnglenumberEnd angle of the Sector. 0 = top center.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Segment

npm.io

→ Interactive demo of Segment

<Segment 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  endAngle={number} 
  size={number} 
  startAngle={number} 
/>
PropTypeDescriptionRequiredDefault
cxnumberCenter x coordinate of the Segment.true
cynumberCenter x coordinate of the Segment.true
sizenumberCircumference of the Segment.true
startAnglenumberStart angle of the Segment. 0 = top center.true
endAnglenumberEnd angle of the Segment. 0 = top center.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Square

npm.io

→ Interactive demo of Square

<Square 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  size={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberWidth & height of the Square.true
cxnumberCenter x coordinate of the Square.true
cynumberCenter x coordinate of the Square.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Star

npm.io

→ Interactive demo of Star

<Star 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  innerSize={number} 
  outerSize={number} 
  points={number} 
/>
PropTypeDescriptionRequiredDefault
outerSizenumberThe outer circumference where points will reach.true
innerSizenumberThe inner circumference where points will end.true
pointsnumberNumber of points for the Star.true
cxnumberCenter x coordinate of the Star.true
cynumberCenter x coordinate of the Star.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

SymH

npm.io

→ Interactive demo of SymH

<SymH 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the H.true
heightnumberHeight of the H.true
cxnumberCenter x coordinate of the SymH.true
cynumberCenter x coordinate of the SymH.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

SymI

npm.io

→ Interactive demo of SymI

<SymI 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the I.true
heightnumberHeight of the I.true
cxnumberCenter x coordinate of the SymI.true
cynumberCenter x coordinate of the SymI.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

SymX

npm.io

→ Interactive demo of SymX

<SymX 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  height={number} 
  width={number} 
/>
PropTypeDescriptionRequiredDefault
widthnumberWidth of the X.true
heightnumberHeight of the X.true
cxnumberCenter x coordinate of the SymX.true
cynumberCenter x coordinate of the SymX.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Triangle

npm.io

→ Interactive demo of Triangle

<Triangle 
  centerEnd={boolean|default = true} 
  cx={number} 
  cy={number} 
  size={number} 
/>
PropTypeDescriptionRequiredDefault
sizenumberSize of the Tirangle.true
cxnumberCenter x coordinate of the Triangle.true
cynumberCenter x coordinate of the Triangle.true
centerEndbooleanDetermines whether cursor should return to cx & cy as a last step.falsetrue

Curves

Arc

npm.io

→ Interactive demo of Arc

<Arc 
  arc={number|default = 0} 
  ex={number} 
  ey={number} 
  relative={boolean|default = false} 
  rotation={number|default = 0} 
  rx={number} 
  ry={number} 
  sweep={number|default = 0} 
  sx={number} 
  sy={number} 
/>
PropTypeDescriptionRequiredDefault
sxnumberStarting x coordinate for the Arc.true
synumberStarting y coordinate for the Arc.true
rxnumberWidth of the underlying ellipse of the Arc.true
rynumberHeight of the underlying ellipse of the Arc.true
rotationnumberRotation of the underlying ellipse of the Arc.false0
arcnumberLarge arc flag: this says whether to follow the larger or smaller part of the underlying ellipse.false0
sweepnumberSweep flag: think of this as direction flag, follow a clockwise or counterclockwise path along the underlying ellipse.false0
exnumberEnding x coordinate for the Arc.true
eynumberEnding y coordinate for the Arc.true
relativebooleanIf set to true all points after sx & sy will become relative to sx & sy.falsefalse

Cubic

npm.io

→ Interactive demo of Cubic

<Cubic 
  S={point-array} 
  cx1={number} 
  cx2={number} 
  cy1={number} 
  cy2={number} 
  ex={number} 
  ey={number} 
  relative={boolean|default = false} 
  s={point-array} 
  sx={number} 
  sy={number} 
/>
PropTypeDescriptionRequiredDefault
sxnumberStarting x coordinate for the Cubic.true
synumberStarting y coordinate for the Cubic.true
cx1numberx coordinate for control point 1.true
cy1numbery coordinate for control point 1.true
cx2numberx coordinate for control point 2.true
cy2numbery coordinate for control point 2.true
exnumberEnding x coordinate for the Cubic.true
eynumberEnding y coordinate for the Cubic.true
Spoint-arrayOptionally String together multiple Cubic wit an array consisting of 2 or more control points.false
spoint-arrayOptional relative "smoooth curve" array consisting of 2 or more control points.false
relativebooleanIf set to true all points after sx & sy will become relative to sx & sy.falsefalse

Quad

npm.io

→ Interactive demo of Quad

<Quad 
  T={point-array} 
  cx={number} 
  cy={number} 
  ex={number} 
  ey={number} 
  relative={boolean|default = false} 
  sx={number} 
  sy={number} 
  t={point-array} 
/>
PropTypeDescriptionRequiredDefault
sxnumberStarting x coordinate for the Quad.true
synumberStarting y coordinate for the Quad.true
cxnumberx coordinate for the control point.true
cynumbery coordinate for the control point.true
exnumberEnding x coordinate for the Quad.true
eynumberEnding y coordinate for the Quad.true
Tpoint-arrayString together multiple Quad curves.false
tpoint-arrayString together multiple Quad curves where coordinates are relative.false
relativebooleanIf set to true all points after sx & sy will become relative to sx & sy.falsefalse

License

MIT © joemaddalone

1.14.0

1 year ago

1.13.3

2 years ago

1.13.2

3 years ago

1.13.1

3 years ago

1.12.1

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago