JS+HTML 5+CSS动态钟表

Canvas的使用:画布
在这里插入图片描述
圆arc(x,y,r,0,b)、直线(指定点到指定点 起点:cxt.moveTo(x,y)、终点:cxt.lineTo(x,y))、直线填充(strokeStyle())、定时器setInterval(function(){函数},t),函数的调用是一秒重绘,消除前一帧图像为 cxt.clearRect(x,y,width,height)
主要是表盘刻度的绘制,在网上看到别人使用的是translate(),我使用正弦、余弦函数完成。本来想用圆的公式,想了想好像更难,就放弃尝试了。

<!doctype html>
<html lang="en">

		 <head>
			  <meta charset="UTF-8">
			  <meta name="Generator" content="EditPlus®">
			  <title>Document</title>
			  <style type="text/css">
				#myCanvas{
					border: 1px solid red;
					position: absolute;
				}
				#demo{
					margin-left: 226px;
					padding-top: 39px;
				}
				#date{
					margin-left: 217px;
					padding-top: 339px;
				}
				#day{
					margin-left: 232px;
					margin-top: -8px;
				}
			  </style>
		 </head>
			
		 <body>
			<canvas id = "myCanvas" width = "500px" height = "500px">
			</canvas>
			<h2 id = 'demo'>时钟</h2>
			<h3 id = 'date'><h3>
			<h4 id = 'day'><h4>
		<script type="text/javascript">
			var canvas = document.getElementById('myCanvas');
			var cxt = canvas.getContext('2d');
				//秒针动画
				setInterval(function(){
						clockPane();
						mte(cxt);
					},1000);
	//表盘绘制
function clockPane(){
	var time = new Date();
	var h = time.getHours();
	var m = time.getMinutes();
	var s = time.getSeconds();
		if(m<10){
			m = '0' +m;
		}
		if(s<10){
			s = '0' +s;
		}
	        document.getElementById('date').innerHTML = h + ':' + m + ':' + s;
	var day;
			switch(time.getDay()){
				case 0:
					day = '星期日';
					break;
				case 1:
					day = '星期一';
					break;
				case 2:
					day = '星期二';
					break;
				case 3:
					day = '星期三';
					break;
				case 4:
					day = '星期四';
					break;
				case 5:
					day = '星期五';
					break;
				case 6:
					day = '星期六';
					break;
			}
			document.getElementById('day').innerHTML = day;
			cxt.clearRect(0,0,500,500);
			cxt.beginPath();
			cxt.lineWidth = 2;
			cxt.strokeStyle = 'black';
			//画圆
			cxt.arc(250,250,150,0,2*Math.PI);
			cxt.arc(250,250,155,0,2*Math.PI);
			//画秒针刻度
	for(var i = 0; i< 60; i ++){
			cxt.moveTo(250+145*Math.cos((i*Math.PI/30)),250-145*Math.sin((i*Math.PI/30)));
			cxt.lineTo(250+150*Math.cos((i*Math.PI/30)),250-150*Math.sin((i*Math.PI/30)));
					}
			//画时、分针刻度
	for(var i = 0; i< 30; i ++){
			cxt.moveTo(250+140*Math.cos((i*Math.PI/6)),250-140*Math.sin((i*Math.PI/6)));
			cxt.lineTo(250+150*Math.cos((i*Math.PI/6)),250-150*Math.sin((i*Math.PI/6)));
					}
			cxt.stroke();
			cxt.closePath();
}
//时、分、秒针绘制
function mte(ctx){
				//秒针
	var d = new Date();   
	var i = -d.getSeconds()+15;    //获取现在时间的秒数
			ctx.beginPath();
			ctx.strokeStyle = "Darkorange";
			ctx.lineWidth = 1;
			ctx.moveTo(250,250);
			ctx.lineTo(250+130*Math.cos((i*Math.PI/30)),250-130*Math.sin((i*Math.PI/30)));
			ctx.stroke();
			ctx.closePath();
				//分针
	var m = -d.getMinutes()+15;
			ctx.beginPath();
			ctx.lineWidth = 2;
			cxt.strokeStyle = 'black';
			ctx.moveTo(250,250);
			ctx.lineTo(250+115*Math.cos((m*Math.PI/30)),250-115*Math.sin((m*Math.PI/30)));
			ctx.stroke();
			ctx.closePath();
				//时针
	var h = d.getHours();
		if(h>12){
			 h = h - 12;
		}
			ctx.beginPath();
			ctx.lineWidth = 3;
			cxt.strokeStyle = 'black';
			ctx.moveTo(250,250);
			ctx.lineTo(250+100*Math.cos((h-3)*Math.PI/6+d.getMinutes()*Math.PI/360),
			           250+100*Math.sin((h-3)*Math.PI/6+d.getMinutes()*Math.PI/360));
			           //需要注意的是Math.cos(x)中x的值必须是弧度值,因为时针计算时需要时针时间加上分针时间来计算时针走过的角度。
			ctx.stroke();
			ctx.closePath();
		}
			</script>
		 </body>

</html>

重用的代码很多,需要简化。
数学很重要,要计算,没事多看看。

动态时钟 body,div,p{ font-family: 'Microsoft Yahei' ;font-size: 14px;} .box{ width: 400px; height: 400px; border:10px solid #8bf2f1;margin:100px auto; border-radius: 50%; box-shadow: 0px 0px 20px 3px #444 inset; position: relative;} /*原点*/ .origin{ width: 20px; height: 20px; border-radius: 50%; background: #ff0000; top:190px; left: 190px; position: absolute;} /* 指针 */ .clock_line{ position: absolute;position:absolute;z-index:20;} .hour_line{width:100px;height:4px;top:198px;left:200px;background-color:#000;border-radius:2px; transform-origin:0 50%;box-shadow:1px -3px 8px 3px #aaa;} .minute_line{width:130px;height:2px;top:199px;left:190px;background-color:#000; transform-origin:7.692% 50%;box-shadow:1px -3px 8px 1px #aaa;} .second_line{width:170px;height:1px;top:199.5px;left:180px;background-color:#f60; transform-origin:11.765% 50%;box-shadow:1px -3px 7px 1px #bbb;} .dot_box{width: inherit; height: inherit;} /*时钟数*/ .dot{ width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 22px; position: absolute;} .clock-scale{width:195px;height:2px;transform-origin:0% 50%;z-index:7; position:absolute;top:199px;left:200px;} .scale-show{ width:12px;height:2px;background-color:#555;float:left;} .scale-hidden{width:183px;height:2px;float:left;} /*日期*/ .date_info{width:160px;height:28px; line-height:28px;text-align:center;position:absolute;top:230px;left:120px;z-index:11;color:#555; font-weight:bold;} .time_info{ width: 110px; height: 35px; line-height: 35px;text-align:center;position:absolute;top:270px;left:150px;z-index:11;color:#555; background: #253e3e; } .time{ width: 35px ;height:35px; float: left; color: #fff; font-size: 22px;} #minute_time{border-left:1px solid #fff;border-right:1px solid #fff;} <div class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值