Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit c03b0c7

Browse files
osdnksatya164
andauthored
feat: consider both velocity and position while calculating the next position (#146)
Co-Authored-By: Satyajit Sahoo <satyajit.happy@gmail.com>
1 parent 6d0fcad commit c03b0c7

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/views/Stack/Card.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ const UNSET = -1;
5757
const DIRECTION_VERTICAL = -1;
5858
const DIRECTION_HORIZONTAL = 1;
5959

60-
const SWIPE_VELOCITY_THRESHOLD_DEFAULT = 500;
61-
const SWIPE_DISTANCE_THRESHOLD_DEFAULT = 60;
62-
63-
const SWIPE_DISTANCE_MINIMUM = 5;
60+
const SWIPE_VELOCITY_IMPACT = 0.01;
6461

6562
/**
6663
* The distance of touch start from the edge of the screen where the gesture will be recognized
@@ -70,7 +67,7 @@ const GESTURE_RESPONSE_DISTANCE_VERTICAL = 135;
7067

7168
const {
7269
abs,
73-
and,
70+
add,
7471
block,
7572
call,
7673
cond,
@@ -83,7 +80,6 @@ const {
8380
multiply,
8481
neq,
8582
onChange,
86-
or,
8783
set,
8884
spring,
8985
sub,
@@ -245,6 +241,21 @@ export default class Card extends React.Component<Props> {
245241
]);
246242
};
247243

244+
private velocitySignum = cond(
245+
this.velocity,
246+
divide(abs(this.velocity), this.velocity),
247+
0
248+
);
249+
private extrapolatedPosition = add(
250+
this.gesture,
251+
multiply(
252+
this.velocity,
253+
this.velocity,
254+
this.velocitySignum,
255+
SWIPE_VELOCITY_IMPACT
256+
)
257+
);
258+
248259
private exec = block([
249260
onChange(
250261
this.isClosing,
@@ -335,22 +346,9 @@ export default class Card extends React.Component<Props> {
335346
set(this.isSwiping, FALSE),
336347
this.runTransition(
337348
cond(
338-
or(
339-
and(
340-
greaterThan(abs(this.gesture), SWIPE_DISTANCE_MINIMUM),
341-
greaterThan(
342-
abs(this.velocity),
343-
SWIPE_VELOCITY_THRESHOLD_DEFAULT
344-
)
345-
),
346-
cond(
347-
greaterThan(
348-
abs(this.gesture),
349-
SWIPE_DISTANCE_THRESHOLD_DEFAULT
350-
),
351-
TRUE,
352-
FALSE
353-
)
349+
greaterThan(
350+
abs(this.extrapolatedPosition),
351+
divide(this.distance, 2)
354352
),
355353
cond(
356354
lessThan(

0 commit comments

Comments
 (0)