-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
69 lines (54 loc) · 2.31 KB
/
index.html
File metadata and controls
69 lines (54 loc) · 2.31 KB
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>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script src="sprite.js"></script>
</head>
<body>
<canvas id="mycanvas" style="border: 5px solid #000000; display: block; margin: auto"></canvas>
<script>
const canvas = document.querySelector("canvas");
const sprites_to_draw = [[], []]; //background and foreground sprites
let draw_loop_timeout;
$(document).ready(function() {
console.log("Page is now ready");
resize();
$.getJSON("Penguins/animationData.json", function(data) {
sprites_to_draw[1].push(new Sprite(data, 500, 100, "idleWave"));
});
draw_loop_timeout = setInterval(draw_loop, 50);
window.addEventListener('resize', resize);
//for arrow keys
window.addEventListener('keydown', function(event) {
var keyCode = event.keyCode;
const sprite = sprites_to_draw[1][0];
if (keyCode === 37) { //left arrow
sprite.moveLeft();
} else if (keyCode === 39) { //right arrow
sprite.moveRight();
} else if (keyCode === 38) { //up arrow
sprite.moveUp();
} else if (keyCode === 40) { //down arrow
sprite.moveDown();
}
});
});
function draw_loop() {
const context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
//do background here so it redraws the bg first and only the bg
const backgroundImage = new Image();
backgroundImage.src = 'beachBG.jpg';
context.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
//draw foreground sprites
for (let i = 0; i < sprites_to_draw[1].length; i++) {
sprites_to_draw[1][i].draw();
}
}
function resize() {
canvas.width = window.innerWidth - 25;
canvas.height = window.innerHeight - 25;
}
</script>
</body>
</html>