forked from gniziemazity/self-driving-car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
40 lines (31 loc) · 1.08 KB
/
main.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 carCanvas=document.getElementById("carCanvas");
carCanvas.width=200;
const networkCanvas=document.getElementById("networkCanvas");
networkCanvas.width=300;
const carCtx = carCanvas.getContext("2d");
const networkCtx = networkCanvas.getContext("2d");
const road=new Road(carCanvas.width/2,carCanvas.width*0.9);
const car=new Car(road.getLaneCenter(1),100,30,50,"AI");
const traffic=[
new Car(road.getLaneCenter(1),-100,30,50,"DUMMY",2)
];
animate();
function animate(time){
for(let i=0;i<traffic.length;i++){
traffic[i].update(road.borders,[]);
}
car.update(road.borders,traffic);
carCanvas.height=window.innerHeight;
networkCanvas.height=window.innerHeight;
carCtx.save();
carCtx.translate(0,-car.y+carCanvas.height*0.7);
road.draw(carCtx);
for(let i=0;i<traffic.length;i++){
traffic[i].draw(carCtx,"red");
}
car.draw(carCtx,"blue");
carCtx.restore();
networkCtx.lineDashOffset=-time/50;
Visualizer.drawNetwork(networkCtx,car.brain);
requestAnimationFrame(animate);
}