首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >交通灯序列

交通灯序列
EN

Code Review用户
提问于 2016-02-11 14:47:18
回答 1查看 8.5K关注 0票数 4

我使用数组和if语句创建了一个功能正常的自动交通灯序列。这一切都是正确的,但我想知道我还能做些什么来改进我的代码,而不改变它的结构或工作方式,也不需要使用字典等等。

代码语言:javascript
复制
<!DOCTYPE html>
<head>
	<title> Traffic Light</title>	
	
	<style>
		.rainbow {
			background-image: -webkit-gradient( linear, left top, right top, color-stop(0, red), color-stop(0.1, yellow), color-stop(0.2, green));
			background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
			color:transparent;
			-webkit-background-clip: text;
			background-clip: text;
		}
	</style>

</head>

<body background="street.gif">
	<h1 class="rainbow">Traffic Light</h1>
		
	<canvas id="myCanvas" width="200" height="300"
	style="border:1px solid #000000;">
	Your browser does not support the HTML5 canvas tag.
	</canvas>
	
	
	<script>   
	
		var c = document.getElementById("myCanvas");
		var ctx = c.getContext("2d");

		ctx.rect(0, 0, 200, 300);
		ctx.fillStyle = "grey";
		ctx.fill();

		var colours=["red", "yellow", "green", "black","red yellow"];
		var current=colours[0];

		function offlight() {
			ctx.beginPath();
			ctx.arc(95,50,40,10,12*Math.PI);
			ctx.fillStyle = "black";
			ctx.fill();
			ctx.stroke();
		}
	
		function offlight1() {
			ctx.beginPath();
			ctx.arc(95,150,40,10,12*Math.PI);
			ctx.fillStyle = "black";
			ctx.fill();
			ctx.stroke();
		}
	
		function offlight2() {
			ctx.beginPath();
			ctx.arc(95,250,40,10,12*Math.PI);
			ctx.fillStyle = "black";
			ctx.fill();
			ctx.stroke();
		}
	
		function drawLight1() {
			ctx.beginPath();
			ctx.arc(95,50,40,10,12*Math.PI);
			ctx.fillStyle = "red";
			ctx.fill();
			ctx.stroke();
		}
	
		function drawLight2() {
			ctx.beginPath();
			ctx.arc(95,150,40,10,12*Math.PI);
			ctx.fillStyle = "yellow";
			ctx.fill();
			ctx.stroke();
		}

		function drawLight3() {
			ctx.beginPath();
			ctx.arc(95,250,40,10,12*Math.PI);
			ctx.fillStyle = "green";
			ctx.fill();
			ctx.stroke();
		}

		function changelight(){

			if (current==colours[0]){
				drawLight1();
				offlight1();
				offlight2();
				current=colours[4]
			} else if (current==colours[4]){
				drawLight1();
				drawLight2();
				offlight2();
				current=colours[2]
			} else if (current==colours[2]) {
				offlight();
				offlight1();
				drawLight3();
				current=colours[3]
			} else if (current==colours[3]){
				offlight();
				drawLight2();
				offlight2();
				current=colours[0]
			}

		}
		setInterval(changelight,1000);

	</script>
		
	<br><br>
	<button onclick="changelight()">Click</button>
			
</body>
</html>
EN

回答 1

Code Review用户

回答已采纳

发布于 2016-02-11 14:49:14

啊,你把你的<h1>搬到<body>里了。你到底在找什么?文件大小(->代码缩减)?速度优化?

一件简单的事情是使用parameters来减少代码重复:

代码语言:javascript
复制
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

ctx.rect(0, 0, 200, 300);
ctx.fillStyle = "grey";
ctx.fill();

var colours = ["red", "yellow", "green",  "red yellow"];
var current = colours[0];

function offlight(a1) {
    ctx.beginPath();
    ctx.arc(95, a1, 40, 10, 12 * Math.PI);
    ctx.fillStyle = "black";
    ctx.fill();
    ctx.stroke();
}

function drawLight(a1, fillParam) {
    ctx.beginPath();
    ctx.arc(95, a1, 40, 10, 12 * Math.PI);
    ctx.fillStyle = fillParam;
    ctx.fill();
    ctx.stroke();
}

function changelight() {
    if (current == colours[0]) {
        drawLight(50, "red");
        offlight(150);
        offlight(250);
        current = colours[4]
    } else if (current == colours[4]) {
        drawLight(50, "red");
        drawLight(150, "yellow");
        offlight(250);
        current = colours[2]
    } else if (current == colours[2]) {
        offlight(50);
        offlight(150);
        drawLight(250, "green");
        current = colours[1]
    } else if (current == colours[1]) {
        offlight(50);
        drawLight(150, "yellow");
        offlight(250);
        current = colours[0]
    }

}
setInterval(changelight, 1000);

你用过颜色吗?(“黄色”)

您还可以删除空白空间,缩短变量和函数名。还可以考虑将JS移动到一个单独的文件中。

这是一把有用的小提琴。

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/119646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档