-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackground.js
40 lines (30 loc) · 1.27 KB
/
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
class Background {
constructor(canvas, image) {
this._canvas = canvas;
this._image = image;
this._sx = 10;
this._x = this._sx;
this._y = (canvas.height / 100) * 90;
this._cloudX = 0;
this._dinoWidth = 86;
this._dinoHeight = 95;
}
draw() {
var width = this._canvas.canvas.width, height = this._canvas.canvas.height;
this._x++;
if(this._x == this._image.width) {
this._x = this._sx;
}
if(this._cloudX < 1) {
this._cloudX = width;
}
this._cloudX -= 0.5;
this._canvas.context.drawImage(this._image, 170, 0, this._dinoWidth, this._dinoHeight, this._cloudX, ((height / 100) * 10), this._dinoWidth, this._dinoHeight);
if(this._x + width > this._image.width) {
var rest = this._image.width - this._x;
this._canvas.context.drawImage(this._image, this._sx, this._image.height - 26, width - rest, 26, rest - 2, this._y, width - (rest - 2), 26);
}
// sx, sy, sw, sh, x, y, w, h
this._canvas.context.drawImage(this._image, this._x, this._image.height - 26, width, 26, 0, this._y, width, 26);
}
}