UNPKG

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