h5:canvas绘制时钟

目录

一:顿一下 

 二:平滑


一:顿一下 

 

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        .demo {
            margin: 20px auto;
            display: block;
            border: 5px solid pink;
            border-radius: 50px;
        }
    </style>

</head>

<body>
    <canvas id="app" class="demo" width="600" height="600"></canvas>
    <script>

        //创建图片加载器
        var img = new Image();

        //监听图片加载完成
        img.onload = function () {
            var canvas = document.getElementById('app');//获取canvas
            var ctx = canvas.getContext('2d');//获取上下文对象
            ctx.translate(canvas.width / 2, canvas.height / 2);//调整坐标系
            ctx.drawImage(img, -img.width / 2, -img.height / 2)//绘制图片
            //绘制中心点
            ctx.arc(0, 0, 10, 0, 2 * Math.PI, false);
            ctx.fillStyle = 'red';
            ctx.fill();

            //绘制静态的时分秒指针
            line(0, 20, 0, -190, 'green', 5);//s
            line(0, 20, 0, -150, 'blue', 7);//m
            line(0, 20, 0, -110, 'hotpink', 9);//h

            // 方案一:各指针跳转执行
            var deg = Math.PI / 30; //秒:每秒6度
            setInterval(function () {
                var data = new Date();//获取时间
                var second = data.getSeconds();//获取时分秒
                var minute = data.getMinutes();
                var hour = data.getHours();

                //清除画布
                ctx.clearRect(-ctx.width / 2, -ctx.height / 2, canvas.width, canvas.height)
                ctx.drawImage(img, -img.width / 2, -img.height / 2); //绘制图片

                ctx.save(); //旋转之前存储状态/
                ctx.rotate(deg * second); //假设5s/先写静态的查看各指针的指位
                line(0, 20, 0, -190, 'green', 5);//s
                ctx.restore();// 恢复状态

                ctx.save(); //旋转之前存储状态
                ctx.rotate(deg * minute);//假设10m
                line(0, 20, 0, -150, 'blue', 7);//m
                ctx.restore();// 恢复状态

                ctx.save(); //旋转之前存储状态
                ctx.rotate(deg * 5 * hour);//假设5h
                line(0, 20, 0, -110, 'hotpink', 9);//h
                ctx.restore();// 恢复状态
            }, 100) //每秒执行5次,保证时间准确
     };
        //加载图片
        img.src = './clock.png';

        function line(x, y, endX, endY, color, num) {  //画一条直线
            var canvas = document.getElementById('app');//获取canvas
            var ctx = canvas.getContext('2d');//获取上下文对象
            ctx.beginPath();//2.启动绘制
            ctx.moveTo(x, y);//3.去画一个起点
            ctx.lineTo(endX, endY); //4.绘制线
            ctx.closePath(); //5.结束绘制
            ctx.strokeStyle = color;//7.更改描边颜色
            ctx.lineWidth = num;//8.绘制线粗
            ctx.stroke();  //6.描边
            //注意:改变颜色如果写在描边,之后,则不能上色,就像画画一样,已经华成默认黑了
        }
    </script>
</body>

</html>

 

二:平滑

 

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        .demo {
            margin: 20px auto;
            display: block;
            border: 5px solid pink;
            border-radius: 50px;
        }
    </style>

</head>

<body>
    <canvas id="app" class="demo" width="600" height="600"></canvas>
    <script>

        //创建图片加载器
        var img = new Image();

        //监听图片加载完成
        img.onload = function () {
            var canvas = document.getElementById('app');//获取canvas
            var ctx = canvas.getContext('2d');//获取上下文对象
            ctx.translate(canvas.width / 2, canvas.height / 2);//调整坐标系
            ctx.drawImage(img, -img.width / 2, -img.height / 2)//绘制图片
            //绘制中心点
            ctx.arc(0, 0, 10, 0, 2 * Math.PI, false);
            ctx.fillStyle = 'red';
            ctx.fill();

            //绘制静态的时分秒指针
            line(0, 20, 0, -190, 'green', 5);//s
            line(0, 20, 0, -150, 'blue', 7);//m
            line(0, 20, 0, -110, 'hotpink', 9);//h

            //方案二:各指针平滑旋转
            var deg = Math.PI / 30; //秒:每秒6度
            var now = Date.now();//获取时间戳
            setInterval(function () {
                var now = Date.now();//注意:这里是东0区的时间
                var second = now / 1000;//s//获取时分秒
                var minute = second / 60;
                var hour = minute / 60;
                second %= 60;
                minute %= 60;
                // (0 - new Date().getTimezoneOffset() / 60)获取当前时区
                hour = hour % 24 + (0 - new Date().getTimezoneOffset() / 60);//加到东八区
                //清除画布
                ctx.clearRect(-ctx.width / 2, -ctx.height / 2, canvas.width, canvas.height)
                ctx.drawImage(img, -img.width / 2, -img.height / 2); //绘制图片

                ctx.save(); //旋转之前存储状态/
                ctx.rotate(deg * second); //假设5s/先写静态的查看各指针的指位
                line(0, 20, 0, -190, 'green', 5);//s
                ctx.restore();// 恢复状态

                ctx.save(); //旋转之前存储状态
                ctx.rotate(deg * minute);//假设10m
                line(0, 20, 0, -150, 'blue', 7);//m
                ctx.restore();// 恢复状态

                ctx.save(); //旋转之前存储状态
                ctx.rotate(deg * 5 * hour);//假设5h
                line(0, 20, 0, -110, 'hotpink', 9);//h
                ctx.restore();// 恢复状态
            }, 100) //每秒执行5次,保证时间准确

        };
        //加载图片
        img.src = './clock.png';

        function line(x, y, endX, endY, color, num) {  //画一条直线
            var canvas = document.getElementById('app');//获取canvas
            var ctx = canvas.getContext('2d');//获取上下文对象
            ctx.beginPath();//2.启动绘制
            ctx.moveTo(x, y);//3.去画一个起点
            ctx.lineTo(endX, endY); //4.绘制线
            ctx.closePath(); //5.结束绘制
            ctx.strokeStyle = color;//7.更改描边颜色
            ctx.lineWidth = num;//8.绘制线粗
            ctx.stroke();  //6.描边
            //注意:改变颜色如果写在描边,之后,则不能上色,就像画画一样,已经华成默认黑了
        }
    </script>
</body>

</html>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值