×

JS Canvas开发应用:挂钟酷炫效果

作者:andy0012021.06.02来源:Web前端之家浏览:5693评论:0
关键词:canvas

JS Canvas开发应用:挂钟酷炫效果。之前发过很多钟表的小应用了,大家可以先看下:

试玩一个简单的钟表效果

Canvas应用:实现最简单的挂钟效果

今天继续分享一个用canvas实现的。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS Canvas开发应用:挂钟酷炫效果 - Web前端之家www.jiangweishan.com</title>
</head>
<body>
    <canvas id="clock" width="500" height="500" ></canvas>
    <script>
        var canvas=document.getElementById("clock");
        var context=canvas.getContext("2d");
        
        // canvas.style.backgroundColor=getRandom()
        function drawClock(){
        //清除画布
        context.clearRect(0,0,canvas.width,canvas.height)
        //获取时间
        var now=new Date()
        var second=now.getSeconds()
        var minute=now.getMinutes()
        var hour1=now.getHours()
        var hour=hour1+minute/60;   //将24小时制转成12小时制,且为浮点型
            hour=hour>12?hour-12:hour;
        var time=now.toLocaleString()   //获取全部时间

        //绘制表盘
        context.beginPath() //开始路径
        context.strokeStyle=getRandom()   //线条颜色
        context.lineWidth=8 //线条粗细
        context.arc(250,250,200,0,360,false)
        context.stroke()
        context.closePath() //结束路径
        
        //绘制时刻度
        for(var i=0;i<12;i++){
            context.save()  //保存当前画布状态
            context.translate(250,250)  //重设画布的原点,以画布中心为原点
            context.lineWidth=3;    
            context.rotate(Math.PI/180*30*i)    //设置画布旋转角度,参数是弧度Math.PI/180*30
            context.beginPath()
            context.strokeStyle=getRandom()
            context.moveTo(0,-180)  //起始位置
            context.lineTo(0,-195)  //结束位置
            context.stroke()        
            context.closePath()
            context.restore()

        }
        //绘制分刻度
        for(var i=0;i<60;i++){
            context.save()  //保存当前画布状态
            context.translate(250,250)  //重设画布的原点,以画布中心为原点
            context.lineWidth=1;    
            context.rotate(Math.PI/180*6*i)    //设置画布旋转角度,参数是弧度Math.PI/180*30
            context.beginPath()
            context.strokeStyle=getRandom()
            context.moveTo(0,-188)  //起始位置
            context.lineTo(0,-195)  //结束位置
            context.stroke()        
            context.closePath()
            context.restore()

        }
        //时针
        context.save()
        context.lineWidth=5;
        context.strokeStyle=getRandom()
        context.translate(250,250)
        context.rotate(hour*30*Math.PI/180)
        context.beginPath()
        context.moveTo(0,10)
        context.lineTo(0,-100)
        context.stroke()
        context.closePath()
        context.restore()

        //分针
        context.save()
        context.lineWidth=3;
        context.strokeStyle=getRandom()
        context.translate(250,250)
        context.rotate(minute*6*Math.PI/180)
        context.beginPath()
        context.moveTo(0,15)
        context.lineTo(0,-130)
        context.stroke()
        context.closePath()
        context.restore()

         //秒针
        context.save()
        context.lineWidth=1;
        context.strokeStyle=getRandom()
        context.translate(250,250)
        context.rotate(second*6*Math.PI/180)
        context.beginPath()
        context.moveTo(0,15)
        context.lineTo(0,-170)
        context.stroke()
        context.closePath()
        context.restore()
        
        //表盘中心
        context.beginPath()
        context.lineWidth=1;
        context.fillStyle="red"
        context.arc(250,250,4,0,360,false)
        context.fill()
        context.closePath()

        //绘制文字时间
        context.font="18px 宋体 Bold"
        context.fillStyle=getRandom()
        context.fillText(time,160,150)
        }
        drawClock()
        setInterval(drawClock,1000)

        function getRandom(){
            var col="#";
            for(var i=0;i<6;i++){
              col+=Math.round(Math.random()*16).toString(16)
            }
            return col
        }

    </script>
</body>
</html>


您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/canvas20210602a2.html

网友评论文明上网理性发言 已有0人参与

发表评论: