-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_keys.js
More file actions
74 lines (60 loc) · 1.47 KB
/
Copy pathcontrol_keys.js
File metadata and controls
74 lines (60 loc) · 1.47 KB
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
'use strict';
document.addEventListener('keyup', function (event) {
if (event.keyCode === 39) {
moveRight();
}
});
document.addEventListener('keyup', function (event) {
if (event.keyCode === 37) {
moveLeft();
}
});
document.addEventListener('keyup', function (event) {
if (event.keyCode === 38) {
moveTop();
}
});
document.addEventListener('keyup', function (event) {
if (event.keyCode === 40) {
moveDown();
}
});
// let field = document.querySelector('.field');
//
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
let xDown = null;
let yDown = null;
function getTouches(evt) {
return evt.touches || // browser API
evt.originalEvent.touches; // jQuery
}
function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
}
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
let xUp = evt.touches[0].clientX;
let yUp = evt.touches[0].clientY;
let xDiff = xDown - xUp;
let yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {
if ( xDiff > 0 ) {
moveLeft();
} else {
moveRight();
}
} else {
if ( yDiff > 0 ) {
moveTop();
} else {
moveDown()
}
}
xDown = null;
yDown = null;
}