Javascript 我怎样做一个自动红绿灯?
Javascript 我怎样做一个自动红绿灯?,javascript,css,Javascript,Css,var RED=“#FF0000”; 变量黄色=“#FFFF00”; 变量绿色=“#00FF00”; var深红色=“#380000”; 变量暗黄=“#383800”; var深绿色=“#003800”; var X_ALL=150; 变量Y_RED=100; 变量Y_黄色=Y_红色+150; 变量Y_绿色=Y_黄色+150; var交通灯状态机; 函数TrafficLightsStateMachine(){ 该状态=0; this.stateMachine=新数组(); this.stateM
var RED=“#FF0000”;
变量黄色=“#FFFF00”;
变量绿色=“#00FF00”;
var深红色=“#380000”;
变量暗黄=“#383800”;
var深绿色=“#003800”;
var X_ALL=150;
变量Y_RED=100;
变量Y_黄色=Y_红色+150;
变量Y_绿色=Y_黄色+150;
var交通灯状态机;
函数TrafficLightsStateMachine(){
该状态=0;
this.stateMachine=新数组();
this.stateMachine[0]=函数(){drawCircles(深红色、黄色、深绿色);};
this.stateMachine[1]=函数(){drawCircles(红色、深黄色、深绿色);};
this.stateMachine[2]=函数(){drawCircles(红色、黄色、深绿色);};
this.stateMachine[3]=函数(){drawCircles(深红色、深黄色、绿色);};
this.process=函数(){
this.stateMachine[this.state]();
this.state=(this.state+1)%this.stateMachine.length;
};
this.drawCircle=函数(画布、颜色、x、y){
var context=canvas.getContext('2d');
context.strokeStyle=“#000000”;
context.fillStyle=颜色;
context.beginPath();
弧(x,y,50,0,Math.PI*2,真);
closePath();
stroke();
context.fill();
};
}
函数drawCircles(第一、第二、第三){
var id='canvas';
var canvas=document.getElementById(id);
if(canvas.getContext){
trafficLightsStateMachine.drawCircle(画布,第一,X_ALL,Y_RED);
trafficLightsStateMachine.drawCircle(画布,第二个,X_ALL,Y_黄色);
trafficLightsStateMachine.drawCircle(帆布,第三,X_ALL,Y_GREEN);
}
}
函数init(){
trafficLightsStateMachine=新trafficLightsStateMachine();
画圈(深红色、深黄色、绿色);
}
#第页
{
宽度:300px;
高度:500px;
保证金:自动;
}
#画布:悬停
{
光标:十字线;
背景色:#191919;
}
#帆布
{
背景色:#252525;
}
身体
{
背景:#22222;
颜色:白色;
}
将设置间隔(trafficLightsStateMachine.process,1000)
放在init()
函数的底部。将设置间隔(trafficLightsStateMachine.process,1000)
放在init()
函数的底部
结果:
var RED=“#FF0000”;
变量黄色=“#FFFF00”;
变量绿色=“#00FF00”;
var深红色=“#380000”;
变量暗黄=“#383800”;
var深绿色=“#003800”;
var X_ALL=150;
变量Y_RED=100;
变量Y_黄色=Y_红色+150;
变量Y_绿色=Y_黄色+150;
var交通灯状态机;
函数TrafficLightsStateMachine(){
该状态=0;
this.stateMachine=新数组();
this.stateMachine[0]=函数(){drawCircles(深红色、黄色、深绿色);};
this.stateMachine[1]=函数(){drawCircles(红色、深黄色、深绿色);};
this.stateMachine[2]=函数(){drawCircles(红色、黄色、深绿色);};
this.stateMachine[3]=函数(){drawCircles(深红色、深黄色、绿色);};
this.process=函数(){
this.stateMachine[this.state]();
this.state=(this.state+1)%this.stateMachine.length;
};
this.drawCircle=函数(画布、颜色、x、y){
var context=canvas.getContext('2d');
context.strokeStyle=“#000000”;
context.fillStyle=颜色;
context.beginPath();
弧(x,y,50,0,Math.PI*2,真);
closePath();
stroke();
context.fill();
};
}
函数drawCircles(第一、第二、第三){
var id='canvas';
var canvas=document.getElementById(id);
if(canvas.getContext){
trafficLightsStateMachine.drawCircle(画布,第一,X_ALL,Y_RED);
trafficLightsStateMachine.drawCircle(画布,第二个,X_ALL,Y_黄色);
trafficLightsStateMachine.drawCircle(帆布,第三,X_ALL,Y_GREEN);
}
}
函数init(){
trafficLightsStateMachine=新trafficLightsStateMachine();
画圈(深红色、深黄色、绿色);
}
window.onload=function(){
init();
setInterval(trafficLightsStateMachine.process.bind(trafficLightsStateMachine),1000);
}
#第页
{
宽度:300px;
高度:500px;
保证金:自动;
}
#画布:悬停
{
光标:十字线;
背景色:#191919;
}
#帆布
{
背景色:#252525;
}
身体
{
背景:#22222;
颜色:白色;
}
结果:
var RED=“#FF0000”;
变量黄色=“#FFFF00”;
变量绿色=“#00FF00”;
var深红色=“#380000”;
变量暗黄=“#383800”;
var深绿色=“#003800”;
var X_ALL=150;
变量Y_RED=100;
变量Y_黄色=Y_红色+150;
变量Y_绿色=Y_黄色+150;
var交通灯状态机;
函数TrafficLightsStateMachine(){
该状态=0;
this.stateMachine=新数组();
this.stateMachine[0]=函数(){drawCircles(深红色、黄色、深绿色);};
this.stateMachine[1]=函数(){drawCircles(红色、深黄色、深绿色);};
this.stateMachine[2]=函数(){drawCircles(红色、黄色、深绿色);};
this.statemachanine[3]=函数(){drawCircles(深_Ured,深色,Derk,GREEN);};
this.process=函数(){
this.stateMachine[this.state]();
this.state=(this.state+1)%this.stateMachine.length;
};
this.drawCircle=函数(画布、颜色、x、y){
var context=canvas.getContext('2d');
context.strokeStyle=“#000000”;
context.fillStyle=颜色;
context.beginPath();
弧(x,y,50,0,Math.PI*2,真);
closePath();
stroke();
context.fill();
};
}
函数drawCircles(第一、第二、第三){
var id='canvas';
var canvas=document.getElementById(id);
if(canvas.getContext){
trafficLightsStateMachine.drawCircle(画布,第一,X_ALL,Y_RED);
trafficLightsStateMachine.drawCircle(画布,第二个,X_ALL,Y_黄色);
交通灯
window.onload=function() {
init();
setInterval(
trafficLightsStateMachine.process.bind(trafficLightsStateMachine),
1000); // syntax necessary to be able to use "this" in the stateMachine
}