mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-20 22:05:14 +00:00
fix percentage bug, add Svg element flex layout support
This commit is contained in:
@@ -11,13 +11,13 @@ class EllipseExample extends Component{
|
|||||||
render() {
|
render() {
|
||||||
return <Svg
|
return <Svg
|
||||||
height="100"
|
height="100"
|
||||||
width="110"
|
width="200"
|
||||||
>
|
>
|
||||||
<Ellipse
|
<Ellipse
|
||||||
cx="50%"
|
cx="50%"
|
||||||
cy="50%"
|
cy="50%"
|
||||||
rx="45%"
|
rx="45%"
|
||||||
ry="30%"
|
ry="40%"
|
||||||
stroke="purple"
|
stroke="purple"
|
||||||
strokeWidth="2"
|
strokeWidth="2"
|
||||||
fill="yellow"
|
fill="yellow"
|
||||||
|
|||||||
@@ -1,11 +1,26 @@
|
|||||||
import React, {
|
import React, {
|
||||||
Component
|
Component,
|
||||||
|
StyleSheet,
|
||||||
|
View
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
container: {
|
||||||
|
flex: 1,
|
||||||
|
height: 100,
|
||||||
|
width: 200
|
||||||
|
},
|
||||||
|
svg: {
|
||||||
|
flex: 1,
|
||||||
|
alignSelf: 'stretch'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
import Svg, {
|
import Svg, {
|
||||||
Circle,
|
Circle,
|
||||||
Rect,
|
Rect,
|
||||||
Path
|
Path,
|
||||||
|
Line
|
||||||
} from 'react-native-svg';
|
} from 'react-native-svg';
|
||||||
|
|
||||||
class SvgExample extends Component{
|
class SvgExample extends Component{
|
||||||
@@ -83,6 +98,41 @@ class SvgViewbox extends Component{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SvgLayout extends Component{
|
||||||
|
static title = 'SVG with flex layout';
|
||||||
|
render() {
|
||||||
|
return <View style={styles.container}>
|
||||||
|
<Svg style={styles.svg}>
|
||||||
|
<Rect
|
||||||
|
width="80%"
|
||||||
|
height="80%"
|
||||||
|
x="10%"
|
||||||
|
y="10%"
|
||||||
|
fill="purple"
|
||||||
|
stroke="yellow"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
<Line
|
||||||
|
x1="10%"
|
||||||
|
y1="10%"
|
||||||
|
x2="90%"
|
||||||
|
y2="90%"
|
||||||
|
stroke="yellow"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
<Line
|
||||||
|
x1="10%"
|
||||||
|
y1="90%"
|
||||||
|
x2="90%"
|
||||||
|
y2="10%"
|
||||||
|
stroke="yellow"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
</Svg>
|
||||||
|
</View>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const icon = <Svg
|
const icon = <Svg
|
||||||
height="20"
|
height="20"
|
||||||
width="20"
|
width="20"
|
||||||
@@ -106,7 +156,7 @@ const icon = <Svg
|
|||||||
/>
|
/>
|
||||||
</Svg>;
|
</Svg>;
|
||||||
|
|
||||||
const samples = [SvgExample, SvgOpacity, SvgViewbox];
|
const samples = [SvgExample, SvgOpacity, SvgViewbox, SvgLayout];
|
||||||
|
|
||||||
export {
|
export {
|
||||||
icon,
|
icon,
|
||||||
|
|||||||
@@ -42,25 +42,29 @@ class Svg extends Component{
|
|||||||
};
|
};
|
||||||
|
|
||||||
measureInWindow = (...args) => {
|
measureInWindow = (...args) => {
|
||||||
this.refs.root.measureInWindow(...args);
|
this.root.measureInWindow(...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
measure = (...args) => {
|
measure = (...args) => {
|
||||||
this.refs.root.measure(...args);
|
this.root.measure(...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
measureLayout = (...args) => {
|
measureLayout = (...args) => {
|
||||||
this.refs.root.measureLayout(...args);
|
this.root.measureLayout(...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
setNativeProps = (...args) => {
|
setNativeProps = (...args) => {
|
||||||
this.refs.root.setNativeProps(...args);
|
this.root.setNativeProps(...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let {props} = this;
|
let {props} = this;
|
||||||
let opacity = +props.opacity;
|
let opacity = +props.opacity;
|
||||||
let content = props.viewbox ? <ViewBox
|
let width = +props.width;
|
||||||
|
let height = +props.height;
|
||||||
|
let flexLayout = !isNaN(width) || !isNaN(height);
|
||||||
|
|
||||||
|
let content = (props.viewbox && !flexLayout) ? <ViewBox
|
||||||
viewbox={props.viewbox}
|
viewbox={props.viewbox}
|
||||||
preserveAspectRatio={props.preserveAspectRatio}
|
preserveAspectRatio={props.preserveAspectRatio}
|
||||||
width={props.width}
|
width={props.width}
|
||||||
@@ -69,18 +73,18 @@ class Svg extends Component{
|
|||||||
{this.getChildren()}
|
{this.getChildren()}
|
||||||
</ViewBox> : this.getChildren();
|
</ViewBox> : this.getChildren();
|
||||||
|
|
||||||
let width = +props.width || 0;
|
|
||||||
let height = +props.height || 0;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NativeSvgView
|
<NativeSvgView
|
||||||
ref="root"
|
ref={ele => this.root = ele}
|
||||||
style={[
|
style={[
|
||||||
props.style,
|
props.style,
|
||||||
!isNaN(opacity) && {
|
!isNaN(opacity) && {
|
||||||
opacity
|
opacity
|
||||||
},
|
},
|
||||||
{width, height}
|
!flexLayout && {
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
}
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
{content}
|
{content}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@
|
|||||||
// draw ellipse
|
// draw ellipse
|
||||||
CGFloat cx = [self getActualProp:@"cx" relative:width];
|
CGFloat cx = [self getActualProp:@"cx" relative:width];
|
||||||
CGFloat cy = [self getActualProp:@"cy" relative:height];
|
CGFloat cy = [self getActualProp:@"cy" relative:height];
|
||||||
CGFloat rx = [self getActualProp:@"rx" relative:height];
|
CGFloat rx = [self getActualProp:@"rx" relative:width];
|
||||||
CGFloat ry = [self getActualProp:@"ry" relative:height];
|
CGFloat ry = [self getActualProp:@"ry" relative:height];
|
||||||
CGPathAddEllipseInRect(path, nil, CGRectMake(cx - rx, cy - ry, rx * 2, ry * 2));
|
CGPathAddEllipseInRect(path, nil, CGRectMake(cx - rx, cy - ry, rx * 2, ry * 2));
|
||||||
break;
|
break;
|
||||||
@@ -60,9 +60,9 @@
|
|||||||
case 2:
|
case 2:
|
||||||
{
|
{
|
||||||
// draw line
|
// draw line
|
||||||
CGFloat x1 = [self getActualProp:@"x1" relative:height];
|
CGFloat x1 = [self getActualProp:@"x1" relative:width];
|
||||||
CGFloat y1 = [self getActualProp:@"y1" relative:height];
|
CGFloat y1 = [self getActualProp:@"y1" relative:height];
|
||||||
CGFloat x2 = [self getActualProp:@"x2" relative:height];
|
CGFloat x2 = [self getActualProp:@"x2" relative:width];
|
||||||
CGFloat y2 = [self getActualProp:@"y2" relative:height];
|
CGFloat y2 = [self getActualProp:@"y2" relative:height];
|
||||||
CGPathMoveToPoint(path, nil, x1, y1);
|
CGPathMoveToPoint(path, nil, x1, y1);
|
||||||
CGPathAddLineToPoint(path, nil, x2, y2);
|
CGPathAddLineToPoint(path, nil, x2, y2);
|
||||||
|
|||||||
@@ -11,5 +11,4 @@
|
|||||||
#import "RNSVGContainer.h"
|
#import "RNSVGContainer.h"
|
||||||
|
|
||||||
@interface RNSVGSvgView : UIView <RNSVGContainer>
|
@interface RNSVGSvgView : UIView <RNSVGContainer>
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -15,20 +15,20 @@
|
|||||||
|
|
||||||
- (void)invalidate
|
- (void)invalidate
|
||||||
{
|
{
|
||||||
[self setNeedsDisplay];
|
[self setNeedsDisplay];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)drawRect:(CGRect)rect
|
- (void)drawRect:(CGRect)rect
|
||||||
{
|
{
|
||||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||||
for (RNSVGNode *node in self.subviews) {
|
for (RNSVGNode *node in self.subviews) {
|
||||||
[node renderTo:context];
|
[node renderTo:context];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
|
- (void)reactSetInheritedBackgroundColor:(UIColor *)inheritedBackgroundColor
|
||||||
{
|
{
|
||||||
self.backgroundColor = inheritedBackgroundColor;
|
self.backgroundColor = inheritedBackgroundColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ RCT_EXPORT_MODULE()
|
|||||||
|
|
||||||
- (UIView *)view
|
- (UIView *)view
|
||||||
{
|
{
|
||||||
return [RNSVGSvgView new];
|
return [RNSVGSvgView new];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user