-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyInput.js
117 lines (110 loc) · 4.1 KB
/
myInput.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class Input{
constructor(){
this.keys = [];
this.lastKey = "";
this.touchX = 0;
this.touchY = 0;
this.touchThreshold = 50;
//developer
//button
this.keypressed = false;
this.buttons = document.querySelectorAll("button");
this.buttons.forEach(btn=>{
btn.addEventListener('touchstart', (e)=>{
if(this.keys.indexOf(e.target.id) === -1){
this.keys.unshift(e.target.id);
this.lastKey = e.target.id;
this.keypressed = true;
}
});
btn.addEventListener ('touchmove', (e)=>{
if(this.keys.indexOf(e.target.id) > -1){
this.keys.splice(this.keys.indexOf(e.target.id), 1);
this.lastKey = "";
this.keypressed = false;
}
})
btn.addEventListener ('touchend', (e)=>{
if(this.keys.indexOf(e.target.id) > -1){
this.keys.splice(this.keys.indexOf(e.target.id), 1);
this.lastKey = "";
this.keypressed = false;
}
})
btn.addEventListener('mousedown', (e)=>{
if(this.keys.indexOf(e.target.id) === -1){
this.keys.unshift(e.target.id);
this.lastKey = e.target.id;
this.keypressed = true;
}
});
btn.addEventListener ('mouseup', (e)=>{
if(this.keys.indexOf(e.target.id) > -1){
this.keys.splice(this.keys.indexOf(e.target.id), 1);
this.lastKey = "";
this.keypressed = false;
}
})
})
//arrows
window.addEventListener('keydown', (e)=>{
if(this.keys.indexOf(e.key) === -1){
this.keys.unshift(e.key);
this.lastKey = e.key;
this.keypressed = true;
}
})
window.addEventListener('keyup', (e)=>{
if(this.keys.indexOf(e.key) > -1){
this.keys.splice(this.keys.indexOf(e.key), 1);
this.lastKey = "";
this.keypressed = false;
}
})
//swipe
window.addEventListener("touchstart", (e)=>{
this.touchX = e.touches[0].clientX;
this.touchY = e.touches[0].clientY;
})
window.addEventListener("touchmove", (e)=>{
const dx = e.touches[0].clientX - this.touchX;
const dy = e.touches[0].clientY - this.touchY;
if(dx < -this.touchThreshold){
this.lastKey = "swipe left";
if(this.keys.indexOf(this.lastKey) === -1){
this.keys.unshift(this.lastKey);
this.keypressed = true;
}
}
else if(dx > this.touchThreshold){
this.lastKey = "swipe right";
if(this.keys.indexOf(this.lastKey) === -1){
this.keys.unshift(this.lastKey);
this.keypressed = true;
}
}
else if(dy < -this.touchThreshold){
this.lastKey = "swipe up";
if (this.keys.indexOf(this.lastKey) === -1) {
this.keys.unshift(this.lastKey);
this.keypressed = true;
}
}
else if(dy > this.touchThreshold){
this.lastKey = "swipe down";
if (this.keys.indexOf(this.lastKey) === -1) {
this.keys.unshift(this.lastKey);
this.keypressed = true;
}
}
})
window.addEventListener("touchend", (e) => {
if(this.keys.indexOf(this.lastKey) > -1){
this.keys.splice(this.keys.indexOf(this.lastKey), 1);
this.lastKey = "";
this.keypressed = false;
}
})
}
}
export const myInput = new Input();