canvas绘制时钟

css样式

<style>
			body {
				background: black
			}
			
			#canvas {
				background: white;
			}
		</style>

html

	<canvas id="canvas" width="400" height="400">
                   不支持
    </canvas>

js:

<script>
			var oC = document.getElementById("c1"); //获取到canvas元素
			var oGc = oC.getContext("2d"); //创建一个2d空间

			setInterval(Time, 1000); //每隔一秒调用一次
			function Time() {
				var nowTime = new Date();
				var nH = nowTime.getHours(); //获得当前小时
				var nM = nowTime.getMinutes(); //获得当前分钟数
				var nS = nowTime.getSeconds(); //获得当前秒数

				oGc.clearRect(0, 0, 400, 400);
				oGc.beginPath();
				oGc.arc(200, 200, 150, 0, 360 * Math.PI / 180, false); //PI是 π,false为顺时针 150为半径长度
				//    oGc.beginPath();  //加上外面的圆就不见了
				for(var i = 0; i < 60; i++) {
					oGc.moveTo(200, 200); //将表中心移到画布的中心
					oGc.arc(200, 200, 150, i * 6 * Math.PI / 180, i * 6 * Math.PI / 180, false);
				}
				//最外面一圈的圆
				oGc.stroke(); //画线,默认黑色
				oGc.beginPath();
				oGc.fillStyle = "#fff"; //填充颜色
				oGc.arc(200, 200, 150 * 19 / 20, 0, 360 * Math.PI / 180, false);
				oGc.fill(); //结束画线
				//12个小短线和里面的小小短线
				oGc.beginPath();
				oGc.lineWidth = "3";
				for(var i = 0; i < 12; i++) {
					oGc.moveTo(200, 200);
					oGc.arc(200, 200, 150, i * 30 * Math.PI / 180, i * 30 * Math.PI / 180, false);
				}
				oGc.stroke();
				oGc.fillStyle = "#fff";
				oGc.arc(200, 200, 150 * 18 / 20, 0, 360 * Math.PI / 180, false);
				oGc.fill();
				//时针
				oGc.beginPath();
				oGc.lineWidth = "5";
				oGc.moveTo(200, 200);
				oGc.arc(200, 200, 150 * 10 / 20, (nH * 30 + nM / 2 - 90) * Math.PI / 180, (nH * 30 + nM / 2 - 90) * Math.PI / 180, false);
				oGc.stroke();
				//分针
				oGc.beginPath();
				oGc.lineWidth = "5";
				oGc.moveTo(200, 200);
				oGc.arc(200, 200, 150 * 13 / 20, (nM * 6 + nS / 10 - 90) * Math.PI / 180, (nM * 6 + nS / 10 - 90) * Math.PI / 180, false);
				oGc.stroke();
				//秒针
				oGc.beginPath();
				oGc.lineWidth = "5";
				oGc.moveTo(200, 200);
				oGc.arc(200, 200, 150 * 16 / 20, (nS * 6 - 90) * Math.PI / 180, (nS * 6 - 90) * Math.PI / 180, false);
				oGc.stroke();
			}
		</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值