-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.js
60 lines (49 loc) · 848 Bytes
/
keys.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
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var mouseDown = false;
var width = 65;
var height = 65;
var x = (canvas.width - width) / 2;
var y = (canvas.height - height) / 2;
function animate() {
if (isHoldingKey('d')) {
x += 1;
}
redraw();
setTimeout(animate, 10);
}
setTimeout(animate, 10)
key('w', function() {
y -= 10;
redraw();
});
key('a', function() {
x -= 10;
redraw();
});
key('s', function() {
y += 10;
redraw();
});
key('up', function() {
height -= 10;
redraw();
});
key('left', function() {
width -= 10;
redraw();
});
key('down', function() {
height += 10;
redraw();
});
key('right', function() {
width += 10;
redraw();
});
function redraw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'red';
ctx.fillRect(x, y, width, height);
}
redraw();