meliorence / react-native-snap-carousel

Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
BSD 3-Clause "New" or "Revised" License
10.35k stars 2.29k forks source link

Floating point precision runs into weird behavior in looping mode #560

Open TheHacky opened 5 years ago

TheHacky commented 5 years ago

286 # Is this a bug report, a feature request, or a question?

bug report

Have you followed the required steps before opening a bug report?

(Check the step you've followed - put an x character between the square brackets ([]).)

Have you made sure that it wasn't a React Native bug?

I can't find anything about this issue.

Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?

iOS 12.2 Android Simulator on Snack

Is the bug reproductible in a production environment (not a debug one)?

Yes.

Environment

Environment: React: 16.5.0 React native: 0.57 (Expo SDK 32.0.0) react-native-snap-carousel: 3.8.0

Target Platform: Android (5.0) iOS (9.2)

Expected Behavior

The slides looping on both sides endlessly.

Actual Behavior

The slides loop only once (regarding loopClonesPerSide) on the right side (swiping to left). On the other side, everything works as intended.

floating_error_carousel

Reproducible Demo

https://snack.expo.io/@thehacky/floating-point-error-in-carousel

Steps to Reproduce

  1. Swipe to left until no item will be rendered
ghost commented 5 years ago

+1

ghost commented 5 years ago

@TheHacky Have you found a solution?

TheHacky commented 5 years ago

@TheHacky Have you found a solution?

I used integers for all my values. Because I want to use a percentage for my size, I use Math.floor to break it down to integer pixel.

ghost commented 5 years ago

@TheHacky Sorry, I don't understand you very much. Is there any code for me to see?

TheHacky commented 5 years ago

@TheHacky Sorry, I don't understand you very much. Is there any code for me to see?

If you calculate your sizes with a percentage, e.g.

width: '50%'

or with a package like react-native-responsive-dimensions:

width: responsiveWidth(50)

then you got floating numbers. Instead, I use Math.floor to round up the value

width: Math.floor(responsiveWidth(50))
ghost commented 5 years ago

@TheHacky Sorry, I don't understand you very much. Is there any code for me to see?

If you calculate your sizes with a percentage, e.g.

width: '50%'

or with a package like react-native-responsive-dimensions:

width: responsiveWidth(50)

then you got floating numbers. Instead, I use Math.floor to round up the value

width: Math.floor(responsiveWidth(50))

This does not seem to solve the problem of sliding to the right when the loop slides, it will not loop.

TheHacky commented 5 years ago

This does not seem to solve the problem of sliding to the right when the loop slides, it will not loop.

I think you are right. It works than for iOS, but not for Android. Maybe, it refers to #529.

Foskas commented 5 years ago

Same here! Any updates on this issue ?

junhoyeo commented 4 years ago

@TheHacky Sorry, I don't understand you very much. Is there any code for me to see?

If you calculate your sizes with a percentage, e.g.

width: '50%'

or with a package like react-native-responsive-dimensions:

width: responsiveWidth(50)

then you got floating numbers. Instead, I use Math.floor to round up the value

width: Math.floor(responsiveWidth(50))

I solved this issue using this comment's solution - by providing integer values in sliderWidth and itemWidth properties in Carousel component. So my code is like this:

const carouselItemWidth = Math.floor(screenWidth * 0.68);

<Carousel
  sliderWidth={carouselItemWidth}
  itemWidth={carouselItemWidth}
  {...otherProps}
/>
dohooo commented 3 years ago

Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2