-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipe-rxjs.html
79 lines (71 loc) · 2.2 KB
/
swipe-rxjs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!doctype html>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#draggable {
cursor: pointer;
position: absolute;
left: 50%;
top: 50%;
width: 100px;
height: 100px;
background: black;
}
</style>
<div id="draggable"></div>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0/dist/global/Rx.js"></script>
<script>
let moveStartsWithDirection = mousedown.concatMap(startEvent =>
mousemove.takeUntil(mouseup)
.elementAt(5)
.catch(err => Rx.Observable.empty())
.map(mousemoveEvent => {
return {
x: startEvent.x,
y: startEvent.y,
dx: mousemoveEvent.x - startEvent.x,
dy: mousemoveEvent.y - startEvent.y
};
}).filter(startEvent =>
Math.abs(startEvent.dx) < Math.abs(startEvent.dy)
).takeUntil(ends).map(endEvent => {
const x = endEvent.x - startEvent.x;
const y = endEvent.y - startEvent.y;
return {
x, y
};
});
);
// Vertical move starts: Keep only those move start events
// where the 3rd subsequent move event is rather vertical than horizontal
let verticalMoveStarts = moveStartsWithDirection;
// Horizontal move starts: Keep only those move start events
// where the 3rd subsequent move event is rather horizontal than vertical
let horizontalMoveStarts = moveStartsWithDirection.filter(dragStartEvent =>
Math.abs(dragStartEvent.intialDeltaX) >= Math.abs(dragStartEvent.initialDeltaY)
);
// Take the moves until an end occurs
const movesUntilEnds = dragStartEvent =>
moves.takeUntil(ends).map(dragEvent => {
const x = dragEvent.x - dragStartEvent.x;
const y = dragEvent.y - dragStartEvent.y;
return {x, y};
});
let verticalMoves = verticalMoveStarts.concatMap(movesUntilEnds);
let horizontalMoves = horizontalMoveStarts.concatMap(movesUntilEnds);
const lastMovesAtEnds = dragStartEvent =>
ends.first().map(dragEndEvent => {
const x = dragEndEvent.x - dragStartEvent.x;
const y = dragEndEvent.y - dragStartEvent.y;
return {x, y};
});
let verticalMoveEnds = verticalMoveStarts.concatMap(lastMovesAtEnds);
let horizontalMoveEnds = horizontalMoveStarts.concatMap(lastMovesAtEnds);
return {
verticalMoves, verticalMoveEnds,
horizontalMoves, horizontalMoveEnds
};