easy-gestures 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +11 -0
- package/README.md +133 -0
- package/bin/main.js +15 -0
- package/example.js +34433 -0
- package/index.html +52 -0
- package/lib/constants.js +42 -0
- package/lib/customEventTypes.js +74 -0
- package/lib/example/preamble.js +10 -0
- package/lib/example/view.js +233 -0
- package/lib/example.js +19 -0
- package/lib/index.js +18 -0
- package/lib/mixins/touch.js +477 -0
- package/lib/position/relative.js +97 -0
- package/lib/position.js +101 -0
- package/lib/preamble.js +10 -0
- package/lib/selectors.js +26 -0
- package/lib/utilities/positions.js +143 -0
- package/license.txt +48 -0
- package/package.json +38 -0
- package/src/constants.js +10 -0
- package/src/customEventTypes.js +17 -0
- package/src/example/preamble.js +7 -0
- package/src/example/view.js +85 -0
- package/src/example.js +22 -0
- package/src/index.js +3 -0
- package/src/mixins/touch.js +754 -0
- package/src/position/relative.js +63 -0
- package/src/position.js +74 -0
- package/src/preamble.js +7 -0
- package/src/selectors.js +5 -0
- package/src/utilities/positions.js +127 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { PI } from "../constants";
|
|
4
|
+
|
|
5
|
+
export default class RelativePosition {
|
|
6
|
+
constructor(top, left, time, speed, magnitude, direction) {
|
|
7
|
+
this.top = top;
|
|
8
|
+
this.left = left;
|
|
9
|
+
this.time = time;
|
|
10
|
+
this.speed = speed;
|
|
11
|
+
this.magnitude = magnitude;
|
|
12
|
+
this.direction = direction;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getTop() {
|
|
16
|
+
return this.top;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getLeft() {
|
|
20
|
+
return this.left;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getTime() {
|
|
24
|
+
return this.time;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getSpeed() {
|
|
28
|
+
return this.speed;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getMagnitude() {
|
|
32
|
+
return this.magnitude;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getDirection() {
|
|
36
|
+
return this.direction;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static fromFirstPositionAndSecondPosition(firstPosition, secondPosition) {
|
|
40
|
+
const position = secondPosition.minus(firstPosition),
|
|
41
|
+
top = position.getTop(),
|
|
42
|
+
left = position.getLeft(),
|
|
43
|
+
time = position.getTime(),
|
|
44
|
+
magnitude = Math.sqrt(top * top + left * left),
|
|
45
|
+
speed = (time === 0) ?
|
|
46
|
+
0 :
|
|
47
|
+
magnitude / time;
|
|
48
|
+
|
|
49
|
+
let direction;
|
|
50
|
+
|
|
51
|
+
if (left === 0) {
|
|
52
|
+
direction = (top < 0) ?
|
|
53
|
+
+PI / 2 :
|
|
54
|
+
-PI / 2;
|
|
55
|
+
} else {
|
|
56
|
+
direction = Math.atan2(-top, left);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const relativePosition = new RelativePosition(top, left, time, speed, magnitude, direction);
|
|
60
|
+
|
|
61
|
+
return relativePosition;
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/position.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
export default class Position {
|
|
4
|
+
constructor(top, left, time, identifier) {
|
|
5
|
+
this.top = top;
|
|
6
|
+
this.left = left;
|
|
7
|
+
this.time = time;
|
|
8
|
+
this.identifier = identifier;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
getTop() {
|
|
12
|
+
return this.top;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getLeft() {
|
|
16
|
+
return this.left;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getTime() {
|
|
20
|
+
return this.time;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getIdentifier() {
|
|
24
|
+
return this.identifier;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
minus(position) {
|
|
28
|
+
const positionTop = position.getTop(),
|
|
29
|
+
positionLeft = position.getLeft(),
|
|
30
|
+
positionTime = position.getTime(),
|
|
31
|
+
top = this.top - positionTop,
|
|
32
|
+
left = this.left - positionLeft,
|
|
33
|
+
time = this.time - positionTime,
|
|
34
|
+
identifier = this.identifier;
|
|
35
|
+
|
|
36
|
+
position = new Position(top, left, time, identifier); ///
|
|
37
|
+
|
|
38
|
+
return position;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
match(position) {
|
|
42
|
+
const identifier = position.getIdentifier(),
|
|
43
|
+
matches = (this.identifier === identifier);
|
|
44
|
+
|
|
45
|
+
return matches;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static fromTouch(touch) {
|
|
49
|
+
const { pageX, pageY, identifier } = touch,
|
|
50
|
+
top = pageY, ///
|
|
51
|
+
left = pageX, ///
|
|
52
|
+
time = getTime(),
|
|
53
|
+
position = new Position(top, left, time, identifier);
|
|
54
|
+
|
|
55
|
+
return position;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static fromMouseEvent(mouseEvent) {
|
|
59
|
+
const { pageX, pageY } = mouseEvent,
|
|
60
|
+
top = pageY, ///
|
|
61
|
+
left = pageX, ///
|
|
62
|
+
time = getTime(),
|
|
63
|
+
identifier = null,
|
|
64
|
+
position = new Position(top, left, time, identifier);
|
|
65
|
+
|
|
66
|
+
return position;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getTime() {
|
|
71
|
+
const time = Date.now(); ///
|
|
72
|
+
|
|
73
|
+
return time;
|
|
74
|
+
}
|
package/src/preamble.js
ADDED
package/src/selectors.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { arrayUtilities } from "necessary";
|
|
4
|
+
|
|
5
|
+
import Position from "../position";
|
|
6
|
+
|
|
7
|
+
const { clear, filter } = arrayUtilities;
|
|
8
|
+
|
|
9
|
+
export function sortPositions(positionsA, positionsB) {
|
|
10
|
+
const positionAMap = positionsA.reduce((positionAMap, positionA) => {
|
|
11
|
+
const identifier = positionA.getIdentifier();
|
|
12
|
+
|
|
13
|
+
positionAMap[identifier] = positionA;
|
|
14
|
+
|
|
15
|
+
return positionAMap;
|
|
16
|
+
}, {});
|
|
17
|
+
|
|
18
|
+
clear(positionsA);
|
|
19
|
+
|
|
20
|
+
positionsB.forEach((positionB) => {
|
|
21
|
+
const identifier = positionB.getIdentifier(),
|
|
22
|
+
positionA = positionAMap[identifier];
|
|
23
|
+
|
|
24
|
+
positionsA.push(positionA);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function matchPositions(positionsA, positionsB) {
|
|
29
|
+
let positionsMatch = false;
|
|
30
|
+
|
|
31
|
+
const positionsALength = positionsA.length,
|
|
32
|
+
positionsBLength = positionsB.length;
|
|
33
|
+
|
|
34
|
+
if (positionsALength === positionsBLength) {
|
|
35
|
+
const identifiersA = identifiersFromPositions(positionsA),
|
|
36
|
+
identifiersB = identifiersFromPositions(positionsB);
|
|
37
|
+
|
|
38
|
+
identifiersA.sort();
|
|
39
|
+
|
|
40
|
+
identifiersB.sort();
|
|
41
|
+
|
|
42
|
+
const identifiersMatch = identifiersA.every((identifierA, index) => {
|
|
43
|
+
const identifierB = identifiersB[index];
|
|
44
|
+
|
|
45
|
+
if (identifierA === identifierB) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
positionsMatch = identifiersMatch; ///
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return positionsMatch;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function filterPositions(positionsA, positionsB) {
|
|
57
|
+
filter(positionsA, (positionA) => {
|
|
58
|
+
const matches = positionsB.some((positionB) => {
|
|
59
|
+
const matches = positionA.match(positionB);
|
|
60
|
+
|
|
61
|
+
if (matches) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (!matches) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function positionsFromMouseEvent(mouseEvent) {
|
|
73
|
+
const position = Position.fromMouseEvent(mouseEvent),
|
|
74
|
+
positions = [
|
|
75
|
+
position
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
return positions;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function positionsFromTouchEvent(touchEvent, changed = true) {
|
|
82
|
+
let touchesNodeList;
|
|
83
|
+
|
|
84
|
+
if (changed) {
|
|
85
|
+
({ changedTouches: touchesNodeList } = touchEvent);
|
|
86
|
+
} else {
|
|
87
|
+
({ touches: touchesNodeList } = touchEvent);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const touches = [
|
|
91
|
+
...touchesNodeList
|
|
92
|
+
],
|
|
93
|
+
positions = touches.map((touch) => {
|
|
94
|
+
const position = Position.fromTouch(touch);
|
|
95
|
+
|
|
96
|
+
return position;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
compressPositions(positions);
|
|
100
|
+
|
|
101
|
+
return positions;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function compressPositions(positions) {
|
|
105
|
+
const positionMap = positions.reduce((positionMap, position) => {
|
|
106
|
+
const identifier = position.getIdentifier();
|
|
107
|
+
|
|
108
|
+
positionMap[identifier] = position;
|
|
109
|
+
|
|
110
|
+
return positionMap;
|
|
111
|
+
}, {});
|
|
112
|
+
|
|
113
|
+
positions = Object.values(positionMap);
|
|
114
|
+
|
|
115
|
+
return positions;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function identifiersFromPositions(positions) {
|
|
119
|
+
const identifiers = positions.map((position) => {
|
|
120
|
+
const identifier = position.getIdentifier();
|
|
121
|
+
|
|
122
|
+
return identifier;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return identifiers;
|
|
126
|
+
}
|
|
127
|
+
|