Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] RoundedRect clipping bug #1480

Open
dylancom opened this issue Apr 6, 2023 · 11 comments
Open

[iOS] RoundedRect clipping bug #1480

dylancom opened this issue Apr 6, 2023 · 11 comments
Labels
bug Something isn't working

Comments

@dylancom
Copy link

dylancom commented Apr 6, 2023

Description

I am using react-native-skia so I can create buttons that use filters such as an "inner shadow".
When using multiple Rounded Rects, and centering them horizontally, I have noticed that some are clipped on the right by approx 1 pixel. If I remove the horizontal centering, the clipping bug disappears.

Notice the 3rd "button" not being perfectly rounded on the right.
Scherm­afbeelding 2023-04-06 om 09 38 41

When placing a background color on the <Canvas> I have noticed that it's the RoundedRect sometimes taking 1 pixel less on the right, but the Canvas itself always has the correct width.
Scherm­afbeelding 2023-04-06 om 09 47 32

Version

0.1.182

Steps to reproduce

  1. Create a reusable component that consists of a <Canvas> and a <RoundedRect>.
  2. Place multiple instances of that component within a <View> that has a "row" direction.
  3. Place the row View within a container View that centers the row view horizontally.

Snack, code example, screenshot, or link to a repository

https://github.com/dylancom/react-native-skia-clipping

@dylancom dylancom added the bug Something isn't working label Apr 6, 2023
@william-candillon
Copy link
Contributor

Could it be due to pixel rounding?
https://reactnative.dev/docs/pixelratio
I've made a video on this topic: https://www.youtube.com/watch?v=a8EWaNVxXz4

@dylancom
Copy link
Author

dylancom commented Apr 6, 2023

Thanks for the fast reply.
Ah I see that the 3rd one is not exactly 96 pixels by using onLayout on the Canvas.

 LOG  {"layout": {"height": 48, "width": 96, "x": 0, "y": 0}, "target": 3}
 LOG  {"layout": {"height": 48, "width": 96, "x": 104, "y": 0}, "target": 5}
 LOG  {"layout": {"height": 48, "width": 95.99998474121094, "x": 208, "y": 0}, "target": 7}

Making use of roundToNearestPixel doesn't change the behavior though.

@william-candillon
Copy link
Contributor

Should we close the issue or do you think there is something that could/should be done on our side?

@william-candillon
Copy link
Contributor

you draw a rect of width 96 on a canvas of 95.999 and it looks like one pixel has been clipped because to some rounding.
This seems ok no?
I am not sure what would be the expected behaviour nor if there is something we can do on our side.

Where the rounding happened exactly because there are a lot of operations done related to pixel density on both RN and Skia at different places.

@dylancom
Copy link
Author

dylancom commented Apr 6, 2023

Yes, the canvas actually rendering a width slightly less than the requested 96 is indeed causing the clipping.
Oke, I thought there might be some workaround but using roundToNearestPixel doesn't fix anything in this case.

@dylancom
Copy link
Author

dylancom commented Apr 6, 2023

Update: I just found a workaround.
If you horizontally center the buttons with a marginLeft using a rounded number, the problem is gone.
(instead of using alignItems: 'center')

So for e.g. marginLeft: Math.round(availableSpace / 2).

@wcandillon
Copy link
Contributor

@dylancom could roundToNearestPixel the size of the flex container work too? I will close this issue since the same would happen with a regular view? But if there is something specific to Skia here, please let us know and we will reopen this issue.

@dylancom
Copy link
Author

Let me try it, thanks for the suggestion. It's specific to using this library. There is no clipping when I use regular views to create rounded buttons or circles. (I'm using this library though to create inner shadows, which I can't with a regular view)

It also happened when I had a heading with a bottom border using StyleSheet.hairlineWidth and placing multiple circles, created with Skia, below. The bottom of the circles would be clipped by approx 1px. (Unless I add a negative margin top of the size of the hairlineWidth to the circles, which fixes the clipping).

@wcandillon
Copy link
Contributor

@dylacom This sounds interesting, could you provide us with a small example where you display these three buttons as RN View (and maybe set a border of harlineWidth) and then below have the same with Skia view instead? We will definitely investigate it if they produce a different result.

@dylancom
Copy link
Author

dylancom commented Apr 16, 2023

@wcandillon see the following examples (https://github.com/dylancom/react-native-skia-clipping/blob/main/App.tsx):
Tested on a iPhone 14 Pro simulator.
The added bottom border (using hairlineWidth) to the titles causes extra clipping to the bottom.

Scherm­afbeelding 2023-04-16 om 10 18 09

Scherm­afbeelding 2023-04-16 om 10 12 37

@wcandillon wcandillon reopened this Apr 17, 2023
@wcandillon
Copy link
Contributor

Thank you for the nice reproducible example, I can confirm that something is off here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants