Canvas小例子-时钟

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas test</title>
</head>
<body>
<div class="wrap">
    <canvas  id="canvas" width="400px" height="300px" style="border: 1px solid red;">
    </canvas>
</div>


<script type="text/javascript">
    window.onload = draw;

    function draw() {
        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            var deg=2*Math.PI/12;
            var context = canvas.getContext("2d");
            context.clearRect(0,0,400,400)
            context.save();
            context.beginPath();
            context.arc(140, 140, 130, 0, 2 * Math.PI, true);
            context.stroke();
            context.closePath();
            context.beginPath();
            context.arc(140, 140, 125, 0, 2 * Math.PI, true);
            context.stroke();
            context.closePath();
            context.save();
            context.translate(140,140)
            context.beginPath();

            for(var i=1;i<13;i++){
                var x1=Math.sin(i*deg);
                var y1=-Math.cos(i*deg);
                context.fillStyle="#000";
                context.font="bold 20px Calibri";
                context.textAlign='center';
                context.textBaseline='middle';
                context.fillText(i,x1*110,y1*110);
            }
            context.stroke();
            context.closePath();
            context.restore();
            context.save();
            context.translate(140,140)
            var deg1=2*Math.PI/60;
            context.beginPath();
            for(var i=0;i<60;i++){
                var x2=Math.sin(i*deg1);
                var y2=-Math.cos(i*deg1);
                context.moveTo(x2*123,y2*123);
                context.lineTo(x2*120,y2*120);
            }
            context.strokeStyle='#000';
            context.lineWidth=2;
            context.stroke();
            context.closePath();
            context.restore();
            context.translate(140,140)
            start(context)
            setTimeout(draw,1000);
        }
    }

    function  start(context){

        var date = new Date();
        var seconds = date.getSeconds()*2*Math.PI/60;
        var minutes = date.getMinutes()*6*Math.PI/180+seconds/60;
        var hour =(date.getHours()%12)*0.5*Math.PI/180+minutes/12+seconds/720;
        drawLine(context,hour,-48,6);
        drawLine(context,minutes,-78,3);
        drawLine(context,seconds,-90,1);
        context.restore();
        context.restore();
    }
    function drawLine(context,mintues,endpoint,width){
        context.save();
        context.beginPath();
        context.rotate(mintues);
        context.moveTo(0,5);
        context.lineTo(0,endpoint);
        context.strokeStyle="black";
        context.lineWidth=width;
        context.stroke();
        context.closePath();
        context.restore();
    }
</script>
</body>
</html>

最后效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值