-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_event.html
69 lines (60 loc) · 1.32 KB
/
keyboard_event.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
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>Skookum Labs - http://labs.skookum.com/demos/barcampclt_physics/</title>
</head>
<body>
<canvas id="surface" width="600" height="400"></canvas>
<script>
// Version 0: Position
var x = 150; var y = 100; var r = 15; var dx = 5; var dy = 5;
var WIDTH = 800; var HEIGHT = 600;
var canvas = document.getElementById("surface");
var ctx = canvas.getContext('2d');
function draw() {
// Clear display
ctx.save();
ctx.fillStyle = "rgba(0, 255, 255, 1)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
// Draw rectangle
ctx.save();
ctx.fillStyle = "white"; ctx.strokeStyle = "black";
ctx.beginPath();
ctx.rect(100,200,200,100);
ctx.closePath();
ctx.fill();
ctx.stroke();
//draw ball
ctx.fillStyle = "purple"; ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, true);
ctx.fill();
}
//draw();
setInterval(draw, 10);
window.addEventListener('keydown',doKeyDown,true);
function doKeyDown(evt){
switch (evt.keyCode) {
case 38: /* Up arrow was pressed */
if (y - dy > 0){
y -= dy;
}
break;
case 40: /* Down arrow was pressed */
if (y + dy < HEIGHT){
y += dy;
}
break;
case 37: /* Left arrow was pressed */
if (x - dx > 0){
x -= dx;
}
break;
case 39: /* Right arrow was pressed */
if (x + dx < WIDTH){
x += dx;
}
break;
}
}
</script> </body></html>