-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.html
243 lines (227 loc) · 7.02 KB
/
index.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JavaScript game controls</title>
<style>* { padding: 0; margin: 0; } canvas { background: #013333 url(img/background.png) no-repeat; display: block; margin: 0 auto; } #output { text-align: center; margin-top: 30px; white-space: pre-wrap } </style>
<script src="js/leap-0.6.4.min.js"></script>
</head>
<body>
<canvas id="myCanvas" width="480" height="320"></canvas>
<script>
// INIT
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var playerHeight = 110;
var playerWidth = 92;
var playerX = (canvas.width - playerWidth) / 2;
var playerY = (canvas.height - playerHeight) / 2;
var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;
var img = new Image();
img.src = "img/player.png";
// KEYBOARD
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if ("code" in e) {
switch(e.code) {
case "Unidentified":
break;
case "ArrowRight":
case "Right": // IE <= 9 and FF <= 36
case "KeyD":
rightPressed = true;
return;
case "ArrowLeft":
case "Left": // IE <= 9 and FF <= 36
case "KeyA":
leftPressed = true;
return;
case "ArrowUp":
case "Up": // IE <= 9 and FF <= 36
case "KeyW":
upPressed = true;
return;
case "ArrowDown":
case "Down": // IE <= 9 and FF <= 36
case "KeyS":
downPressed = true;
return;
default:
return;
}
}
if(e.keyCode == 39) {
rightPressed = true;
}
else if(e.keyCode == 37) {
leftPressed = true;
}
if(e.keyCode == 40) {
downPressed = true;
}
else if(e.keyCode == 38) {
upPressed = true;
}
}
function keyUpHandler(e) {
if ("code" in e) {
switch(e.code) {
case "Unidentified":
break;
case "ArrowRight":
case "Right": // IE <= 9 and FF <= 36
case "KeyD":
rightPressed = false;
return;
case "ArrowLeft":
case "Left": // IE <= 9 and FF <= 36
case "KeyA":
leftPressed = false;
return;
case "ArrowUp":
case "Up": // IE <= 9 and FF <= 36
case "KeyW":
upPressed = false;
return;
case "ArrowDown":
case "Down": // IE <= 9 and FF <= 36
case "KeyS":
downPressed = false;
return;
default:
return;
}
}
if(e.keyCode == 39) {
rightPressed = false;
}
else if(e.keyCode == 37) {
leftPressed = false;
}
if(e.keyCode == 40) {
downPressed = false;
}
else if(e.keyCode == 38) {
upPressed = false;
}
}
// MOUSE
document.addEventListener("mousemove", mouseMoveHandler);
function mouseMoveHandler(e) {
playerX = e.pageX - canvas.offsetLeft - playerWidth / 2;
playerY = e.pageY - canvas.offsetTop - playerHeight / 2;
output.textContent = `Mouse:\nx: ${playerX}, y: ${playerY}`;
}
// TOUCH
document.addEventListener("touchstart", touchHandler);
document.addEventListener("touchmove", touchHandler);
function touchHandler(e) {
if(e.touches) {
playerX = e.touches[0].pageX - canvas.offsetLeft - playerWidth / 2;
playerY = e.touches[0].pageY - canvas.offsetTop - playerHeight / 2;
output.textContent = `Touch:\nx: ${playerX}, y: ${playerY}`;
e.preventDefault();
}
}
// GAMEPAD
window.addEventListener("gamepadconnected", gamepadHandler);
var controller = {};
var buttonsPressed = [];
function gamepadHandler(e) {
controller = e.gamepad;
output.textContent = `Gamepad: ${controller.id}`;
}
function gamepadUpdateHandler() {
buttonsPressed = [];
if(controller.buttons) {
for(var b=0; b<controller.buttons.length; b++) {
if(controller.buttons[b].pressed) {
buttonsPressed.push(b);
}
}
}
}
function gamepadButtonPressedHandler(button) {
var press = false;
for(var i=0; i<buttonsPressed.length; i++) {
if(buttonsPressed[i] == button) {
press = true;
}
}
return press;
}
// LEAP MOTION
var toDegrees = 1 / (Math.PI / 180);
var horizontalDegree = 0;
var verticalDegree = 0;
var degreeThreshold = 30;
var grabStrength = 0;
Leap.loop({
hand: function(hand) {
horizontalDegree = Math.round(hand.roll() * toDegrees);
verticalDegree = Math.round(hand.pitch() * toDegrees);
grabStrength = hand.grabStrength;
output.textContent = `Leap Motion: \nroll: ${horizontalDegree}°\npitch: ${verticalDegree}°\nstrength: ${grabStrength}`;
}
});
// DRAW
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// KEYBOARD
if(rightPressed) {
playerX += 5;
}
else if(leftPressed) {
playerX -= 5;
}
if(downPressed) {
playerY += 5;
}
else if(upPressed) {
playerY -= 5;
}
// GAMEPAD
gamepadUpdateHandler();
if(gamepadButtonPressedHandler(0)) {
playerY -= 5;
}
else if(gamepadButtonPressedHandler(1)) {
playerY += 5;
}
if(gamepadButtonPressedHandler(2)) {
playerX -= 5;
}
else if(gamepadButtonPressedHandler(3)) {
playerX += 5;
}
if(gamepadButtonPressedHandler(11)) {
alert('BOOM!');
}
// LEAP MOTION
if(horizontalDegree > degreeThreshold) {
playerX -= 5;
}
else if(horizontalDegree < -degreeThreshold) {
playerX += 5;
}
if(verticalDegree > degreeThreshold) {
playerY += 5;
}
else if(verticalDegree < -degreeThreshold) {
playerY -= 5;
}
if(grabStrength == 1) {
alert('BOOM!');
}
ctx.drawImage(img, playerX, playerY);
requestAnimationFrame(draw);
}
draw();
</script>
<div id="output"></div>
</body>
</html>