canvas之时钟

刚学了canvas,用canvas做了一个时钟,很简单,但是每一秒获得新的时间都要重新绘制一次。

代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            html,body{
                height: 100%;
                overflow: hidden;
                background: pink;
            }
            #test{
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                margin: auto;
                background: gray;
            }
        </style>
    </head>
    <body>
        <canvas id="test" height="400" width="400"></canvas>
    </body>
    <script type="text/javascript">
        window.onload = function(){
            var canvas = document.querySelector("#test")
            if(canvas.getContext){
                var ctx = canvas.getContext("2d")
                setInterval(function  () {
                    ctx.clearRect(0,0,canvas.width,canvas.height)
                    move()
                },1000)
                move()
                function move(){
                    ctx.save()
                    //将圆心调整到画布中央
                    ctx.translate(200,200)
                    //调整坐标轴为正常的方向(逆时针旋转90度=顺时针旋转270度)
                    ctx.rotate(-90*Math.PI/180,false)
                    //初始化状态
                    ctx.strokeStyle = "black"
                    ctx.lineWidth = 6
                    ctx.lineCap = "round"
                    ctx.beginPath()
                    
                    //外层圆盘
                    ctx.save()
                    ctx.strokeStyle = "darkgray"
                    ctx.lineWidth = 5
                    ctx.beginPath()
                    ctx.arc(0,0,140,0,2*Math.PI)
                    ctx.stroke()
                    ctx.restore()
                        
                    //时钟刻度,利用循环来做
                    ctx.save()
                    for(var i = 0;i <= 12;i++){
                        //注意translate rotate scale等变换一定要在绘制图形之前写才能生效
                        //每一个时钟刻度旋转的角度为30度
                        ctx.rotate(30*Math.PI/180)//canvas中的rotate是累加的
                        ctx.beginPath()
                        ctx.moveTo(100,0)
                        ctx.lineTo(120,0)
                        ctx.stroke()
                    }
                    ctx.restore()
                        
                    //分针刻度,还是用循环
                    ctx.save()
                    ctx.lineWidth = 4
                    for(var i = 0;i <= 60;i++){
                        //注意translate rotate scale等变换一定要在绘制图形之前写才能生效
                        //每一个分针刻度旋转的角度为6度
                        ctx.rotate(6*Math.PI/180)
                        if((i+1) % 5 != 0){
                            ctx.beginPath()
                            ctx.moveTo(117,0)
                            ctx.lineTo(110,0)
                            ctx.stroke()
                        }
                    }
                    ctx.restore()
                    
                    
                    //时针、分针、秒针 表座
                    //初始化时间
                    var time = new Date()
                    var s = time.getSeconds()
                    var m = time.getMinutes() + s /60
                    var h = time.getHours() + m / 60
                    h = h > 12? h-12 : h
                    //时针
                    ctx.save()
                    ctx.lineWidth = 14
                    ctx.rotate(h*30*Math.PI/180)
                    ctx.beginPath()
                    ctx.moveTo(-20,0)
                    ctx.lineTo(55,0)
                    ctx.stroke()
                    ctx.restore()
                        
                    //分针
                    ctx.save()
                    ctx.lineWidth = 10
                    ctx.strokeStyle = "dimgray"
                    ctx.rotate(m*6*Math.PI/180)
                    ctx.beginPath()
                    ctx.moveTo(-20,0)
                    ctx.lineTo(85,0)
                    ctx.stroke()
                    ctx.restore()
                        
                    //秒针
                    ctx.save()
                    ctx.lineWidth = 8
                    ctx.strokeStyle = "darkslategray"
                    ctx.rotate(s*6*Math.PI/180)
                    ctx.beginPath()
                    ctx.moveTo(-20,0)
                    ctx.lineTo(115,0)
                    ctx.stroke()
                    ctx.restore()
                        
                    //表座
                    ctx.save()
                    ctx.fillStyle = "darkgray"
                    ctx.beginPath()
                    ctx.arc(0,0,10,0,2*Math.PI)
                    ctx.fill()
                    ctx.restore()
                    ctx.restore()
                }
            }
        }
    </script>
</html>

效果图:

时钟效果图

猜你喜欢

转载自blog.csdn.net/qq_31207499/article/details/81123595