Skip to content

Commit

Permalink
fix: getBBox size (#2549)
Browse files Browse the repository at this point in the history
# 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   |    ✅      |
  • Loading branch information
jakex7 authored Nov 21, 2024
1 parent d1d936a commit d125be1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions apple/RNSVGRenderableModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ @implementation RNSVGRenderableModule
[svg getPath:nil];

CGRect bounds = CGRectNull;
if (fill) {
if (fill && !CGRectIsEmpty(svg.fillBounds)) {
bounds = CGRectUnion(bounds, svg.fillBounds);
}
if (stroke) {
if (stroke && !CGRectIsEmpty(svg.strokeBounds)) {
bounds = CGRectUnion(bounds, svg.strokeBounds);
}
if (markers) {
if (markers && !CGRectIsEmpty(svg.markerBounds)) {
bounds = CGRectUnion(bounds, svg.markerBounds);
}
if (clipped) {
Expand All @@ -155,8 +155,9 @@ @implementation RNSVGRenderableModule
bounds = CGRectIntersection(bounds, clipBounds);
}
}
if (CGRectIsNull(bounds))
if (CGRectIsNull(bounds)) {
bounds = CGRectZero;
}
CGPoint origin = bounds.origin;
CGSize size = bounds.size;
return @{@"x" : @(origin.x), @"y" : @(origin.y), @"width" : @(size.width), @"height" : @(size.height)};
Expand Down

0 comments on commit d125be1

Please sign in to comment.