Skip to content

Commit d125be1

Browse files
authored
fix: getBBox size (#2549)
# Summary When calling `getBBox` on rect, it will not give correct dimensions as `markers` that are 90% `CGRectZero` so CGRectUnion between some values and (0, 0, 0, 0) will return (0, 0, width, height) ## Test Plan ```tsx import React, { useRef } from 'react'; import { Button, View } from 'react-native'; import { Rect, Svg } from 'react-native-svg'; function App(): React.JSX.Element { const ref1 = useRef<Rect>(null); return ( <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}> <Svg width={300} height={300}> <Rect x={100} y={100} width={100} height={100} fill="red" opacity={0.5} ref={ref1} /> </Svg> <Button title="Press me" onPress={() => console.log(ref1.current?.getBBox())} /> </View> ); } export default App; ``` Before: return `{"height": 100, "width": 100, "x": 0, "y": 0}` After: return `{"height": 100, "width": 100, "x": 100, "y": 100}` ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | MacOS | ✅ |
1 parent d1d936a commit d125be1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

apple/RNSVGRenderableModule.mm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ @implementation RNSVGRenderableModule
139139
[svg getPath:nil];
140140

141141
CGRect bounds = CGRectNull;
142-
if (fill) {
142+
if (fill && !CGRectIsEmpty(svg.fillBounds)) {
143143
bounds = CGRectUnion(bounds, svg.fillBounds);
144144
}
145-
if (stroke) {
145+
if (stroke && !CGRectIsEmpty(svg.strokeBounds)) {
146146
bounds = CGRectUnion(bounds, svg.strokeBounds);
147147
}
148-
if (markers) {
148+
if (markers && !CGRectIsEmpty(svg.markerBounds)) {
149149
bounds = CGRectUnion(bounds, svg.markerBounds);
150150
}
151151
if (clipped) {
@@ -155,8 +155,9 @@ @implementation RNSVGRenderableModule
155155
bounds = CGRectIntersection(bounds, clipBounds);
156156
}
157157
}
158-
if (CGRectIsNull(bounds))
158+
if (CGRectIsNull(bounds)) {
159159
bounds = CGRectZero;
160+
}
160161
CGPoint origin = bounds.origin;
161162
CGSize size = bounds.size;
162163
return @{@"x" : @(origin.x), @"y" : @(origin.y), @"width" : @(size.width), @"height" : @(size.height)};

0 commit comments

Comments
 (0)