canvas绘制表盘时钟

canvas學習

先上效果圖吧,最後大擺錘有幾點不會實現,就用其他的頂替了一下下,嘿嘿。大擺錘的gif我放到下面了哈
在这里插入图片描述

<div class="container">
    <div class="box">
        <div class="img1"></div>
        <canvas id="canvas" width="375" height="650" style="margin: 0 auto;"></canvas>
        <canvas id="canvas2" width="300" height="200"></canvas>
    </div>
</div>
    // 錶盤半徑
        const R = 150
        // 弧度
        const deg = Math.PI / 180
        // 計時器變量
        let timer = null
        var clockRadius = 250;

        // 獲取canvas對象
        const canvas = document.getElementById('canvas')
        const ctx = canvas.getContext('2d')
        // 園中心的xy坐標
        // const centerPoint = [0, 0]
        const centerPoint = [canvas.width / 2, 185]
        console.log(canvas.width, canvas.height)

        // 設置畫布的位置,重新映射畫布上的 (0,0) 位置
        ctx.translate(centerPoint[0], centerPoint[1])
        // 清除計時器
        timer && clearInterval(timer)
        // 繪製畫布
        drawClock()
        // 更新畫布
        timer = setInterval(() => {
            drawClock()
        }, 1000);
        // 繪製底部時鐘擺錘
        drawBob()



        // 獲取當前時間函數
        function getNowTime() {
            const date = new Date()
            let [h, m, s] = [date.getHours(), date.getMinutes(), date.getSeconds()]
            if (h > 12) {
                h -= 12
            }
            return [h, m, s, date]
        }
        // 清除畫布
        function clearDraw() {
            ctx.clearRect(-centerPoint[0], -centerPoint[1], canvas.width, canvas.height)
        }
        // 繪製時鐘函數
        function drawClock() {
            // 首先清除畫布
            clearDraw()
            // 重新繪製畫布
            // 獲取當前時間
            const [h, m, s, date] = getNowTime()
            // 創建漸變填充
            const my_gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
            my_gradient.addColorStop(0, "#afd");
            my_gradient.addColorStop(1, "#adf");
            ctx.fillStyle = my_gradient;
            // 繪製矩形 開始的xy坐標,寬高
            ctx.fillRect(-centerPoint[0], -centerPoint[1], canvas.width, canvas.height);
            // 繪製圓形,並填充顔色
            ctx.beginPath();
            ctx.arc(0, 0, R, 0, 2 * Math.PI);
            // 創建漸變
            const gradient = ctx.createRadialGradient(0, 0, R / 6, 0, 0, R);
            // gradient.addColorStop("0", "magenta");
            gradient.addColorStop("0.5", "pink");
            gradient.addColorStop("1.0", "white");
            ctx.fillStyle = gradient
            ctx.fill();
            // 繪製園中心(指針中心)
            ctx.beginPath()
            ctx.arc(0, 0, 5, 0, 2 * Math.PI)
            ctx.fillStyle = '#000'
            ctx.fill()
            // 繪製錶盤刻度
            drawScale()
            // 绘制表盘刻度数字
            drawNum()
            // 繪製日期文字
            drawDate(date)
            // 繪製指針 時針
            drawPointer(.4, 5, 'black', h * 5)
            // 繪製指針 分針
            drawPointer(.46, 4, 'green', m)
            // 繪製指針 秒針
            drawPointer(.52, 3, 'red', s)
        }
        // 繪製錶盤刻度函數
        function drawScale() {
            for (let i = 1; i <= 60; i++) {
                ctx.beginPath();
                ctx.save();
                ctx.rotate(6 * i * deg);
                ctx.moveTo(0, -150);
                // 區分刻度
                if (i % 15 == 0) {
                    ctx.strokeStyle = '#afd';
                    ctx.lineWidth = 3;
                    ctx.lineTo(0, -135);
                } else if (i % 5 == 0) {
                    ctx.strokeStyle = '#adf';
                    ctx.lineWidth = 2;
                    ctx.lineTo(0, -140);
                } else {
                    ctx.strokeStyle = '#000';
                    ctx.lineWidth = 1;
                    ctx.lineTo(0, -145);
                }
                ctx.stroke();
                ctx.restore();
                ctx.closePath()
            }
        }
        // 绘制表盘刻度数字函数
        function drawNum() {
            ctx.save()
            // 創建漸變
            const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
            gradient.addColorStop("0", "magenta");
            gradient.addColorStop("0.5", "blue");
            gradient.addColorStop("1.0", "red");
            // 設置字體
            ctx.font = '14px Arial';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';

            for (let n = 1; n <= 12; n++) {
                const theta = (n - 3) * (Math.PI * 2) / 12;
                if (n % 3 === 0) {
                    ctx.fillStyle = gradient;
                } else {
                    ctx.fillStyle = '#000';
                }
                const x = clockRadius * 0.5 * Math.cos(theta);
                const y = clockRadius * 0.5 * Math.sin(theta);
                ctx.fillText(n, x, y);
            }
            ctx.restore()
        }
        // 绘制指针
        function drawPointer(longRate, width, color, i = 0) {
            ctx.save();
            ctx.rotate((6 * i - 90) * deg);
            ctx.beginPath();
            ctx.fillStyle = color;
            ctx.moveTo(-15, -width);
            ctx.lineTo(-15, width);
            ctx.lineTo(clockRadius * longRate, 1);
            ctx.lineTo(clockRadius * longRate, -1);
            ctx.fill();
            ctx.restore();
        }
        // 繪製日期函數
        function drawDate(date) {
            ctx.save()
            ctx.fillStyle = '#fff'
            ctx.lineJoin = "round";
            ctx.lineWidth = 140
            ctx.fillRect(-55, 55, 110, 30)
            // 創建漸變
            const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
            gradient.addColorStop("0", "magenta");
            gradient.addColorStop("0.5", "blue");
            gradient.addColorStop("1.0", "red");
            // 設置字體
            ctx.font = '14px Arial';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillStyle = gradient
            ctx.fillText(dateFormatter(date, 'yyyy年MM月dd日'), 0, 70)
            ctx.restore()
        }
        // 繪製底部時鐘擺錘函數
        function drawBob() {
            const canvas = document.getElementById('canvas2')
            const ctx = canvas.getContext('2d')
            // 設置畫布的位置,重新映射畫布上的 (0,0) 位置
            // ctx.translate(centerPoint[0], centerPoint[1])
            ctx.save()
            // 繪製方形空洞用來存放擺錘
            // 創建漸變
            const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
            gradient.addColorStop("0", "rgba(255, 0, 0, .3)");
            gradient.addColorStop("0.5", "rgba(0, 255, 0, .3)");
            gradient.addColorStop("1.0", "rgba(0, 0, 255, .3)");
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, R * 2, 200)
            // 绘制摆锤

            // 绘制方形玻璃遮罩
            // ...
            ctx.restore()
        }

        // 日期格式化
        function dateFormatter(str, fmt = 'yyyy-MM-dd hh:mm:ss') {
            const date = new Date(str || Date.now());
            const map = {
                'y+': date.getFullYear(),
                'M+': date.getMonth() + 1,
                'd+': date.getDate(),
                'h+': date.getHours(),
                'm+': date.getMinutes(),
                's+': date.getSeconds()
            };
            for (const key in map) {
                if (Object.prototype.hasOwnProperty.call(map, key)) {
                    fmt = fmt.replace(new RegExp(key), (a, b, c) => {
                        // a: 匹配到的字符串,b:index索引,c:模板字符串
                        // console.log(a, b, c)
                        const target = String(map[key]);
                        if (a.length < target.length && a.length >= 2) {
                            return target.substr(-a.length);
                        }
                        return target.padStart(a.length, '0');
                    });
                }
            }
            return fmt;
        }
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
            /* background: #cccccc; */
        }

        .container {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .box {
            position: relative;
            margin: 0 auto;
        }

        #canvas2 {
            position: absolute;
            bottom: 30px;
            left: 0;
            right: 0;
            margin: 0 auto;
        }

        .img1 {
            width: 300px;
            height: 200px;
            position: absolute;
            bottom: 30px;
            left: 0;
            right: 0;
            margin: 0 auto;
            background-size: cover;
            background-position-x: -10px;
            background-image: url(./a.gif);
        }

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面是一个简单的HTML5 Canvas绘制时钟的示例代码: ``` <!DOCTYPE html> <html> <head> <title>Canvas时钟</title> <style> canvas { border: 2px solid black; } </style> </head> <body> <canvas id="clock" width="200" height="200"></canvas> <script> function drawClock() { // 获取Canvas元素 var canvas = document.getElementById("clock"); var ctx = canvas.getContext("2d"); // 获取Canvas中心点坐标 var centerX = canvas.width / 2; var centerY = canvas.height / 2; // 绘制表盘 ctx.beginPath(); ctx.arc(centerX, centerY, 80, 0, 2 * Math.PI); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制刻度 for (var i = 1; i <= 12; i++) { var angle = i * Math.PI / 6; var x = centerX + 60 * Math.sin(angle); var y = centerY - 60 * Math.cos(angle); ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI); ctx.fillStyle = "black"; ctx.fill(); } // 绘制时针 var now = new Date(); var hour = now.getHours() % 12; var minute = now.getMinutes(); var second = now.getSeconds(); var hourAngle = (hour + minute / 60) * Math.PI / 6; var hourX = centerX + 40 * Math.sin(hourAngle); var hourY = centerY - 40 * Math.cos(hourAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(hourX, hourY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制分针 var minuteAngle = minute * Math.PI / 30; var minuteX = centerX + 60 * Math.sin(minuteAngle); var minuteY = centerY - 60 * Math.cos(minuteAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(minuteX, minuteY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制秒针 var secondAngle = second * Math.PI / 30; var secondX = centerX + 60 * Math.sin(secondAngle); var secondY = centerY - 60 * Math.cos(secondAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(secondX, secondY); ctx.strokeStyle = "red"; ctx.stroke(); } // 每秒钟更新时钟 setInterval(drawClock, 1000); </script> </body> </html> ``` 这段代码使用Canvas绘制了一个时钟,并且每秒钟更新一次。你可以将代码复制到一个HTML文件中运行,就可以看到时钟的效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值