react-native: Rotate transform not working well on View with borderRadius

When rotating a component, its children also rotate as expected, except for the Views with borderRadius > 0, which their rounded border does not rotate.

Environment

Environment: OS: macOS High Sierra 10.13.3 Node: 8.9.0 Yarn: 1.5.1 npm: 5.7.1 Watchman: 4.9.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed) react: ^16.3.0-alpha.1 => 16.3.0-alpha.1 react-native: ^0.54.0 => 0.54.0

Expected Behavior

Borders should rotate like everything else.

Actual Behavior

<div> SCREENSHOT GIF </div>

^ The one from the right has overflow: hidden on the main photo container, so things got worse. We actually can notice it’s not just the border, but the actual view mask / clip bounds that’s not rotating.

Steps to Reproduce

[ANDROID] https://snack.expo.io/@brunolemos/react-native---border-radius-rotate-bug

Full Code
import React, { Component } from 'react';
import { Image, Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.block}>
          <View style={styles.textContainer}>
            <Text>SQUARE</Text>
            <Image
              source={require('./assets/logo_facebook_square.png')}
              style={styles.image}
            />
          </View>
          <View style={[styles.textContainer, { borderRadius: 10 }]}>
            <Text>ROUND</Text>
            <Image
              source={require('./assets/logo_facebook_square.png')}
              style={[styles.image, { borderRadius: 10 }]}
            />
          </View>
        </View>

        <View style={[styles.block, { transform: [{ rotate: '-10deg' }] }]}>
          <View style={styles.textContainer}>
            <Text>SQUARE</Text>
            <Image
              source={require('./assets/logo_facebook_square.png')}
              style={styles.image}
            />
          </View>
          <View style={[styles.textContainer, { borderRadius: 10 }]}>
            <Text>ROUND</Text>
            <Image
              source={require('./assets/logo_facebook_square.png')}
              style={[styles.image, { borderRadius: 10 }]}
            />
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
  block: {
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 60,
    width: 200,
    height: 200,
    backgroundColor: 'white',
  },
  textContainer: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    margin: 10,
    padding: 10,
    borderWidth: 2,
    borderColor: 'red',
  },
  image: {
    width: 40,
    height: 40,
    borderWidth: 4,
    borderColor: 'green',
  },
});

Investigating + Related

After more investigation this is what I found:

Possibly related:

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 27
  • Comments: 41 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Im not sure whether it is an Android or React Native issue, but here is what is working using RN 0.59.8: Android 21 - Fine Android 22 - Fine Android 23 - Fine Android 24 - Not working Android 25 - Not working Android 26 - Not working Android 27 - Not working Android 28 - Not working Android Q - Fine

Still seeing this in 0.57 on Android. iOS works fine as originally noted.

Apart from child Views with borders not rotating, the rotated View’s own border is misbehaving too.

Before rotation: image

After a 45deg rotation: image

This is on 55.3

The issue still persists in React Native 0.73. The solution is to set the renderToHardwareTextureAndroid prop to true.

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Why would you close if everything worked fine on react-native@0.49.5 and got broke on 0.50+? Unless the fix is released on npm I don’t see why a bug should be closed.

From my understanding, there were two problems here.

  1. Border rendering was re-implemented sometime around v0.51.0, which may likely have broken rotated rounded borders. This problem has since been fixed in React Native. Since the fix is in master, it’ll eventually be shipped to npm.
  2. Sometime around v0.51.0 a clipping algorithm was also implemented to support overflow: hidden for views with non-rounded borders. I’m not completely sure about the history of borders and overflow: hidden in React Native, but I just tried v0.49.5 and overflow: hidden doesn’t work at all. Child views render on top of their parent’s borders, but are clipped to the outer edge of their parent regardless of the overflow property. image This clipping algorithm worked perfectly fine for rotated views. However, when it was extended to account for rounded borders, it broke due to what I think was an AOSP bug. Whatever the probem was, it was resolved in the latest release of Android.

So really, overflow: hidden is the only thing that’s presently broken. Specifically, I think it’s broken whenever the inner border edge has a curved corner, which happens when borderRadius > borderWidth. This problem also only occurs on Android API < 28.

From where I stand, I see two options:

  1. Do nothing and wait, and expect app developers to work around this problem. Apps running the latest edition of React Native (what’s currently on master and what will eventually be released on npm) and running on the latest edition of Android do not experience this problem. Plus, there’s an easy way to circumvent this problem, which is to ensure that borderWidth > borderRadius or to not use borders. This is a bit of a bummer, I know.
  2. Hack together a solution for overflow:hidden on android devices running API < 28. Maybe this involves adding a nested view so the contents of the border are clipped to the outer edge of the inner view. Or maybe, there’s—there probably is—a better solution. I’m really not sure. (What I do know is that we have to be careful about view hierarchy depth on older Android devices, because they can easily run into stack overflow errors. So, maybe the multi-layer solution isn’t the best idea?)

Previously, I was leaning more toward the first option. That’s why I suggested that we should close this issue. But now that I think about it a bit more, I’m not sure what the best approach is. Any thoughts, @shergin?

Edit 1: I’ll do some more thorough testing and see if things actually are working. Edit 2: Hmm… so it looks like API 28 works perfectly for all cases I’ve tried, but with borders whose width is specified using border(Top|Left|Bottom|Right)Width, devices running API 27 and below don’t render rotated borders correctly. Sigh…

The issue is caused from the canvas cropping on a LAYER_TYPE_NONE on Android Sdk 24-28. Adding the below code to ReactViewGroup.java fixes the issue. I am preparing a pull request.

// Disable Hardware Accelleration on Views with property overflow hidden
// for SDK 24-28 https://github.com/facebook/react-native/issues/18266
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N 
  && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { 
    setLayerType(View.LAYER_TYPE_HARDWARE, null);
}

before canvas.clipPath(mPath)

https://github.com/facebook/react-native/blob/4f5a092bf68a0cd825328ce4a1e6bb41a8fad2e3/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java#L862-L863

Result in the example from @RSNara comment before and after the fix.

The issue was caused from clipping the view that had property overflow: 'hidden' and LAYER_TYPE_NONE, in specific drawing an area (mPath) to crop the canvas from the before mentioned view using a method called addRoundRect, which would not take in consideration the view rotation previously applied with view.setRotation()

https://github.com/facebook/react-native/blob/4f5a092bf68a0cd825328ce4a1e6bb41a8fad2e3/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java#L849-L860

The original expo snack posted from @brunolemos does not reproduce anymore on react-native master, but the issue in the above example from @RSNara still reproduces.

The issue and solution was tested on Physical Devices and Emulators running Android Sdk 24-29, my pull request will explicitly set the layerType for Views with property overflow: 'hidden' on devices running Android 24-28 and avoid this issue.

I am preparing the pull request. Thanks a lot for reading this message 😃

Still seen at 0.61.5 and Android SDK 25, but not seen at Android SDK 29, seems using overflow: 'visible' for the parent view could solve the problem.

Same shit different day😭

I also still have this issue on some devices. Working fine on Android 6.0.1 but not on 8.1.0. Using RN 0.59.4

Tagged as a regression so the bot does not close this due to staleness. Thanks @rsnara for the writeup. 0.56.0-rc should have the fixes you mentioned.

Hey @brunolemos!

Your problem reproduces on React Native ~0.55.2. As evidence, here’s a screenshot:

image

But I also ran your code against React Native master and saw that the rounded border rendering has been fixed. Screenshot:

image

However, it still looks like clipping is broken (with rounded borders).

Code: https://snack.expo.io/Hyc6HMPxX

Edit: I did some digging around and narrowed it down to the canvas.clipPath call inside ReactViewGroup.drawdispatchOverflowDraw. Clipping works perfectly fine if I zero all the radii and use canvas.clipPath. However, if I leave the radii non-zero, it fails. An interesting consequence is that when borderWidth > borderRadius, clipping works correctly (because the clipped rectangle won’t be rounded at that point).

I’ll look into this a bit further.

+1

I can reproduce this on the latest stable release.

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest stable release?

Thank you for your contributions.

How to ContributeWhat to Expect from Maintainers

This happened in react-native 0.61.0. When set rotate and border Radius and overflow hidden to the parent, the child works fine, but when I set background color to children, parent border radius does not work.

I’m trying to prepare a pull request to fix this, would be happy to receive your advice. I think this was caused from: https://github.com/facebook/react-native/blob/b81c8b51fc6fe3c2dece72e3fe500e175613c5d4/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java#L809-L823

Changing the code fixed the issue diplayed in the comment from @RSNara. Issue would reproduce as from your screenshot above. The result is displayed below.

The radii array of floats [X,Y] for each corner of the square are the radius distance used to draw the corner. The path is a square with rounded corners used to clip the canvas available for drawing.

In the below example the clipped Canvas is displayed in red while the rectangle displayed in the Canvas is displayed in blue. If we set [X,Y] to [0,0], we fix this as we do not crop corners (but it is not the solution).

I believe the clipping is caused by using the wrong [X,Y] values. The [X,Y] values should be the same used to draw the parent corners.

Issues that I found till now: FIRST-ISSUE: The borderRadius is converted from DPI to Pixels while the original value is in Pixels: https://github.com/facebook/react-native/blob/673cbb3110855c45beb7e340b61e7daf927d9ade/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java#L107-L114

SECOND-ISSUE: I don’t see any logic to transform the mPath and give it the -10dg rotation before clipping. I applied the transformation, but seems to not fix the problem. Removing the transform from the View fixes the problem, so I believe this is connected to transform and clip. https://github.com/facebook/react-native/blob/b81c8b51fc6fe3c2dece72e3fe500e175613c5d4/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java#L822

THIRD-ISSUE: The issue is caused by using addRoundRect or drawing the path with lineTo and quadTo. The problem is not experienced when using addRect or removing the rounded corners.

Related Issue https://github.com/facebook/react-native/issues/3198

You could try fixing this by using prop rendertohardwaretextureandroid with value of true

https://reactnative.dev/docs/view#rendertohardwaretextureandroid

Whether this View should render itself (and all of its children) into a single hardware texture on the GPU.

On Android, this is useful for animations and interactions that only modify opacity, rotation, translation, and/or scale: in those cases, the view doesn’t have to be redrawn and display lists don’t need to be re-executed. The texture can be re-used and re-composited with different parameters. The downside is that this can use up limited video memory, so this prop should be set back to false at the end of the interaction/animation.

My pr would just enable hardware accelleration on that view after applying the border radius, but you could solve this issue by enabling hardware accelleration on the view in Javascript for Android API N till P

https://github.com/facebook/react-native/blob/e6658a203db20e7950648de55cb80f14961470e1/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java#L267-L270

the prop allows you to call setLayerType(HARDWARE) on each React View.

https://github.com/facebook/react-native/blob/1465c8f3874cdee8c325ab4a4916fda0b3e43bdb/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java#L120-L122

https://github.com/facebook/react-native/issues/29312#issuecomment-892405146

We are also running into this issue. Tried removing individual border radius widths and sizes, however that breaks specifying different border radius colours for each side on the affect platforms.

Changing this:

const AStyledView = styled.View`
  border-top-width: 8;
  border-bottom-width: 8;
  border-right-width: 8;
  border-left-width: 8;
  border-top-right-radius: 50;
  border-top-left-radius: 50;
  border-bottom-left-radius: 50;
  border-bottom-right-radius: 50;
  border-right-color: yellow;
  border-top-color: yellow;
  border-left-color: transparent;
  border-bottom-color: transparent;
  transform: rotateZ(45deg);
`;

To this:

const AStyledView = styled.View`
  border-width: 8;
  border-radius: 50;
  border-right-color: yellow;
  border-top-color: yellow;
  border-left-color: transparent;
  border-bottom-color: transparent;
  transform: rotateZ(45deg);
`;

Breaks the separate border colours, they are all black on android. Works on iOS.

@johnyoonh can you open a new issue? This is marked as fix, so we’d need to get more details from you in order to determine if this is the same issue or something else.

I was able to get overflow: 'hidden' to work by also adding the style zIndex: 1 to the same element. It seems #3198 was closed.