-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas.js
142 lines (124 loc) · 2.89 KB
/
canvas.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { createApp } from "vue";
const eventBus = createApp({});
export function createCanvasApp(
// Get needed data from Space.vue
myPlayerId,
users,
canvas,
canvasImage,
characterImage
) {
const ctx = canvas.getContext("2d");
// Set width and height of canvas
canvas.width = 1024;
canvas.height = 768;
// Draw background image
const worldImg = new Image();
worldImg.src = canvasImage;
// Draw character
const characterImg = new Image();
characterImg.src = characterImage;
// Constants
const SPEED = 5;
// Key states
const keys = {
w: {
pressed: false,
},
a: {
pressed: false,
},
s: {
pressed: false,
},
d: {
pressed: false,
},
};
// Animate canvas
function animate() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Get my player(the current user)
const myPlayer = users.find((user) => user.id === myPlayerId);
// Center camera on player
let cameraX = 0;
let cameraY = 0;
if (myPlayer) {
// Prevent tearing
cameraX = parseInt(myPlayer.x - canvas.width / 2);
cameraY = parseInt(myPlayer.y - canvas.height / 2);
}
// Draw background so it covers all canvas and moves with camera
ctx.drawImage(
worldImg,
0,
0,
worldImg.width,
worldImg.height,
-cameraX,
-cameraY,
worldImg.width,
worldImg.height
);
// Draw users on center
users.forEach((user) => {
ctx.drawImage(
characterImg,
0,
0,
characterImg.width / 4,
characterImg.height,
canvas.width / 2 - characterImg.width / 8,
canvas.height / 2 - characterImg.height / 8,
characterImg.width / 4,
characterImg.height
);
});
// Request next frame
setTimeout(() => {
requestAnimationFrame(animate);
}, 100);
}
// Start drawing
animate();
// Key presses
window.addEventListener("keydown", (e) => {
switch (e.key) {
case "w":
keys.w.pressed = true;
break;
case "a":
keys.a.pressed = true;
break;
case "s":
keys.s.pressed = true;
break;
case "d":
keys.d.pressed = true;
break;
default:
break;
}
});
// Key releases
window.addEventListener("keyup", (e) => {
switch (e.key) {
case "w":
keys.w.pressed = false;
break;
case "a":
keys.a.pressed = false;
break;
case "s":
keys.s.pressed = false;
break;
case "d":
keys.d.pressed = false;
break;
default:
break;
}
});
}
//ctx.drawImage(image, sx(top-left of x), sy(top-left of y), swidth(width portion to draw), sheight(height portion to draw), x(x to draw on), y(y to draw on), width(width of image to draw on canvas), height(height of image to draw on canvas))