-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
60 lines (55 loc) · 1.27 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
<!DOCTYPE>
<html>
<head>
<title>JS Project Demo</title>
</head>
<body>
<style>
canvas {
border: 10px solid red;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", () => {
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const game = {
startX: Math.floor(Math.random() * canvas.clientWidth),
startY: Math.floor(Math.random() * canvas.clientHeight),
ctx,
draw: function() {
this.ctx.fillRect(this.startX, this.startY, 10, 10)
}
}
function step() {
game.draw()
requestAnimationFrame(step)
}
document.addEventListener("keydown", (e) => {
game.ctx.fillStyle = randColor()
switch (e.keyCode) {
case 38:
game.startY -= 2
break
case 37:
game.startX -= 2
break
case 40:
game.startY += 2
break
case 39:
game.startX += 2
break
default:
console.log('this is not the key you are looking for')
}
})
step()
})
function randColor() {
return `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`
}
</script>
<canvas height="400" width="400"></canvas>
</body>
</html>