UNPKG

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