-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
40 lines (32 loc) · 887 Bytes
/
background.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
const imageLayers = [];
for (let i = 1; i <= 6; i++) {
imageLayers.push(`./images/background-layers/${i}.png`);
}
const backgroundLayers = imageLayers.map((url) => {
const image = new Image();
image.src = url;
return image;
});
class Background {
constructor(game) {
this.game = game;
this.x = 0;
this.speed = 2;
}
runLogic() {
this.x += this.speed;
}
paint() {
const context = this.game.context;
const $canvas = context.canvas;
const width = $canvas.width * 2;
const height = $canvas.height;
const distance = this.game.player.x + this.x;
for (let i = 0; i < backgroundLayers.length; i++) {
const layer = backgroundLayers[i];
const outset = ((distance * i) / 6) % width;
context.drawImage(layer, -outset, 0, width, height);
context.drawImage(layer, -outset + width, 0, width, height);
}
}
}