<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{padding: 0;margin: 0;}
div{
width:400px;height:400px;
position:relative;
margin:100px auto;
}
canvas{
background:white;
}
</style>
</head>
<body>
<div>
<canvas id="mycan" width="400px" height="400px"></canvas>
</div>
<script>
var myCan=document.getElementById("mycan");
var Can=myCan.getContext("2d");
Can.translate(200,200);
var str=[12,1,2,3,4,5,6,7,8,9,10,11];
Can.rotate(-Math.PI/2);
setInterval(function(){
Can.clearRect(-200,-200,400,400);
var data = new Date();
var h = data.getHours();
var f = data.getMinutes();
var s = data.getSeconds();
var hours=h*Math.PI/6+f*Math.PI/360+s*Math.PI/21600;
var minutes=f*Math.PI/30+s*Math.PI/30/60;
var seconds=s*Math.PI/30;
Can.save();
Can.beginPath();
for(var i=0;i<60;i++){
Can.strokeStyle="red";
Can.moveTo(0,0);
Can.arc(0,0,180,(Math.PI)/30*i,(Math.PI/30)*(i+1),false);
Can.stroke();
}
Can.restore();
Can.save();
Can.beginPath();
Can.fillStyle="white"
Can.arc(0,0,172,0,Math.PI*2,false);
Can.fill();
Can.restore();
Can.save();
Can.beginPath();
for(var i=0;i<12;i++){
Can.lineWidth="3";
Can.strokeStyle="red";
Can.moveTo(0,0);
Can.arc(0,0,180,(Math.PI)/6*i,(Math.PI/6)*(i+1),false);
Can.stroke();
}
Can.restore();
Can.save();
Can.beginPath();
Can.fillStyle="white"
Can.arc(0,0,165,0,Math.PI*2,false);
Can.fill();
Can.restore();
//时
Can.save();
Can.beginPath();
Can.rotate(seconds);
Can.lineWidth="1";
Can.moveTo(0,0);
Can.lineTo(150,0);
Can.stroke();
Can.restore();
//分
Can.save();
Can.beginPath();
Can.rotate(minutes);
Can.lineWidth="3";
Can.moveTo(0,0);
Can.lineTo(120,0);
Can.stroke();
Can.restore();
//秒
Can.save();
Can.beginPath();
Can.rotate(hours);
Can.lineWidth="5";
Can.moveTo(0,0);
Can.lineTo(90,0);
Can.stroke();
Can.restore();
Can.save();
Can.beginPath();
Can.fillStyle="black"
Can.arc(0,0,5,0,Math.PI*2,false);
Can.fill();
Can.restore();
Can.save();
Can.beginPath();
Can.rotate(Math.PI/2)
Can.textBaseline="middle";
Can.textAlign="center";
Can.font="bold 20px 微软雅黑";
for(var i=0;i<str.length;i++){
Can.fillText(str[i],150*Math.sin(i*Math.PI/6),-150*Math.cos(i*Math.PI/6));
}
Can.restore();
},1000)
</script>
</body>
</html>
Canvas画表实现当前时间走动
最新推荐文章于 2023-08-23 20:28:01 发布