-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad.js
103 lines (88 loc) · 2.92 KB
/
road.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
// road class
class Road {
//constructoir properties.
constructor(x, width, laneCount = 5) {
//lanecount default value of 5
this.x = x + 5; //x coordinate position
this.width = width - 5; //width of canvas
this.laneCount = laneCount;
this.left = (x - width / 2) + 5;
this.right = (x + width / 2) - 5;
//road go intinite upwards
const infinity = 1000000;
this.top = -infinity;
this.bottom = infinity;
/*
* Road borders
*/
const topLeft = {
x: this.left,
y: this.top
}
const bottomRight = {
x: this.right,
y: this.bottom
};
const topRight = {
x: this.right,
y: this.top
};
const bottomLeft = {
x: this.left,
y: this.bottom
};
this.borders = [
[topLeft, bottomLeft],
[topRight, bottomRight]
]
}
//get leane center point to set the car center of any specific lane.
getLineCenter(lineIndex) {
const laneWidth = this.width / this.laneCount;
return this.left + laneWidth / 2 + Math.min(lineIndex, this.laneCount - 1) * laneWidth;
}
//lane draw method
Draw() {
//loop of lane count to draw lane
for (let i = 0; i <= this.laneCount; i++) {
ctx.lineWidth = 3; //3px width
ctx.strokeStyle = "white"; //whtie color lane
/*
* lerp() a function defined bellow out of Road class
* x value depend on three things left and right position of road
* and current index of i devided by laneCount.
* this is basically make stapes of lane.
*/
const x = lerp(this.left, this.right, i / this.laneCount);
// if (i > 0 && i < this.laneCount) {
/*
* setLineDash() a function from context object which used to create line dash in canvas.
* first parameter is array of number that represent length of each dashes.
* second parameter is offset of start of pattern.
* it create line dash to lane. not include first and last lane.
*/
// ctx.setLineDash([30, 20])
// } else {
// ctx.setLineDash([]);
// }
ctx.setLineDash([20, 20])
ctx.beginPath();
ctx.moveTo(x, this.top)
ctx.lineTo(x, this.bottom)
ctx.stroke();
}
ctx.lineWidth = 4;
ctx.strokeStyle = 'green';
ctx.setLineDash([]);
this.borders.forEach(border => {
ctx.beginPath();
ctx.moveTo(border[0].x, border[0].y);
ctx.lineTo(border[1].x, border[1].y);
ctx.stroke();
})
}
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
// console.log(lerp(5, 255, 1 / 3));