Html5--canvas时钟

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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值