-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.js
67 lines (41 loc) · 1.35 KB
/
camera.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
var Racer = Racer || {};
Racer.camera = {
// NOTE: There is no actual camera, objects are just being moved around
// to simulate a camera is following the car
centerCar: function() {
var moveX=0, move=0;
// Get the position of the car
var currentX = Racer.car.config.posX;
var currentY = Racer.car.config.posY;
var canvas = Racer.canvas.layer.car.el;
// Set the center point of the canvas
var borderOffsetX = canvas.width/2;
var borderOffsetY = canvas.height/2;
// The speed of the camera
var smoothness = 0.07;
// Calculate the X positions
if(currentX < borderOffsetX) {
moveX = (borderOffsetX-currentX)*smoothness;
} else if (currentX > canvas.width-borderOffsetX) {
moveX = -(currentX-(canvas.width-borderOffsetX))*smoothness;
}
// Calculate the Y positions
if(currentY < borderOffsetY) {
moveY = (borderOffsetY-currentY)*smoothness;
} else if (currentY > canvas.height-borderOffsetY) {
moveY = -(currentY-(canvas.height-borderOffsetY))*smoothness;
}
if(moveX !== 0) {
// move the car back to the center
Racer.car.config.posX += moveX;
// Move the track with it
Racer.track.config.posX += moveX;
}
if(moveY !== 0) {
// move the car back to the center
Racer.car.config.posY += moveY;
// Move the track with it
Racer.track.config.posY += moveY;
}
}
};