-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgesture.js
83 lines (63 loc) · 2.15 KB
/
gesture.js
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
80
81
82
83
let touchstartX = []
let touchendX = []
let touchstartY = []
let touchendY = []
let touchNum = 0
const threshold = 20
// Chat
function addSwipe() {
const boardElement = document.getElementById('board')
function handleGesture() {
const tix = touchstartX[0], tiy = touchstartY[0]; // touch-initial
const tfx = touchendX[0], tfy = touchendY[0]; // touch-final
if(touchNum == 2) {
board.rotate();
touchNum = 0;
return;
}
if(abs(movement.y) == 0 && abs(tfx-tix) > abs(tfy-tiy)){
if (tfx < tix-threshold) movement.x = -1
if (tfx > tix+threshold) movement.x = 1
}
movement.y = swipingVertDir(tix, tiy, tfx, tfy);
}
boardElement.addEventListener('touchstart', e => {
touchstartX = mapList(e.changedTouches, t => t.screenX);
touchstartY = mapList(e.changedTouches, t => t.screenY);
touchNum = e.touches.length;
})
boardElement.addEventListener('touchend', e => {
touchendX = mapList(e.changedTouches, t => t.screenX);
touchendY = mapList(e.changedTouches, t => t.screenY);
if(touchNum > 0) handleGesture();
})
boardElement.style.setProperty("overscroll-behavior", "contain");
}
function swipingVertDir(tix, tiy, tfx, tfy) {
if(abs(movement.x) == 0 && abs(tfx-tix) < abs(tfy-tiy)){
if (tfy < tiy-threshold) return -1
if (tfy > tiy+threshold) return 1
}
return 0;
}
function mapList(list, f) {
const newArr = [];
for(let i = 0; i < list.length; i ++) {
newArr[i] = f(list[i]);
}
return newArr;
}
// Stops scrolling with arrow keys
window.addEventListener("keydown", function(e) {
if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
e.preventDefault();
}
}, false);
// Stops scrolling with swipes on canvas
document.addEventListener('DOMContentLoaded', stop_move)
function stop_move() {
board_div = document.querySelector('#board');
// Ill move this to gesture.js where it will be in scope. May I? Yes also it works! Never mind then. lets go
board_div.addEventListener('touchstart', (e) => e.preventDefault(), false);
board_div.addEventListener('touchmove', (e) => e.preventDefault(), false);
}