UNPKG

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