canvas 绘制的简易时钟

<!DOCTYPE html>
<html lang="en">
	<head>
	<meta charset="UTF-8">
	<title>钟表</title>
	<style>
		canvas{
			position:absolute;
			left:10;
			top:10;
		}
	</style>
	</head>
	<body>
	<canvas id="dial_canvas">表盘</canvas>
	<canvas id="hand_canvas">指针</canvas>

	<script>
		//定义变量
		var w = 600, h = 600; //画布的宽高
		var x = 300, y = 300; //圆心坐标
		var r = 200;     //半径

		//获取 canvas元素
		var dial_canvas = document.querySelector("#dial_canvas");
		dial_canvas.width = w;
		dial_canvas.height = h;

		var hand_canvas = document.querySelector("#hand_canvas");
		hand_canvas.width = w;
		hand_canvas.height = h;


		//获取绘图环境
		var dial_cxt = dial_canvas.getContext("2d");
		var hand_cxt = hand_canvas.getContext("2d");


		//绘制表盘
		dial_cxt.arc(x, y, r, 0, Math.PI * 2);
		dial_cxt.strokeStyle = "#000";
		dial_cxt.lineWidth = 10;
		dial_cxt.stroke();

		//绘制 小时 刻度
		drawScale(dial_cxt, x, y, r, r-20, 12, "#000", 10);

		//绘制 分钟刻度 
		drawScale(dial_cxt, x, y, r, r-10, 60, "#000", 4);


		//绘制指针
		function runTime(){

			
			hand_cxt.clearRect(0,0,w,h); //清空画布
			//hand_canvas.width = hand_canvas.width;

			//获取当前时间
			var date = new Date();

			//绘制秒针
			var s = date.getSeconds();
			drawHand(hand_cxt, x, y, s / 60 * 360, 180, 2, "red");

			//绘制分针
			var m = date.getMinutes() + s / 60;
			drawHand(hand_cxt, x, y, m / 60 * 360, 150, 5, "#000");


			//绘制时针
			var hour = date.getHours() % 12 + m / 60;
			drawHand(hand_cxt, x, y, hour / 12 * 360, 120, 8, "#000")
			
			setTimeout(runTime, 1000);
		}

		runTime();



		

		/**
         * 绘制指针
         * object cxt 绘图环境
         * number x 圆心x坐标
         * number y 圆心y坐标
         * number angle 偏移的角度
         * number handLength 指针的长度
         * number handWidth  指针的宽度
         * string handColor  指针的颜色 
		*/
		function drawHand(cxt, x, y, angle, handLength, handWidth = 10, handColor = "#000"){
			
			cxt.save();
			cxt.translate(x, y);
			cxt.rotate((angle - 90) / 180 * Math.PI);

			cxt.beginPath();
			cxt.moveTo(-20, 0);
			cxt.lineTo(handLength, 0);

			cxt.strokeStyle = handColor;
			cxt.lineWidth = handWidth;
			cxt.stroke();

			cxt.restore();
		}

		/**
         * 绘制刻度
         *  object cxt  绘图环境
		 *  nubmer x    圆心x坐标
		 *  nubmer y    圆心y坐标
		 *  number outerRadius  外圆半径
		 *  number innerRadius  内圆半径
		 *  number num  刻度的数量
		 *  number strokeStyle  刻度的颜色 默认值 #000
		 *  number strokeWidth   刻度的宽度 默认值 10
		*/
		function drawScale(cxt, x, y, outerRadius, innerRadius, num, strokeStyle = "#000", strokeWidth = 10) {
			//计算 间隔的角度(弧度)
			var angle = Math.PI * 2 / num;
			//相关设置
			cxt.beginPath();  //开启路径
			cxt.strokeStyle = strokeStyle;
			cxt.lineWidth = strokeWidth;

			//循环绘制
			for (var i = 0; i < num; i ++) {
				var x1 = Math.cos(angle * i) * outerRadius + x;
				var y1 = Math.sin(angle * i) * outerRadius + y;

				var x2 = Math.cos(angle * i) * innerRadius + x;
				var y2 = Math.sin(angle * i) * innerRadius + y;

				cxt.moveTo(x1, y1);
				cxt.lineTo(x2, y2);
			}


			cxt.stroke();  //进行绘制

		}
	</script>
	</body>
</html>

转载于:https://my.oschina.net/u/3502339/blog/1069072

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的HTML5 Canvas绘制时钟的示例代码: ``` <!DOCTYPE html> <html> <head> <title>Canvas时钟</title> <style> canvas { border: 2px solid black; } </style> </head> <body> <canvas id="clock" width="200" height="200"></canvas> <script> function drawClock() { // 获取Canvas元素 var canvas = document.getElementById("clock"); var ctx = canvas.getContext("2d"); // 获取Canvas中心点坐标 var centerX = canvas.width / 2; var centerY = canvas.height / 2; // 绘制表盘 ctx.beginPath(); ctx.arc(centerX, centerY, 80, 0, 2 * Math.PI); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制刻度 for (var i = 1; i <= 12; i++) { var angle = i * Math.PI / 6; var x = centerX + 60 * Math.sin(angle); var y = centerY - 60 * Math.cos(angle); ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI); ctx.fillStyle = "black"; ctx.fill(); } // 绘制时针 var now = new Date(); var hour = now.getHours() % 12; var minute = now.getMinutes(); var second = now.getSeconds(); var hourAngle = (hour + minute / 60) * Math.PI / 6; var hourX = centerX + 40 * Math.sin(hourAngle); var hourY = centerY - 40 * Math.cos(hourAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(hourX, hourY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制分针 var minuteAngle = minute * Math.PI / 30; var minuteX = centerX + 60 * Math.sin(minuteAngle); var minuteY = centerY - 60 * Math.cos(minuteAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(minuteX, minuteY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制秒针 var secondAngle = second * Math.PI / 30; var secondX = centerX + 60 * Math.sin(secondAngle); var secondY = centerY - 60 * Math.cos(secondAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(secondX, secondY); ctx.strokeStyle = "red"; ctx.stroke(); } // 每秒钟更新时钟 setInterval(drawClock, 1000); </script> </body> </html> ``` 这段代码使用Canvas绘制了一个时钟,并且每秒钟更新一次。你可以将代码复制到一个HTML文件中运行,就可以看到时钟的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值