## react-native-svg
[![Version](https://img.shields.io/npm/v/react-native-svg.svg)](https://www.npmjs.com/package/react-native-svg)
[![NPM](https://img.shields.io/npm/dm/react-native-svg.svg)](https://www.npmjs.com/package/react-native-svg)
`react-native-svg` is built to provide a SVG interface to react native on both iOS and Android.
[Check out the demo](https://getexponent.com/@rncommunity/react-native-svg-examples).
## Features
1. Supports most SVG elements and properties (Rect, Circle, Line, Polyline, Polygon, G ...).
2. Easy to convert SVG code into ReactNative code.
### Installation
#### Automatic
*With Expo, this is pre-installed. Jump ahead to [Usage](#Usage)*
1. Install library from `npm`
```bash
npm install react-native-svg --save
```
# NOTICE:
- react-native-svg >= 3.2.0 only supports react-native >= 0.29.0
- react-native-svg >= 4.2.0 only supports react-native >= 0.32.0
- react-native-svg >= 4.3.0 only supports react-native >= 0.33.0
- react-native-svg >= 4.4.0 only supports react-native >= 0.38.0 and react >= 15.4.0
- react-native-svg >= 4.5.0 only supports react-native >= 0.40.0 and react >= 15.4.0
- react-native-svg >= 5.1.8 only supports react-native >= 0.44.0 and react == 16.0.0-alpha.6
- react-native-svg >= 5.2.0 only supports react-native >= 0.45.0 and react == 16.0.0-alpha.12
- react-native-svg >= 5.3.0 only supports react-native >= 0.46.0 and react == 16.0.0-alpha.12
- react-native-svg >= 5.4.1 only supports react-native >= 0.47.0 and react == 16.0.0-alpha.12
- react-native-svg >= 5.5.1 only supports react-native >= 0.50.0 and react == 16.0.0
2. Link native code
```bash
react-native link react-native-svg
```
Or use `rnpm` instead:
```bash
rnpm link react-native-svg
```
#### Manual
##### Android
1. `npm install react-native-svg --save`
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-svg'
project(':react-native-svg').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-svg/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-svg')
```
4. Open up `android/app/src/main/java/[...]/MainApplication.java
- Add `import com.horcrux.svg.SvgPackage;` to the imports at the top of the file
- Add `new SvgPackage()` to the list returned by the `getPackages()` method. Add a comma to the previous item if there's already something there.
### Usage
Here's a simple example. To render output like this:
![SVG example](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/svg.png)
Use the following code:
```javascript
import Svg,{
Circle,
Ellipse,
G,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Symbol,
Text,
Use,
Defs,
Stop
} from 'react-native-svg';
class SvgExample extends Component {
render() {
return (
);
}
}
```
[Try this on Snack](https://snack.expo.io/r1hCVAaEZ)
### Common props:
Name | Default | Description
----------------|------------|--------------
fill | '#000' | The fill prop refers to the color inside the shape.
fillOpacity | 1 | This prop specifies the opacity of the color or the content the current object is filled with.
fillRule | nonzero | The fillRule prop determines what side of a path is inside a shape, which determines how fill will paint the shape, can be `nonzero` or `evenodd`
stroke | 'none' | The stroke prop controls how the outline of a shape appears.
strokeWidth | 1 | The strokeWidth prop specifies the width of the outline on the current object.
strokeOpacity | 1 | The strokeOpacity prop specifies the opacity of the outline on the current object.
strokeLinecap | 'square' | The strokeLinecap prop specifies the shape to be used at the end of open subpaths when they are stroked. Can be either `'butt'`, `'square'` or `'round'`.
strokeLinejoin | 'miter' | The strokeLinejoin prop specifies the shape to be used at the corners of paths or basic shapes when they are stroked. Can be either `'miter'`, `'bevel'` or `'round'`.
strokeDasharray | [] | The strokeDasharray prop controls the pattern of dashes and gaps used to stroke paths.
strokeDashoffset| null | The strokeDashoffset prop specifies the distance into the dash pattern to start the dash.
x | 0 | Translate distance on x-axis.
y | 0 | Translate distance on y-axis.
rotate | 0 | Rotation degree value on the current object.
scale | 1 | Scale value on the current object.
origin | 0, 0 | Transform origin coordinates for the current object.
originX | 0 | Transform originX coordinates for the current object.
originY | 0 | Transform originY coordinates for the current object.
### Supported elements:
#### Svg
```html
```
### Rect
The element is used to create a rectangle and variations of a rectangle shape:
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/rect.png)
Code explanation:
* The width and height props of the element define the height and the width of the rectangle.
* The x prop defines the left position of the rectangle (e.g. x="25" places the rectangle 25 px from the left margin).
* The y prop defines the top position of the rectangle (e.g. y="5" places the rectangle 5 px from the top margin).
##### Circle
The element is used to create a circle:
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/circle.png)
Code explanation:
* The cx and cy props define the x and y coordinates of the center of the circle. If cx and cy are omitted, the circle's center is set to (0,0)
* The r prop defines the radius of the circle
#### Ellipse
The element is used to create an ellipse.
An ellipse is closely related to a circle. The difference is that an ellipse has an x and a y radius that differs from each other, while a circle has equal x and y radius.
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/ellipse.png)
Code explanation:
* The cx prop defines the x coordinate of the center of the ellipse
* The cy prop defines the y coordinate of the center of the ellipse
* The rx prop defines the horizontal radius
* The ry prop defines the vertical radius
#### Line
The element is an SVG basic shape, used to create a line connecting two points.
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/line.png)
Code explanation:
* The x1 prop defines the start of the line on the x-axis.
* The y1 prop defines the start of the line on the y-axis.
* The x2 prop defines the end of the line on the x-axis.
* The y2 prop defines the end of the line on the y-axis.
#### Polygon
The element is used to create a graphic that contains at least three sides. Polygons are made of straight lines, and the shape is "closed" (all the lines connect up).
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/polygon.png)
Code explanation:
* The points prop defines the x and y coordinates for each corner of the polygon
#### Polyline
The element is used to create any shape that consists of only straight lines:
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/polyline.png)
Code explanation:
* The points prop defines the x and y coordinates for each point of the polyline
#### Path
The element is used to define a path.
The following commands are available for path data:
* M = moveto
* L = lineto
* H = horizontal lineto
* V = vertical lineto
* C = curveto
* S = smooth curveto
* Q = quadratic Bézier curve
* T = smooth quadratic Bézier curveto
* A = elliptical Arc
* Z = closepath
`Note:` All of the commands above can also be expressed with lower letters. Capital letters means absolutely positioned, lower cases means relatively positioned.
```html
```
![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/path.png)
#### Text
The element is used to define text.
```html
```
![Text](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/text.png)
#### TSpan
The element is used to draw multiple lines of text in SVG. Rather than having to position each line of text absolutely, the element makes it possible to position a line of text relatively to the previous line of text.
```html
```
![TSpan](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/tspan.png)
#### TextPath
In addition to text drawn in a straight line, SVG also includes the ability to place text along the shape of a element. To specify that a block of text is to be rendered along the shape of a , include the given text within a element which includes an href attribute with a reference to a element.
```html
```
![TextPath](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/text-path.png)
#### G
The element is a container used to group other SVG elements. Transformations applied to the g element are performed on all of its child elements, and any of its props are inherited by its child elements. It can also group multiple elements to be referenced later with the [<Use />](#use) element.
```html
```
![G](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/g.png)
#### Use
The