-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[Animated] Implement modulo operator method #5743
Conversation
By analyzing the blame information on this pull request, we identified @sahrens, @lelandrichardson and @ide to be potential reviewers. |
I was just thinking about ways to do this the other day.
Similar to |
@lelandrichardson I like that api a lot better, but I could not figure out how to make it work since the underlying Animation (spring/timing/decay) has already chosen the direction to go. I suppose it could work as an state = {
rotation: Animated.value(0)
}
// ...
let degrees = Math.random()*360
if (lastDegreeValue - degrees > 180) degrees += 360
if (lastDegreeValue - degrees < -180) degrees -= 360
Animated.timing(this.state.rotation, {toValue: degrees}).start()
// ...
<Thingy style={{transform: [{rotate: Animated.modulo(this.state.rotation, 360)]}} /> |
@rt2zz right. I think this is also better since it doesn't depend on using the |
I love the purity of the Because modulo in js is already messed up ( |
@rt2zz any modulo range could be expressed with a combination of |
True but as a consumer you would then always have to map your raw input values to a (0, n) or (-n, n) range only to interpolate them back. Maybe @sahrens or others can weigh in on api design - I am ok either way but prefer having an explicit range. |
That's fair. A range would be very easy to do regardless....
versus
|
@@ -145,6 +145,7 @@ type TimingAnimationConfigSingle = AnimationConfig & { | |||
easing?: (value: number) => number; | |||
duration?: number; | |||
delay?: number; | |||
wrap?: array; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
identifier array
Could not resolve name
Note: I will be completely redoing this next week per @lelandrichardson suggestion |
@rt2zz updated the pull request. |
Ok I updated the PR per leland's recommendation. This now adds a Note: ecmascript thinks |
@rt2zz is the Edit: nvm, that doesn't change anything. |
@lelandrichardson ya I guess I should have been more explicit i.e. |
@rt2zz updated the pull request. |
I just rebased master. The diff is pretty trivial and this functionality cannot be achieved through any other means. |
@facebook-github-bot shipit |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
ae0ad1f
Summary:This is an initial take on how to allow for value wrapping in Animated (currently `.timing` only). It adds a wrap config options which is an array specifying a range of values to be wrapped around. Anytime the delta of `toValue and fromValue > wrapDistance / 2` instead of animating the long way it will animate towards the range extremity and then after breaching the range it will readjust to animated to the true toValue. Example Usage: ```js Animated.timing( this.state.animatedDegrees, {toValue: Math.random()*360, wrap: [0, 360]} ).start() ``` This is presumably very valuable in rotation based animations, but could also be useful in other applications like wrapping the edges of the viewport. Questions & Todo: - Is `wrap` as a config key semantically meaningful and accurate? - Is there a way to expose this as a config option on `.interpolate` rather than timing - Generalize to all animated API's (spring, decay), and will this work with ValueXY out of the box? - Add tests Closes facebook#5743 Differential Revision: D2979992 fb-gh-sync-id: 69be510feba8c43acb10c253036f5854adff9258 shipit-source-id: 69be510feba8c43acb10c253036f5854adff9258
-a % b should be a negative number because that's how js behaves. In my case, I have a swipeX Animated Value and it can be + or -. When I use modulo on swipeX, I lose which direction swipeX is going in. Now I guess I need to have another swipeDirection variable which is either 1 or -1 and multiply it by the result of modulo to make it the value it should be if it behaved like js %. |
This is an initial take on how to allow for value wrapping in Animated (currently
.timing
only). It adds a wrap config options which is an array specifying a range of values to be wrapped around. Anytime the delta oftoValue and fromValue > wrapDistance / 2
instead of animating the long way it will animate towards the range extremity and then after breaching the range it will readjust to animated to the true toValue.Example Usage:
This is presumably very valuable in rotation based animations, but could also be useful in other applications like wrapping the edges of the viewport.
Questions & Todo:
wrap
as a config key semantically meaningful and accurate?.interpolate
rather than timing