Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
react-native-svg
Advanced tools
The react-native-svg package allows React Native developers to render SVG images within their mobile applications. It provides a set of SVG primitives that work in a similar way to the standard React Native components. With react-native-svg, you can create complex shapes, paths, and graphics that are scalable without losing quality, which is essential for responsive design.
Drawing basic shapes
This code sample demonstrates how to draw a basic circle shape with a green fill and a blue stroke.
{"<Svg height=\"100\" width=\"100\"><Circle cx=\"50\" cy=\"50\" r=\"45\" stroke=\"blue\" strokeWidth=\"2.5\" fill=\"green\"/></Svg>"}
Creating paths
This code sample shows how to create a path using the 'Path' component, which allows for more complex shapes and lines.
{"<Svg height=\"100\" width=\"100\"><Path d=\"M40 60 A 10 10 0 0 0 60 60\" fill=\"none\" stroke=\"black\"/></Svg>"}
Applying gradients
This code sample illustrates how to apply a linear gradient to an ellipse shape.
{"<Svg height=\"100\" width=\"100\"><Defs><LinearGradient id=\"grad\" x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"0%\"><Stop offset=\"0%\" stopColor=\"rgb(255,255,0)\" stopOpacity=\"0\" /><Stop offset=\"100%\" stopColor=\"rgb(255,0,0)\" stopOpacity=\"1\" /></LinearGradient></Defs><Ellipse cx=\"50\" cy=\"50\" rx=\"50\" ry=\"20\" fill=\"url(#grad)\"/></Svg>"}
Text rendering
This code sample shows how to render text within an SVG using the 'Text' component.
{"<Svg height=\"100\" width=\"100\"><Text x=\"50\" y=\"50\" textAnchor=\"middle\" fill=\"purple\">SVG Text</Text></Svg>"}
Victory-native is a collection of components for building interactive data visualizations in React Native applications. It is built on top of react-native-svg and provides a higher-level abstraction for creating charts and graphs. While react-native-svg is more about drawing SVG elements, victory-native focuses on data representation and charting.
React-native-canvas provides a 2D drawing context similar to HTML canvas. It is not SVG-based but allows for dynamic graphics and animations. It can be more performant for complex animations but lacks the scalability of SVG.
React-native-art-svg is another library for rendering SVG images in React Native. It is based on the ART library, which is a retained mode vector drawing system. It offers similar functionalities to react-native-svg but has a different API and is less commonly used.
react-native-svg
is built to provide a SVG interface to react native on both iOS and Android
react-native-art-svg
has changed name to react-native-svg
(thanks for @Brent Vatne handing over me this npm package.).
And furthermore:
react-native-svg
NO LONGER rely on ReactNativeART, it has its own native code dependency now (which is based on ReactNativeART code but support more SVG features)
npm install react-native-svg --save
2 . Link native code
react-native link react-native-svg
react-native@0.29.0 and 0.29.1 cannot work with Android link properly:here
Or use rnpm instead
rnpm link react-native-svg
There is an easy example
import Svg,{
Circle,
Ellipse,
G,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Symbol,
Text,
Use,
Defs,
Stop
} from 'react-native-svg';
class SvgExample extends Component {
render() {
return (
<Svg
height="100"
width="100"
>
<Circle
cx="50"
cy="50"
r="45"
stroke="blue"
strokeWidth="2.5"
fill="green"
/>
<Rect
x="15"
y="15"
width="70"
height="70"
stroke="red"
strokeWidth="2"
fill="yellow"
/>
</Svg>
);
}
}
This will draw a graphics like this
Name | Default | Description |
---|---|---|
fill | '#000' | The fill prop refers to the color inside the shape. |
fillOpacity | 1 | This prop specifies the opacity of the color or the content the current object is filled with. |
stroke | 'none' | The stroke prop controls how the outline of a shape appears. |
strokeWidth | 1 | The strokeWidth prop specifies the width of the outline on the current object. |
strokeOpacity | 1 | The strokeOpacity prop specifies the opacity of the outline on the current object. |
strokeLinecap | 'square' | The strokeLinecap prop specifies the shape to be used at the end of open subpaths when they are stroked. |
strokeLinejoin | 'miter' | The strokeLinejoin prop specifies the shape to be used at the corners of paths or basic shapes when they are stroked. |
strokeDasharray | [] | The strokeDasharray prop controls the pattern of dashes and gaps used to stroke paths. |
strokeDashoffset | null | The strokeDashoffset prop specifies the distance into the dash pattern to start the dash. |
x | 0 | Translate distance on x-axis. |
y | 0 | Translate distance on y-axis. |
rotate | 0 | Rotation degree value on the current object. |
scale | 1 | Scale value on the current object. |
origin | 0, 0 | Transform origin coordinates for the current object. |
originX | 0 | Transform originX coordinates for the current object. |
originY | 0 | Transform originY coordinates for the current object. |
<Svg
height="100"
width="100"
>
<Rect x="0" y="0" width="100" height="100" fill="black" />
<Circle cx="50" cy="50" r="30" fill="yellow" />
<Circle cx="40" cy="40" r="4" fill="black" />
<Circle cx="60" cy="40" r="4" fill="black" />
<Path d="M 40 60 A 10 10 0 0 0 60 60" stroke="black" />
</Svg>;
The element is used to create a rectangle and variations of a rectangle shape:
<Svg
width="200"
height="60"
>
<Rect
x="25"
y="5"
width="150"
height="50"
fill="rgb(0,0,255)"
strokeWidth="3"
stroke="rgb(0,0,0)"
/>
</Svg>
Code explanation:
The element is used to create a circle:
<Svg
height="100"
width="100"
>
<Circle
cx="50"
cy="50"
r="50"
fill="pink"
/>
</Svg>
Code explanation:
The element is used to create an ellipse.
An ellipse is closely related to a circle. The difference is that an ellipse has an x and a y radius that differs from each other, while a circle has equal x and y radius
<Svg
height="100"
width="110"
>
<Ellipse
cx="55"
cy="55"
rx="50"
ry="30"
stroke="purple"
strokeWidth="2"
fill="yellow"
/>
</Svg>
Code explanation:
The element is an SVG basic shape, used to create a line connecting two points.
<Svg
height="100"
width="100"
>
<Line
x1="0"
y1="0"
x2="100"
y2="100"
stroke="red"
strokeWidth="2"
/>
</Svg>
Code explanation:
The element is used to create a graphic that contains at least three sides.
Polygons are made of straight lines, and the shape is "closed" (all the lines connect up).
<Svg
height="100"
width="100"
>
<Polygon
points="40,5 70,80 25,95"
fill="lime"
stroke="purple"
strokeWidth="1"
/>
</Svg>
Code explanation:
The element is used to create any shape that consists of only straight lines:
<Svg
height="100"
width="100"
>
<Polyline
points="10,10 20,12 30,20 40,60 60,70 95,90"
fill="none"
stroke="black"
strokeWidth="3"
/>
</Svg>
Code explanation:
The element is used to define a path.
The following commands are available for path data:
Note:
All of the commands above can also be expressed with lower letters. Capital letters means absolutely positioned, lower cases means relatively positioned.<Svg
height="100"
width="100"
>
<Path
d="M25 10 L98 65 L70 25 L16 77 L11 30 L0 4 L90 50 L50 10 L11 22 L77 95 L20 25"
fill="none"
stroke="red"
/>
</Svg>
The element is used to define a text.
<Svg
height="60"
width="200"
>
<Text
fill="none"
stroke="purple"
fontSize="20"
fontWeight="bold"
x="100"
y="20"
textAnchor="middle"
>STROKED TEXT</Text>
</Svg>
The element is a container used to group other SVG elements. Transformations applied to the g element are performed on all of its child elements, and any of its props are inherited by its child elements. It can also group multiple elements to be referenced later with the <Use /> element.
<Svg
height="100"
width="200"
>
<G
rotate="50"
origin="100, 50"
>
<Line
x1="60"
y1="10"
x2="140"
y2="10"
stroke="#060"
/>
<Rect
x="60"
y="20"
height="50"
width="80"
stroke="#060"
fill="#060"
/>
<Text
x="100"
y="75"
stroke="#600"
fill="#600"
textAnchor="middle"
>
Text grouped with shapes</Text>
</G>
</Svg>
The element can reuse an SVG shape from elsewhere in the SVG document, including elements and elements.
The reused shape can be defined inside the <Defs> element (which makes the shape invisible until used) or outside.
<Svg
height="100"
width="300"
>
<Defs>
<G id="shape">
<G>
<Circle cx="50" cy="50" r="50" />
<Rect x="50" y="50" width="50" height="50" />
<Circle cx="50" cy="50" r="5" fill="blue" />
</G>
</G>
</Defs>
<Use href="#shape" x="20" y="0"/>
<Use href="#shape" x="170"y="0" />
</Svg>
This example shows a element defined inside a <Defs> element. This makes the invisible unless referenced by a element.
Before the element can be referenced, it must have an ID set on it via its id prop. The element references the element via its href
prop. Notice the # in front of the ID in the prop value.
The element specifies where to show the reused shapes via its x and y props. Notice that the shapes inside the element are located at 0,0. That is done because their position is added to the position specified in the element.
The SVG element is used to define reusable symbols. The shapes nested inside a are not displayed unless referenced by a element.
<Svg
height="150"
width="110"
>
<Symbol id="symbol" viewbox="0 0 150 110" width="100" height="50">
<Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red"/>
<Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white"/>
</Symbol>
<Use
href="#symbol"
x="0"
y="0"
/>
<Use
href="#symbol"
x="0"
y="50"
width="75"
height="38"
/>
<Use
href="#symbol"
x="0"
y="100"
width="50"
height="25"
/>
</Svg>
The 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.
The element is used to define a linear gradient. The element must be nested within a <Defs> tag. The <Defs> tag is short for definitions and contains definition of special elements (such as gradients).
Linear gradients can be defined as horizontal, vertical or angular gradients:
<Svg
height="150"
width="300"
>
<Defs>
<LinearGradient id="grad" x1="0" y1="0" x2="170" y2="0">
<Stop offset="0" stopColor="rgb(255,255,0)" stopOpacity="0" />
<Stop offset="1" stopColor="red" stopOpacity="1" />
</LinearGradient>
</Defs>
<Ellipse cx="150" cy="75" rx="85" ry="55" fill="url(#grad)" />
</Svg>
Code explanation:
NOTICE: LinearGradient also supports percentage as prop:
<LinearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
<Stop offset="0%" stopColor="rgb(255,255,0)" stopOpacity="0" />
<Stop offset="100%" stopColor="red" stopOpacity="1" />
</LinearGradient>
This result is same as the example before.
But I recommend you to use exact number instead, it has more performance advantages while using number instead of percentage.
The element is used to define a radial gradient. The element must be nested within a <Defs> tag. The <Defs> tag is short for definitions and contains definition of special elements (such as gradients).
<Svg
height="150"
width="300"
>
<Defs>
<RadialGradient id="grad" cx="150" cy="75" rx="85" ry="55" fx="150" fy="75">
<Stop
offset="0"
stopColor="#ff0"
stopOpacity="1"
/>
<Stop
offset="1"
stopColor="#83a"
stopOpacity="1"
/>
</RadialGradient>
</Defs>
<Ellipse cx="150" cy="75" rx="85" ry="55" fill="url(#grad)" />
</Svg>
Code explanation:
cd ./Example
npm install
FAQs
SVG library for react-native
The npm package react-native-svg receives a total of 899,087 weekly downloads. As such, react-native-svg popularity was classified as popular.
We found that react-native-svg demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.