-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic backup.html
173 lines (156 loc) · 5.76 KB
/
traffic backup.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Traffic signal</title>
<style>
body{
overflow: hidden;
background: rgb(48, 47, 47);
}
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.traffic-div{
height:100vh;
width: 100%;
display: flex;
align-content: center;
justify-content:center
}
#traffic-canvas{
height:100vh;
}
</style>
</head>
<body>
<div class="traffic-div">
<canvas id="traffic-canvas" width="600" height="400"></canvas>
</div>
<script>
let Red_time=2;
let Green_time=2;
let Yellow_time = 1;
let cycle_lenth = Red_time+Green_time+Yellow_time;
const canvas = document.getElementById('traffic-canvas');
const ctx = canvas.getContext('2d');
const lightRadius = 20; // radius of the traffic light circles
const lightSpacing = 50; // space between the traffic light circles
const lightX = canvas.width / 2; // x-coordinate of the center of the traffic light
const lightY = canvas.height / 2; // y-coordinate of the center of the traffic light
const roadWidth = 150; // width of the road
const carSize = 40; // size of the cars
let currentLight = 'red'; // the current state of the traffic light
let car1X = 0; // x-coordinate of the first car
let car2X = canvas.width - carSize; // x-coordinate of the second car
let car3X = canvas.width - carSize; // x-coordinate of the second car
let car4X = canvas.width - carSize; // x-coordinate of the second car
let car1Speed = 2; // speed of the first car
let car2Speed = 4; // speed of the second car
let car3Speed = 4; // speed of the second car
let car4Speed = 4; // speed of the second car
let lastTime = Date.now(); // time of the last update
function drawTrafficLight() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // clear the canvas
// draw the roads
ctx.fillStyle = 'gray';
ctx.fillRect(0, lightY - roadWidth / 2, canvas.width, roadWidth / 2); // top left lane
ctx.fillRect(0, lightY, canvas.width, roadWidth / 2); // top right lane
ctx.fillRect(lightX - roadWidth / 2, 0, roadWidth / 2, canvas.height); // bottom left lane
ctx.fillRect(lightX, 0, roadWidth / 2, canvas.height); // bottom right lane
ctx.strokeStyle = 'white';
ctx.setLineDash([5, 5]); // draw a dashed line
ctx.beginPath();
ctx.moveTo(lightX - roadWidth / 4, 0);
ctx.lineTo(lightX - roadWidth / 4, canvas.height);
ctx.stroke(); // draw the separator on the bottom road
ctx.beginPath();
ctx.moveTo(lightX + roadWidth / 4, 0);
ctx.lineTo(lightX + roadWidth / 4, canvas.height);
ctx.stroke(); // draw the separator on the bottom road
ctx.beginPath();
ctx.moveTo(0, lightY - roadWidth / 4);
ctx.lineTo(canvas.width, lightY - roadWidth / 4);
ctx.stroke(); // draw the separator on the top road
ctx.beginPath();
ctx.moveTo(0, lightY + roadWidth / 4);
ctx.lineTo(canvas.width, lightY + roadWidth / 4);
ctx.stroke(); // draw the separator on the top road
ctx.setLineDash([]); // reset the line dash style
// draw the roads
ctx.fillStyle = 'gray';
ctx.fillRect(0, lightY - roadWidth / 2, canvas.width, roadWidth);
ctx.fillRect(lightX - roadWidth / 2, 0, roadWidth, canvas.height);
// draw the red light
ctx.beginPath();
ctx.arc(lightX, lightY - lightSpacing, lightRadius, 0, 2 * Math.PI);
ctx.fillStyle = currentLight === 'red' ? 'red' : 'gray';
ctx.fill();
ctx.stroke();
// draw the yellow light
ctx.beginPath();
ctx.arc(lightX, lightY, lightRadius, 0, 2 * Math.PI);
ctx.fillStyle = currentLight === 'yellow' ? 'yellow' : 'gray';
ctx.fill();
ctx.stroke();
// draw the green light
ctx.beginPath();
ctx.arc(lightX, lightY + lightSpacing, lightRadius, 0, 2 * Math.PI);
ctx.fillStyle = currentLight === 'green' ? 'green' : 'gray';
ctx.fill();
ctx.stroke();
// draw the cars
ctx.fillStyle = 'blue';
ctx.fillRect(car1X, lightY - carSize / 2, carSize, carSize);
ctx.fillRect(car2X, lightY - carSize / 2, carSize, carSize);
}
// function to move the cars
function moveCars() {
// calculate the elapsed time since the last update
const now = Date.now();
const elapsed = now - lastTime;
lastTime = now;
// Calculate the intermediate position of the cars based on the elapsed time
car1X += car1Speed * elapsed / 1000;
car2X -= car2Speed * elapsed / 1000;
car3X += car3Speed * elapsed / 1000;
car4X -= car4Speed * elapsed / 1000;
// move the cars to the opposite side of the canvas when they go off the screen
if (car1X > canvas.width) {
car1X = -carSize;
}
if (car2X < -carSize) {
car2X = canvas.width;
}
if (car3X > canvas.width) {
car3X = -carSize;
}
if (car4X < -carSize) {
car4X = canvas.width;
}
}
// function to change the state of the traffic light
function changeLight() {
if (currentLight === 'red') {
currentLight = 'green';
setTimeout(changeLight, Green_time*1000);
} else if (currentLight === 'yellow') {
currentLight = 'red';
setTimeout(changeLight, Red_time*1000);
} else if (currentLight === 'green') {
currentLight = 'yellow';
setTimeout(changeLight, Yellow_time*1000);
}
drawTrafficLight(); // update the traffic light and cars on the canvas
}
// start the traffic light and car updates
setInterval(moveCars, 20); // update the cars every 10 milliseconds
changeLight(); // start the traffic light change sequence
drawTrafficLight(); // draw the initial traffic light and cars on the canvas
</script>
</body>
</html>