-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
72 lines (62 loc) · 2.14 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Canvas Sprite Animation</title>
<script type="text/javascript" src="js/Sprite.js"></script>
<script type="text/javascript" src="js/Sonic.js"></script>
<script type="text/javascript" src="js/Coin.js"></script>
</head>
<body>
<canvas id="gamecanvas" width="400" height="200" style="border: 1px solid black; "></canvas>
<div>
<button type="button" onclick="game.sonic.walk()">Walk</button>
<button type="button" onclick="game.sonic.run()">Run</button>
<button type="button" onclick="game.sonic.idle()">Idle</button>
</div>
<script>
const game = {
isRunning: true,
init() {
game.canvas = document.getElementById("gamecanvas");
game.context = game.canvas.getContext("2d");
game.loader = loader;
game.loader.init();
this.sonic = new Sonic(80, 50, game.context, loader.images.sonic);
this.coin = new Coin(300, 20, game.context, loader.images.coin);
// Start game
game.drawingLoop();
},
drawingLoop() {
// Clear canvas
game.context.clearRect(0, 0, game.canvas.width, game.canvas.height);
// Draw and update frame index
game.sonic.render();
game.sonic.update();
game.coin.render();
game.coin.update();
if (game.isRunning) {
requestAnimationFrame(game.drawingLoop);
}
},
};
const loader = {
count: 0,
images: {},
add(title, src) {
const image = new Image();
image.src = src;
this.images[title] = image;
this.count++;
},
init() {
loader.add('sonic', Sonic.src);
loader.add('coin', Coin.src);
}
};
window.addEventListener("load", () => {
game.init();
});
</script>
</body>
</html>