1.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
canvas{
position: absolute;
background: lightgreen;
left: 450px;
top:200px;
}
</style>
</head>
<body>
<h1>使用canvas绘制时钟</h1>
<canvas width="300px" height="300px" id="canvas"></canvas>
<script src="script.js"></script>
</body>
</html>
2.script.js
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var width = context.canvas.width;
var height = context.canvas.height;
var r = width/2;
var rem = width / 200;
//绘制表盘
function drawBackground() {
context.save();
context.translate(r,r);
context.beginPath();
context.lineWidth = 10 * rem;
context.arc(0,0,r - context.lineWidth/2,0,2 * Math.PI,false);
context.stroke();
//绘制数字
var hourNumbers = [3,4,5,6,7,8,9,10,11,12,1,2];
context.textAlign = "center";
context.textBaseline = "middle";
context.font = 18 * rem + "px Arial";
hourNumbers.forEach(function(number,index){
var rad = 2 * Math.PI / 12 * index;
var x = Math.cos(rad) * (r - 40* rem);
var y = Math.sin(rad) * (r - 40* rem);
context.fillText(number,x,y);
});
//绘制时刻线
for(var i = 0;i < 60;i++) {
var rad = 2 * Math.PI / 60 * i;
var x = Math.cos(rad) * (r - 20 * rem);
var y = Math.sin(rad) * (r - 20 * rem);
context.beginPath();
if (i % 5 === 0) {
context.fillStyle = "#999";
context.arc(x,y,4 * rem,0,2 * Math.PI,false);
} else {
context.fillStyle = "#000";
context.arc(x,y,3 * rem,0,2 * Math.PI,false);
}
context.fill();
}
}
function drawHour(hour,minute) {
context.save();
context.beginPath();
var radH = 2 * Math.PI /12 * hour;
var radM = 2 * Math.PI /12 /60 * minute;
var rad = radH + radM;
context.rotate(rad);
context.lineWidth = 10 * rem;
context.lineCap = "round";
context.moveTo(0,10 * rem);
context.lineTo(0,-r + 40 * rem);
context.stroke();
context.restore();
}
function drawMinute(minute) {
context.save();
context.beginPath();
var rad = 2 * Math.PI / 60 * minute;
context.rotate(rad);
context.lineWidth = 4 * rem;
context.lineCap = "round";
context.moveTo(0,10 * rem);
context.lineTo(0, -r + 18 * rem);
context.stroke();
context.restore();
}
function drawSeconds(second) {
context.save();
context.beginPath();
context.fillStyle = "#c14543";
var rad = 2 * Math.PI / 60 * second;
context.rotate(rad);
context.moveTo(-2 * rem,20 * rem);
context.lineTo(2 * rem,20 * rem);
context.lineTo(0, -r + 18 * rem);
context.lineTo(-1 * rem,-r + 18 * rem);
context.fill();
context.restore();
}
function drawDot() {
context.beginPath();
context.fillStyle = "#FFF";
context.arc(0,0,4 * rem,0,2 * Math.PI,false);
context.fill();
}
function draw(){
context.clearRect(0,0,canvas.width,canvas.height);
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
drawBackground();
drawHour(hour,minute);
drawMinute(minute);
drawSeconds(second);
drawDot();
context.restore();
}
draw();
setInterval(draw,1000);
Html5--canvas时钟
最新推荐文章于 2022-05-11 19:24:55 发布