Skip to content

Commit

Permalink
fix(👋🏻): Minor improvement in preserveOffset (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Jul 12, 2019
1 parent e718431 commit 50d8a99
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Gesture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Animated from "react-native-reanimated";
import {
State as GestureState,
State,
PanGestureHandlerEventExtra,
GestureHandlerStateChangeNativeEvent,
PinchGestureHandlerEventExtra,
Expand Down Expand Up @@ -32,24 +32,23 @@ const {

export const preserveOffset = (
value: Animated.Adaptable<number>,
state: Animated.Adaptable<GestureState>
state: Animated.Adaptable<State>,
offset: Animated.Value<number> = new Value(0)
) => {
const previous = new Value(0);
const offset = new Value(0);

return block([
cond(
eq(state, GestureState.BEGAN),
[set(previous, 0)],
[set(offset, add(offset, sub(value, previous))), set(previous, value)]
eq(state, State.ACTIVE),
[set(offset, add(offset, sub(value, previous))), set(previous, value)],
[set(previous, 0)]
),
offset
]);
};

export const decay = (
value: Animated.Adaptable<number>,
state: Animated.Adaptable<GestureState>,
state: Animated.Adaptable<State>,
velocity: Animated.Adaptable<number>
) => {
const decayedValue = new Value(0);
Expand All @@ -59,7 +58,7 @@ export const decay = (

return block([
cond(
eq(state, GestureState.END),
eq(state, State.END),
[
set(
decayedValue,
Expand All @@ -68,7 +67,7 @@ export const decay = (
],
[
stopClock(clock),
cond(eq(state, GestureState.BEGAN), [
cond(eq(state, State.BEGAN), [
set(rerunDecaying, 0),
set(offset, sub(decayedValue, value))
]),
Expand All @@ -81,7 +80,7 @@ export const decay = (

export const spring = (
translation: Animated.Value<number>,
state: Animated.Value<GestureState>,
state: Animated.Value<State>,
snapPoint: Animated.Adaptable<number>,
defaultOffset: number = 0,
springConfig?: Animated.SpringConfig
Expand All @@ -102,7 +101,7 @@ export const spring = (
};
return block([
cond(
eq(state, GestureState.END),
eq(state, State.END),
[
set(
springedValue,
Expand All @@ -111,7 +110,7 @@ export const spring = (
],
[
stopClock(clock),
cond(eq(state, GestureState.BEGAN), [
cond(eq(state, State.BEGAN), [
set(rerunSpring, 0),
set(offset, sub(springedValue, translation))
]),
Expand All @@ -124,15 +123,15 @@ export const spring = (

export const limit = (
value: Animated.Adaptable<number>,
state: Animated.Adaptable<GestureState>,
state: Animated.Adaptable<State>,
min: Animated.Adaptable<number>,
max: Animated.Adaptable<number>
) => {
const offset = new Animated.Value(0);
const offsetValue = add(offset, value);

return block([
cond(eq(state, GestureState.BEGAN), [
cond(eq(state, State.BEGAN), [
cond(lessThan(offsetValue, min), set(offset, sub(min, value))),
cond(greaterThan(offsetValue, max), set(offset, sub(max, value)))
]),
Expand All @@ -153,7 +152,7 @@ export const preserveMultiplicativeOffset = (

return block([
cond(
eq(state, GestureState.BEGAN),
eq(state, State.BEGAN),
[set(previous, 1)],
[
set(offset, multiply(offset, divide(value, previous))),
Expand Down

0 comments on commit 50d8a99

Please sign in to comment.