<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>钟表
</title
>
<script type
="text/javascript">
window
.onload
=function(){
var canvas
=document
.getElementById("canvas");
var cx
=canvas
.getContext("2d");
function
clock(){
cx
.fillStyle
="tomato";
cx
.beginPath();
cx
.arc(300,300,200,0,Math
.PI
*2);
cx
.closePath();
cx
.fill();
cx
.lineWidth
=2;
cx
.strokeStyle
="black";
for(var i
=0;i
<12;i
++){
cx
.save();
cx
.translate(300,300);
cx
.rotate(i
*(Math
.PI
/6));
cx
.beginPath();
cx
.moveTo(0,-180);
cx
.lineTo(0,-200);
cx
.stroke();
cx
.closePath();
cx
.fillStyle
="black";
cx
.font
="16px blod";
cx
.rotate(Math
.PI
/6);
cx
.fillText(i
+1,-6,-220);
cx
.restore();
}
for(var i
=0;i
<60;i
++){
cx
.save();
cx
.translate(300,300);
cx
.rotate(i
*(Math
.PI
/30));
cx
.beginPath();
cx
.moveTo(0,-190);
cx
.lineTo(0,-200);
cx
.stroke();
cx
.closePath();
cx
.restore();
}
var today
=new
Date();
var hour
=today
.getHours();
var min
=today
.getMinutes();
var sec
=today
.getSeconds();
hour
=hour
+min
/60;
cx
.lineWidth
=4;
cx
.save();
cx
.translate(300,300);
cx
.beginPath();
cx
.rotate(hour
*(Math
.PI
/6));
cx
.moveTo(0,10);
cx
.lineTo(0,-130);
cx
.closePath();
cx
.stroke();
cx
.restore();
cx
.lineWidth
=2;
cx
.save();
cx
.translate(300,300);
cx
.beginPath();
cx
.rotate(min
*(Math
.PI
/30));
cx
.moveTo(0,10);
cx
.lineTo(0,-160);
cx
.closePath();
cx
.stroke();
cx
.restore();
cx
.lineWidth
=1;
cx
.strokeStyle
="black";
cx
.save();
cx
.translate(300,300);
cx
.beginPath();
cx
.rotate(sec
*(Math
.PI
/30));
cx
.moveTo(0,10);
cx
.lineTo(0,-160);
cx
.closePath();
cx
.stroke();
cx
.restore();
cx
.fillStyle
='#ccc';
cx
.strokeStyle
="red";
cx
.save();
cx
.translate(300,300);
cx
.beginPath();
cx
.beginPath();
cx
.arc(0,0,4,0,Math
.PI
*2);
cx
.closePath();
cx
.fill();
cx
.closePath();
cx
.stroke();
cx
.restore();
setTimeout(clock
,1000);
}
clock();
}
</script
>
</head
>
<body
>
<canvas id
="canvas" width
="1300px" height
="600px" style
="background-color: #ccc;"></canvas
>
</body
>
</html
>
转载请注明原文地址:https://tech.qufami.com/read-17650.html