UNPKG

35.1 kBMarkdownView Raw
1## react-native-svg
2
3[![Version](https://img.shields.io/npm/v/react-native-svg.svg)](https://www.npmjs.com/package/react-native-svg)
4[![NPM](https://img.shields.io/npm/dm/react-native-svg.svg)](https://www.npmjs.com/package/react-native-svg)
5
6`react-native-svg` provides SVG support to React Native on iOS and Android, and a compatibility layer for the web.
7
8[Check out the demo](https://snack.expo.io/@msand/react-native-svg-example)
9
10## Features
11
121. Supports most SVG elements and properties (Rect, Circle, Line, Polyline, Polygon, G ...).
132. Easy to [convert SVG code](https://svgr.now.sh/) to react-native-svg.
14
15- [Installation](#installation)
16 - [Automatically](#automatically)
17 - [Manually](#manually)
18 - [Android](#android)
19 - [iOS](#ios)
20- [Troubleshooting](#troubleshooting)
21- [Usage](#usage)
22 - [Use with content loaded from uri](#use-with-content-loaded-from-uri)
23 - [Use with svg files](#use-with-svg-files)
24- [Common props](#common-props)
25- [Supported elements](#supported-elements)
26 - [Svg](#svg)
27 - [Rect](#rect)
28 - [Circle](#circle)
29 - [Ellipse](#ellipse)
30 - [Line](#line)
31 - [Polygon](#polygon)
32 - [Polyline](#polyline)
33 - [Path](#path)
34 - [Text](#text)
35 - [TSpan](#tspan)
36 - [TextPath](#textpath)
37 - [G](#g)
38 - [Use](#use)
39 - [Symbol](#symbol)
40 - [Defs](#defs)
41 - [Image](#image)
42 - [ClipPath](#clippath)
43 - [LinearGradient](#lineargradient)
44 - [RadialGradient](#radialgradient)
45 - [Mask](#mask)
46 - [Pattern](#pattern)
47- [Touch Events](#touch-events)
48- [Run example](#run-example)
49- [TODO](#todo)
50- [Known issues](#known-issues)
51
52### Installation
53
54#### Automatically
55
56*With Expo, this is pre-installed. Jump ahead to [Usage](#Usage)*
57
581. Install library from `npm`
59
60 ```bash
61 yarn add react-native-svg
62 ```
63
642. Link native code
65
66 With autolinking (react-native 0.60+)
67 ```bash
68 cd ios && pod install
69 ```
70
71 Pre 0.60
72 ```bash
73 react-native link react-native-svg
74 ```
75
76
77# NOTICE:
78
79Due to breaking changes in react-native, the version given in the left column
80(and higher versions) of react-native-svg only supports the react-native version
81in the right column (and higher versions, if possible).
82
83It is recommended to use the version of react given in the peer dependencies
84of the react-native version you are using.
85
86The latest version of react-native-svg should always work in a clean react-native project.
87
88| react-native-svg | react-native |
89|------------------|--------------|
90| 3.2.0 | 0.29 |
91| 4.2.0 | 0.32 |
92| 4.3.0 | 0.33 |
93| 4.4.0 | 0.38 |
94| 4.5.0 | 0.40 |
95| 5.1.8 | 0.44 |
96| 5.2.0 | 0.45 |
97| 5.3.0 | 0.46 |
98| 5.4.1 | 0.47 |
99| 5.5.1 | >=0.50 |
100| >=6 | >=0.50 |
101| >=7 | >=0.57.4 |
102| >=8 | >=0.57.4 |
103| >=9 | >=0.57.4 |
104
105Or, include [this PR](https://github.com/facebook/react-native/pull/17842) manually for v7+ stability on android for older RN ( [included in 0.57-stable](https://github.com/facebook/react-native/commit/d9f5319cf0d9828b29d0e350284b22ce29985042) and newer).
106
107The latest version of v6, v7, v8 and v9 should all work in the latest react-native version.
108
109v7 and newer requires the patch for making android thread safe, to get native animation support.
110
111#### Manually
112
113##### Android pre RN 0.60
114
1151. `yarn add react-native-svg` In RN 0.60+, this is all you should ever need to do get Android working. Before this, react-native link was responsible for the following steps:
116
1172. Append the following lines to `android/settings.gradle`:
118
119 ```
120 include ':react-native-svg'
121 project(':react-native-svg').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-svg/android')
122 ```
123
1243. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
125
126 ```
127 implementation project(':react-native-svg')
128 ```
129
1304. Open up `android/app/src/main/java/[...]/MainApplication.java`
131 - Add `import com.horcrux.svg.SvgPackage;` to the imports at the top of the file
132 - Add `new SvgPackage()` to the list returned by the `getPackages()` method. Add a comma to the previous item if there's already something there.
133
134##### iOS pre RN 0.60
135
136[Manual linking](http://facebook.github.io/react-native/docs/linking-libraries-ios.html#manual-linking)
137
138To install react-native-svg on iOS visit the link referenced above or do the following (react-native link should do this for you):
139
1401. Open your project in XCode and drag the `RNSVG.xcodeproj` file (located in `.../node_modules/react-native-svg/ios`) into the Libraries directory shown in XCode.
1412. Expand the `RNSVG.xcodeproj` file you just added to XCode until you see: `libRNSVG.a` (located in `RNSVG.xcodeproj` > `Products` )
1423. Drag `libRNSVG.a` into the Link Binary With Libraries section (located in Build Phases which may be found at the top of the XCode window)
143
144###### CocoaPods
145
146Alternatively, you can use [CocoaPods](https://cocoapods.org/) to manage your native (Objective-C and Swift) dependencies:
147
1481. Add RNSVG to your Podfile (with RN 0.60+ autolinking, this is not needed)
149```
150pod 'RNSVG', :path => '../node_modules/react-native-svg'
151```
152
153If `cocoapods` is used and if error `RNSVGImage.m:12:9: 'React/RCTImageLoader.h' file not found` occurs:
154
155Add the following entry in Podfile:
156
157```ruby
158 pod 'React', :path => '../node_modules/react-native', :subspecs => [
159 [...]
160 'RCTImage', # <-- Add RCTImage
161 ]
162```
163
164and run `pod install` from `ios` folder
165
166### Troubleshooting
167
168If you have build errors, then it might be caused by caching issues, please try:
169
170```bash
171watchman watch-del-all
172rm -fr $TMPDIR/react-*
173react-native start --reset-cache
174
175Or,
176
177rm -rf node_modules
178yarn
179react-native start --reset-cache
180```
181
182#### Unexpected behavior
183
184If you have unexpected behavior, please create a clean project with the latest versions of react-native and react-native-svg
185
186```bash
187react-native init CleanProject
188cd CleanProject/
189yarn add react-native-svg
190cd ios && pod install && cd ..
191```
192
193Make a reproduction of the problem in `App.js`
194
195```bash
196react-native run-ios
197react-native run-android
198```
199
200# Opening issues
201
202Verify that it is still an issue with the latest version as specified in the previous step. If so, open a new issue, include the entire `App.js` file, specify what platforms you've tested, and the results of running this command:
203
204```bash
205react-native info
206```
207
208If you suspect that you've found a spec conformance bug, then you can test using your component in a react-native-web project by forking this codesandbox, to see how different browsers render the same content: https://codesandbox.io/s/pypn6mn3y7 If any evergreen brower with significant userbase or other svg user agent renders some svg content better, or supports more of the svg and related specs, please open an issue asap.
209
210### <a name="Usage">Usage</a>
211
212Here's a simple example. To render output like this:
213
214![SVG example](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/svg.png)
215
216Use the following code:
217
218```javascript
219import Svg,{
220 Circle,
221 Ellipse,
222 G,
223 Text,
224 TSpan,
225 TextPath,
226 Path,
227 Polygon,
228 Polyline,
229 Line,
230 Rect,
231 Use,
232 Image,
233 Symbol,
234 Defs,
235 LinearGradient,
236 RadialGradient,
237 Stop,
238 ClipPath,
239 Pattern,
240 Mask,
241} from 'react-native-svg';
242
243/* Use this if you are using Expo
244import { Svg } from 'expo';
245const { Circle, Rect } = Svg;
246*/
247
248import React from 'react';
249import { View, StyleSheet } from 'react-native';
250
251export default class SvgExample extends React.Component {
252 render() {
253 return (
254 <View
255 style={[
256 StyleSheet.absoluteFill,
257 { alignItems: 'center', justifyContent: 'center' },
258 ]}>
259 <Svg height="50%" width="50%" viewBox="0 0 100 100">
260 <Circle
261 cx="50"
262 cy="50"
263 r="45"
264 stroke="blue"
265 strokeWidth="2.5"
266 fill="green"
267 />
268 <Rect
269 x="15"
270 y="15"
271 width="70"
272 height="70"
273 stroke="red"
274 strokeWidth="2"
275 fill="yellow"
276 />
277 </Svg>
278 </View>
279 );
280 }
281}
282```
283
284[Try this on Snack](https://snack.expo.io/@msand/react-native-svg-example)
285
286### Use with content loaded from uri
287
288Try [react-native-svg-uri](https://github.com/vault-development/react-native-svg-uri)
289```jsx
290import * as React from 'react';
291import SvgUri from 'react-native-svg-uri';
292
293export default () => (
294 <SvgUri
295 width="200"
296 height="200"
297 source={{
298 uri: 'http://thenewcode.com/assets/images/thumbnails/homer-simpson.svg',
299 }}
300 />
301);
302```
303
304### Use with svg files
305
306Try [react-native-svg-transformer](https://github.com/kristerkari/react-native-svg-transformer) to get compile time conversion and cached transformations.
307https://github.com/kristerkari/react-native-svg-transformer#installation-and-configuration
308https://github.com/kristerkari/react-native-svg-transformer#for-react-native-v057-or-newer--expo-sdk-v3100-or-newer
309
310`metro.config.js`
311
312```js
313const { getDefaultConfig } = require("metro-config");
314
315module.exports = (async () => {
316 const {
317 resolver: { sourceExts, assetExts }
318 } = await getDefaultConfig();
319 return {
320 transformer: {
321 babelTransformerPath: require.resolve("react-native-svg-transformer")
322 },
323 resolver: {
324 assetExts: assetExts.filter(ext => ext !== "svg"),
325 sourceExts: [...sourceExts, "svg"]
326 }
327 };
328})();
329```
330
331Import your .svg file inside a React component:
332
333```jsx
334import Logo from "./logo.svg";
335```
336
337You can then use your image as a component:
338
339```jsx
340<Logo width={120} height={40} />
341```
342
343Alternatively, you can use [react-native-svg-uri](https://github.com/vault-development/react-native-svg-uri) with [babel-plugin-inline-import](https://github.com/credcollective/babel-plugin-inline-import/), but with transforms done at run-time.
344
345.babelrc
346```json
347{
348 "presets": ["module:metro-react-native-babel-preset"],
349 "plugins": [
350 ["babel-plugin-inline-import", {
351 "extensions": [
352 ".svg"
353 ]
354 }]
355 ]
356}
357```
358
359App.js
360```jsx
361import * as React from 'react';
362import SvgUri from 'react-native-svg-uri';
363import testSvg from './test.svg';
364export default () => (
365 <SvgUri
366 width="200"
367 height="200"
368 svgXmlData={testSvg}
369 />
370);
371```
372
373### Common props:
374
375Name | Default | Description
376----------------|------------|--------------
377fill | '#000' | The fill prop refers to the color inside the shape.
378fillOpacity | 1 | This prop specifies the opacity of the color or the content the current object is filled with.
379fillRule | 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`
380stroke | 'none' | The stroke prop controls how the outline of a shape appears.
381strokeWidth | 1 | The strokeWidth prop specifies the width of the outline on the current object.
382strokeOpacity | 1 | The strokeOpacity prop specifies the opacity of the outline on the current object.
383strokeLinecap | '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'`.
384strokeLinejoin | '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'`.
385strokeDasharray | [] | The strokeDasharray prop controls the pattern of dashes and gaps used to stroke paths.
386strokeDashoffset| null | The strokeDashoffset prop specifies the distance into the dash pattern to start the dash.
387x | 0 | Translate distance on x-axis.
388y | 0 | Translate distance on y-axis.
389rotation | 0 | Rotation degree value on the current object.
390scale | 1 | Scale value on the current object.
391origin | 0, 0 | Transform origin coordinates for the current object.
392originX | 0 | Transform originX coordinates for the current object.
393originY | 0 | Transform originY coordinates for the current object.
394
395
396### Supported elements:
397
398#### Svg
399
400```html
401<Svg
402 height="100"
403 width="100"
404>
405 <Rect x="0" y="0" width="100" height="100" fill="black" />
406 <Circle cx="50" cy="50" r="30" fill="yellow" />
407 <Circle cx="40" cy="40" r="4" fill="black" />
408 <Circle cx="60" cy="40" r="4" fill="black" />
409 <Path d="M 40 60 A 10 10 0 0 0 60 60" stroke="black" />
410</Svg>
411```
412
413Colors set in the Svg element are inherited by its children:
414
415```html
416<Svg
417 width="130"
418 height="130"
419 fill="blue"
420 stroke="red"
421 color="green"
422 viewBox="-16 -16 544 544"
423>
424 <Path
425 d="M318.37,85.45L422.53,190.11,158.89,455,54.79,350.38ZM501.56,60.2L455.11,13.53a45.93,45.93,0,0,0-65.11,0L345.51,58.24,449.66,162.9l51.9-52.15A35.8,35.8,0,0,0,501.56,60.2ZM0.29,497.49a11.88,11.88,0,0,0,14.34,14.17l116.06-28.28L26.59,378.72Z"
426 strokeWidth="32"
427 />
428 <Path d="M0,0L512,512" stroke="currentColor" strokeWidth="32" />
429</Svg>
430```
431
432![Pencil](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/pencil.png)
433
434 Code explanation:
435
436 * The fill prop defines the color inside the object.
437 * The stroke prop defines the color of the line drawn around the object.
438 * The color prop is a bit special in the sense that it won't color anything by itself, but define a kind of color variable that can be used by children elements. In this example we're defining a "green" color in the Svg element and using it in the second Path element via stroke="currentColor". The "currentColor" is what refers to that "green" value, and it can be used in other props that accept colors too, e.g. fill="currentColor".
439
440### Rect
441
442The <Rect> element is used to create a rectangle and variations of a rectangle shape:
443
444
445```html
446<Svg
447 width="200"
448 height="60"
449>
450 <Rect
451 x="25"
452 y="5"
453 width="150"
454 height="50"
455 fill="rgb(0,0,255)"
456 strokeWidth="3"
457 stroke="rgb(0,0,0)"
458 />
459</Svg>
460```
461
462![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/rect.png)
463
464 Code explanation:
465
466 * The width and height props of the <Rect> element define the height and the width of the rectangle.
467 * The x prop defines the left position of the rectangle (e.g. x="25" places the rectangle 25 px from the left margin).
468 * The y prop defines the top position of the rectangle (e.g. y="5" places the rectangle 5 px from the top margin).
469
470##### Circle
471
472The <Circle> element is used to create a circle:
473
474```html
475<Svg
476 height="100"
477 width="100"
478>
479 <Circle
480 cx="50"
481 cy="50"
482 r="50"
483 fill="pink"
484 />
485</Svg>
486```
487
488![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/circle.png)
489
490 Code explanation:
491
492 * 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)
493 * The r prop defines the radius of the circle
494
495#### Ellipse
496
497The <Ellipse> element is used to create an ellipse.
498
499An 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.
500
501```html
502<Svg
503 height="100"
504 width="110"
505>
506 <Ellipse
507 cx="55"
508 cy="55"
509 rx="50"
510 ry="30"
511 stroke="purple"
512 strokeWidth="2"
513 fill="yellow"
514 />
515</Svg>
516```
517![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/ellipse.png)
518
519Code explanation:
520
521* The cx prop defines the x coordinate of the center of the ellipse
522* The cy prop defines the y coordinate of the center of the ellipse
523* The rx prop defines the horizontal radius
524* The ry prop defines the vertical radius
525
526#### Line
527
528The <Line> element is an SVG basic shape, used to create a line connecting two points.
529
530```html
531<Svg
532 height="100"
533 width="100"
534>
535 <Line
536 x1="0"
537 y1="0"
538 x2="100"
539 y2="100"
540 stroke="red"
541 strokeWidth="2"
542 />
543</Svg>
544```
545
546![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/line.png)
547
548Code explanation:
549
550* The x1 prop defines the start of the line on the x-axis.
551* The y1 prop defines the start of the line on the y-axis.
552* The x2 prop defines the end of the line on the x-axis.
553* The y2 prop defines the end of the line on the y-axis.
554
555#### Polygon
556
557The <Polygon> 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).
558
559```html
560<Svg
561 height="100"
562 width="100"
563>
564 <Polygon
565 points="40,5 70,80 25,95"
566 fill="lime"
567 stroke="purple"
568 strokeWidth="1"
569 />
570</Svg>
571```
572
573![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/polygon.png)
574
575Code explanation:
576
577* The points prop defines the x and y coordinates for each corner of the polygon
578
579#### Polyline
580
581The <Polyline> element is used to create any shape that consists of only straight lines:
582
583```html
584<Svg
585 height="100"
586 width="100"
587>
588 <Polyline
589 points="10,10 20,12 30,20 40,60 60,70 95,90"
590 fill="none"
591 stroke="black"
592 strokeWidth="3"
593 />
594</Svg>
595```
596
597![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/polyline.png)
598
599Code explanation:
600
601* The points prop defines the x and y coordinates for each point of the polyline
602
603#### Path
604
605The <Path> element is used to define a path.
606
607The following commands are available for path data:
608
609 * M = moveto
610 * L = lineto
611 * H = horizontal lineto
612 * V = vertical lineto
613 * C = curveto
614 * S = smooth curveto
615 * Q = quadratic Bézier curve
616 * T = smooth quadratic Bézier curveto
617 * A = elliptical Arc
618 * Z = closepath
619
620`Note:` All of the commands above can also be expressed with lower letters. Capital letters means absolutely positioned, lower cases means relatively positioned. See [Path document of SVG](https://www.w3.org/TR/SVG/paths.html) to know parameters for each command.
621
622```html
623<Svg
624 height="100"
625 width="100"
626>
627 <Path
628 d="M25 10 L98 65 L70 25 L16 77 L11 30 L0 4 L90 50 L50 10 L11 22 L77 95 L20 25"
629 fill="none"
630 stroke="red"
631 />
632</Svg>
633```
634
635![Rect](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/path.png)
636
637
638#### Text
639
640The <Text> element is used to define text.
641
642```html
643<Svg
644 height="60"
645 width="200"
646>
647 <Text
648 fill="none"
649 stroke="purple"
650 fontSize="20"
651 fontWeight="bold"
652 x="100"
653 y="20"
654 textAnchor="middle"
655 >STROKED TEXT</Text>
656</Svg>
657```
658
659![Text](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/text.png)
660
661#### TSpan
662
663The <TSpan> element is used to draw multiple lines of text in SVG. Rather than having to position each line of text absolutely, the <TSpan> element makes it possible to position a line of text relatively to the previous line of text.
664
665```html
666<Svg
667 height="160"
668 width="200"
669>
670 <Text y="20" dx="5 5">
671 <TSpan x="10" >tspan line 1</TSpan>
672 <TSpan x="10" dy="15">tspan line 2</TSpan>
673 <TSpan x="10" dx="10" dy="15">tspan line 3</TSpan>
674 </Text>
675 <Text x="10" y="60" fill="red" fontSize="14">
676 <TSpan dy="5 10 20" >12345</TSpan>
677 <TSpan fill="blue" dy="15" dx="0 5 5">
678 <TSpan>6</TSpan>
679 <TSpan>7</TSpan>
680 </TSpan>
681 <TSpan dx="0 10 20" dy="0 20" fontWeight="bold" fontSize="12">89a</TSpan>
682 </Text>
683 <Text y="140" dx="0 5 5" dy="0 -5 -5">delta on text</Text>
684</Svg>
685```
686
687![TSpan](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/tspan.png)
688
689#### TextPath
690
691In addition to text drawn in a straight line, SVG also includes the ability to place text along the shape of a <Path> element. To specify that a block of text is to be rendered along the shape of a <Path>, include the given text within a <TextPath> element which includes an href attribute with a reference to a <Path> element.
692
693```html
694<Svg
695 height="100"
696 width="200"
697>
698 <Defs>
699 <Path
700 id="path"
701 d={path}
702 />
703 </Defs>
704 <G y="20">
705 <Text
706 fill="blue"
707
708 >
709 <TextPath href="#path" startOffset="-10%">
710 We go up and down,
711 <TSpan fill="red" dy="5,5,5">then up again</TSpan>
712 </TextPath>
713 </Text>
714 <Path
715 d={path}
716 fill="none"
717 stroke="red"
718 strokeWidth="1"
719 />
720 </G>
721</Svg>
722```
723
724![TextPath](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/text-path.png)
725
726#### G
727
728The <G> 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 [&lt;Use /&gt;](#use) element.
729
730```html
731<Svg
732 height="100"
733 width="200"
734>
735 <G
736 rotation="50"
737 origin="100, 50"
738 >
739 <Line
740 x1="60"
741 y1="10"
742 x2="140"
743 y2="10"
744 stroke="#060"
745 />
746
747 <Rect
748 x="60"
749 y="20"
750 height="50"
751 width="80"
752 stroke="#060"
753 fill="#060"
754 />
755
756 <Text
757 x="100"
758 y="75"
759 stroke="#600"
760 fill="#600"
761 textAnchor="middle"
762 >
763 Text grouped with shapes</Text>
764 </G>
765</Svg>
766```
767![G](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/g.png)
768
769#### Use
770
771The <Use> element can reuse an SVG shape from elsewhere in the SVG document, including <G> elements and <Symbol> elements. The reused shape can be defined inside the [&lt;Defs&gt;](#defs) element (which makes the shape invisible until used) or outside.
772
773```html
774<Svg
775 height="100"
776 width="300"
777>
778 <Defs>
779 <G id="shape">
780 <G>
781 <Circle cx="50" cy="50" r="50" />
782 <Rect x="50" y="50" width="50" height="50" />
783 <Circle cx="50" cy="50" r="5" fill="blue" />
784 </G>
785 </G>
786 </Defs>
787 <Use href="#shape" x="20" y="0"/>
788 <Use href="#shape" x="170"y="0" />
789</Svg>
790```
791
792This example shows a <G> element defined inside a [&lt;Defs&gt;](#defs) element. This makes the <G> invisible unless referenced by a <Use> element.
793
794Before the <G> element can be referenced, it must have an ID set on it via its id prop. The <Use> element references the <G> element via its `href` prop. Notice the # in front of the ID in the prop value.
795
796The <Use> element specifies where to show the reused shapes via its x and y props. Notice that the shapes inside the <G> element are located at 0,0. That is done because their position is added to the position specified in the <Use> element.
797
798![use](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/use.png)
799
800
801#### Symbol
802
803The SVG <Symbol> element is used to define reusable symbols. The shapes nested inside a <Symbol> are not displayed unless referenced by a <Use> element.
804
805```html
806<Svg
807 height="150"
808 width="110"
809>
810 <Symbol id="symbol" viewBox="0 0 150 110" width="100" height="50">
811 <Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
812 <Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
813 </Symbol>
814
815 <Use
816 href="#symbol"
817 x="0"
818 y="0"
819 />
820 <Use
821 href="#symbol"
822 x="0"
823 y="50"
824 width="75"
825 height="38"
826 />
827 <Use
828 href="#symbol"
829 x="0"
830 y="100"
831 width="50"
832 height="25"
833 />
834</Svg>
835```
836
837![Symbol](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/symbol.png)
838
839#### Defs
840
841The <Defs> element is used to embed definitions that can be reused inside an SVG image. For instance, you can group SVG shapes together and reuse them as a single shape.
842
843#### Image
844
845The <Image> element allows a raster image to be included in an Svg componenet.
846
847```html
848<Svg
849 height="100"
850 width="100"
851>
852 <Defs>
853 <ClipPath id="clip">
854 <Circle cx="50%" cy="50%" r="40%"/>
855 </ClipPath>
856 </Defs>
857 <Rect
858 x="0"
859 y="0"
860 width="100%"
861 height="100%"
862 fill="red"
863 />
864 <Rect
865 x="5%"
866 y="5%"
867 width="50%"
868 height="90%"
869 />
870
871 <Image
872 x="5%"
873 y="5%"
874 width="50%"
875 height="90%"
876 preserveAspectRatio="xMidYMid slice"
877 opacity="0.5"
878 href={require('../image.jpg')}
879 clipPath="url(#clip)"
880 />
881 <Text
882 x="50"
883 y="50"
884 textAnchor="middle"
885 fontWeight="bold"
886 fontSize="16"
887 fill="blue"
888 >HOGWARTS</Text>
889</Svg>
890```
891
892![Image](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/image.png)
893
894#### ClipPath
895
896The <ClipPath> SVG element defines a clipping path. A clipping path is used/referenced using the clipPath property
897
898```html
899<Svg
900 height="100"
901 width="100"
902>
903 <Defs>
904 <RadialGradient id="grad" cx="50%" cy="50%" rx="50%" ry="50%" fx="50%" fy="50%" gradientUnits="userSpaceOnUse">
905 <Stop
906 offset="0%"
907 stopColor="#ff0"
908 stopOpacity="1"
909 />
910 <Stop
911 offset="100%"
912 stopColor="#00f"
913 stopOpacity="1"
914 />
915 </RadialGradient>
916 <ClipPath id="clip">
917 <G scale="0.9" x="10">
918 <Circle cx="30" cy="30" r="20"/>
919 <Ellipse cx="60" cy="70" rx="20" ry="10" />
920 <Rect x="65" y="15" width="30" height="30" />
921 <Polygon points="20,60 20,80 50,70" />
922 <Text
923 x="50"
924 y="30"
925 fontSize="32"
926 fonWeight="bold"
927 textAnchor="middle"
928 scale="1.2"
929 >Q</Text>
930 </G>
931 </ClipPath>
932 </Defs>
933 <Rect
934 x="0"
935 y="0"
936 width="100"
937 height="100"
938 fill="url(#grad)"
939 clipPath="url(#clip)"
940 />
941</Svg>
942```
943
944![ClipPath](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/clip-path.png)
945
946#### LinearGradient
947
948The <LinearGradient> element is used to define a linear gradient.
949The <LinearGradient> element must be nested within a [&lt;Defs&gt;](#defs) tag. The [&lt;Defs&gt;](#defs) tag is short for definitions and contains definition of special elements (such as gradients).
950
951Linear gradients can be defined as horizontal, vertical or angular gradients:
952
953 * Horizontal gradients are created when y1 and y2 are equal and x1 and x2 differ
954 * Vertical gradients are created when x1 and x2 are equal and y1 and y2 differ
955 * Angular gradients are created when x1 and x2 differ and y1 and y2 differ
956
957```html
958<Svg
959 height="150"
960 width="300"
961>
962 <Defs>
963 <LinearGradient id="grad" x1="0" y1="0" x2="170" y2="0">
964 <Stop offset="0" stopColor="rgb(255,255,0)" stopOpacity="0" />
965 <Stop offset="1" stopColor="red" stopOpacity="1" />
966 </LinearGradient>
967 </Defs>
968 <Ellipse cx="150" cy="75" rx="85" ry="55" fill="url(#grad)" />
969</Svg>
970```
971
972Code explanation:
973
974 * The id prop of the <LinearGradient> tag defines a unique name for the gradient
975 * The x1, x2, y1,y2 props of the <LinearGradient> tag define the start and end position of the gradient
976 * The color range for a gradient can be composed of two or more colors. Each color is specified with a <Stop> tag. The offset prop is used to define where the gradient color begin and end
977 * The fill prop links the ellipse element to the gradient
978
979![LinearGradient](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/lineargradient.png)
980
981*NOTICE:*
982LinearGradient also supports percentage as prop:
983```html
984<LinearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
985 <Stop offset="0%" stopColor="rgb(255,255,0)" stopOpacity="0" />
986 <Stop offset="100%" stopColor="red" stopOpacity="1" />
987</LinearGradient>
988```
989This result is same as the example before. But it's recommend to use exact number instead; it has performance advantages over using percentages.
990
991#### RadialGradient
992
993The <RadialGradient> element is used to define a radial gradient. The <RadialGradient> element must be nested within a [&lt;Defs&gt;](#defs) tag. The [&lt;Defs&gt;](#defs) tag is short for definitions and contains definition of special elements (such as gradients).
994
995```html
996<Svg
997 height="150"
998 width="300"
999>
1000 <Defs>
1001 <RadialGradient id="grad" cx="150" cy="75" rx="85" ry="55" fx="150" fy="75" gradientUnits="userSpaceOnUse">
1002 <Stop
1003 offset="0"
1004 stopColor="#ff0"
1005 stopOpacity="1"
1006 />
1007 <Stop
1008 offset="1"
1009 stopColor="#83a"
1010 stopOpacity="1"
1011 />
1012 </RadialGradient>
1013 </Defs>
1014 <Ellipse cx="150" cy="75" rx="85" ry="55" fill="url(#grad)" />
1015</Svg>
1016```
1017
1018Code explanation:
1019
1020 * The id prop of the <radialGradient> tag defines a unique name for the gradient
1021 * The cx, cy and r props define the outermost circle and the fx and fy define the innermost circle
1022 * The color range for a gradient can be composed of two or more colors. Each color is specified with a <stop> tag. The offset prop is used to define where the gradient color begin and end
1023 * The fill prop links the ellipse element to the gradient
1024
1025![RadialGradient](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/radialgradient.png)
1026
1027#### Mask
1028
1029
1030In SVG, you can specify that any other graphics object or ‘G’ element can be used as an alpha mask for compositing the current object into the background.
1031
1032A mask is defined with a ‘Mask’ element. A mask is used/referenced using the ‘mask’ property.
1033
1034A ‘Mask’ can contain any graphical elements or container elements such as a ‘G’.
1035
1036The <Mask> element must be nested within a [&lt;Defs&gt;](#defs) tag. The [&lt;Defs&gt;](#defs) tag is short for definitions and contains definition of special elements (such as gradients).
1037
1038https://www.w3.org/TR/SVG11/images/masking/mask01.svg
1039```html
1040 <Svg width="100%" height="100%" viewBox="0 0 800 300">
1041 <Defs>
1042 <LinearGradient
1043 id="Gradient"
1044 gradientUnits="userSpaceOnUse"
1045 x1="0"
1046 y1="0"
1047 x2="800"
1048 y2="0"
1049 >
1050 <Stop offset="0" stopColor="white" stopOpacity="0" />
1051 <Stop offset="1" stopColor="white" stopOpacity="1" />
1052 </LinearGradient>
1053 <Mask
1054 id="Mask"
1055 maskUnits="userSpaceOnUse"
1056 x="0"
1057 y="0"
1058 width="800"
1059 height="300"
1060 >
1061 <Rect
1062 x="0"
1063 y="0"
1064 width="800"
1065 height="300"
1066 fill="url(#Gradient)"
1067 />
1068 </Mask>
1069 <Text
1070 id="Text"
1071 x="400"
1072 y="200"
1073 fontFamily="Verdana"
1074 fontSize="100"
1075 textAnchor="middle"
1076 >
1077 Masked text
1078 </Text>
1079 </Defs>
1080 <Rect x="0" y="0" width="800" height="300" fill="#FF8080" />
1081 <Use href="#Text" fill="blue" mask="url(#Mask)" />
1082 <Use href="#Text" fill="none" stroke="black" stroke-width="2" />
1083 </Svg>
1084```
1085
1086Code explanation: https://www.w3.org/TR/SVG11/masking.html#MaskElement
1087
1088![Mask](https://www.w3.org/TR/SVG11/images/masking/mask01.svg)
1089
1090#### Pattern
1091
1092A pattern is used to fill or stroke an object using a pre-defined graphic object which can be replicated ("tiled") at fixed intervals in x and y to cover the areas to be painted. Patterns are defined using a ‘pattern’ element and then referenced by properties ‘fill’ and ‘stroke’ on a given graphics element to indicate that the given element shall be filled or stroked with the referenced pattern.
1093The <Mask> element must be nested within a [&lt;Defs&gt;](#defs) tag. The [&lt;Defs&gt;](#defs) tag is short for definitions and contains definition of special elements (such as gradients).
1094
1095https://www.w3.org/TR/SVG11/images/pservers/pattern01.svg
1096```html
1097 <Svg width="100%" height="100%" viewBox="0 0 800 400">
1098 <Defs>
1099 <Pattern
1100 id="TrianglePattern"
1101 patternUnits="userSpaceOnUse"
1102 x="0"
1103 y="0"
1104 width="100"
1105 height="100"
1106 viewBox="0 0 10 10"
1107 >
1108 <Path d="M 0 0 L 7 0 L 3.5 7 z" fill="red" stroke="blue" />
1109 </Pattern>
1110 </Defs>
1111 <Rect
1112 fill="none"
1113 stroke="blue"
1114 x="1"
1115 y="1"
1116 width="798"
1117 height="398"
1118 />
1119 <Ellipse
1120 fill="url(#TrianglePattern)"
1121 stroke="black"
1122 stroke-width="5"
1123 cx="400"
1124 cy="200"
1125 rx="350"
1126 ry="150"
1127 />
1128 </Svg>
1129```
1130
1131Code explanation: https://www.w3.org/TR/SVG11/pservers.html#PatternElement
1132
1133![Pattern](https://www.w3.org/TR/SVG11/images/pservers/pattern01.svg)
1134
1135#### Touch Events
1136
1137Touch events are supported in react-native-svg. These include:
1138
1139 - `disabled`
1140 - `onPress`
1141 - `onPressIn`
1142 - `onPressOut`
1143 - `onLongPress`
1144 - `delayPressIn`
1145 - `delayPressOut`
1146 - `delayLongPress`
1147
1148You can use these events to provide interactivity to your react-native-svg components.
1149
1150```html
1151<Circle
1152 cx="50%"
1153 cy="50%"
1154 r="38%"
1155 fill="red"
1156 onPress={() => alert('Press on Circle')}
1157/>
1158```
1159
1160![TouchEvents](https://raw.githubusercontent.com/react-native-community/react-native-svg/master/screenShoots/touchevents.gif)
1161
1162For more examples of touch in action, checkout the [TouchEvents.js examples](https://github.com/magicismight/react-native-svg-example/blob/master/examples/TouchEvents.js).
1163
1164### Run example:
1165
1166```bash
1167
1168git clone https://github.com/magicismight/react-native-svg-example.git
1169cd react-native-svg-example
1170yarn
1171
1172# run Android: react-native run-android
1173# run iOS: react-native run-ios
1174
1175```
1176
1177
1178### TODO:
11791. Add Native methods for elements.
11802. Marker element.
11813. Filters
1182
1183### Known issues:
11841. Unable to apply focus point of RadialGradient on Android.