• Html -- 文字时钟


    Html – 文字时钟

    文字时钟,之前在Android上实现了相关效果,闲来无事,弄个网页版的玩玩。。。
    
    • 1

    在这里插入图片描述

    直接上代码:
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Text Clocktitle>
    
        <style type="text/css">
            html, body {
                margin: 0;
                padding: 0;
                background: black;
            }
    
            .root {
                display: flex;
                justify-content: center;
            }
    
            .container {
                display: block;
                background: black;
            }
        style>
    head>
    <body>
    <div class="root">
        <canvas id="container" class="container">canvas>
    div>
    
    <script type="text/javascript">
        const HOURS = [
            "一点", "二点", "三点", "四点", "五点", "六点", "七点", "八点", "九点", "十点", "十一点", "十二点"
        ];
        const MINUTES = [
            "一分", "二分", "三分", "四分", "五分", "六分", "七分", "八分", "九分", "十分",
            "十一分", "十二分", "十三分", "十四分", "十五分", "十六分", "十七分", "十八分", "十九分", "二十分",
            "二十一分", "二十二分", "二十三分", "二十四分", "二十五分", "二十六分", "二十七分", "二十八分", "二十九分", "三十分",
            "三十一分", "三十二分", "三十三分", "三十四分", "三十五分", "三十六分", "三十七分", "三十八分", "三十九分", "四十分",
            "四十一分", "四十二分", "四十三分", "四十四分", "四十五分", "四十六分", "四十七分", "四十八分", "四十九分", "五十分",
            "五十一分", "五十二分", "五十三分", "五十四分", "五十五分", "五十六分", "五十七分", "五十八分", "五十九分", ""
        ];
        const SECONDS = [
            "一秒", "二秒", "三秒", "四秒", "五秒", "六秒", "七秒", "八秒", "九秒", "十秒",
            "十一秒", "十二秒", "十三秒", "十四秒", "十五秒", "十六秒", "十七秒", "十八秒", "十九秒", "二十秒",
            "二十一秒", "二十二秒", "二十三秒", "二十四秒", "二十五秒", "二十六秒", "二十七秒", "二十八秒", "二十九秒", "三十秒",
            "三十一秒", "三十二秒", "三十三秒", "三十四秒", "三十五秒", "三十六秒", "三十七秒", "三十八秒", "三十九秒", "四十秒",
            "四十一秒", "四十二秒", "四十三秒", "四十四秒", "四十五秒", "四十六秒", "四十七秒", "四十八秒", "四十九秒", "五十秒",
            "五十一秒", "五十二秒", "五十三秒", "五十四秒", "五十五秒", "五十六秒", "五十七秒", "五十八秒", "五十九秒", ""
        ];
    
        const INTERVAL_UPDATE = 1000;//时间刷新间隔
        const mColorCenetr = "#ffffff";//中心字体颜色
        const mColorCur = "#ff0000";//选中字体颜色
        const mColorCom = "#bdbdbd";//基础色
        const mColorBg = "#000000";//背景颜色
        const mFont = "华文行楷";//字体
    
        var mWidth, mHeight;//宽高
        var mH, mM, mS;//当前的时分秒
        var mH24;//24制小时
        var mWeek, mDate, mTime;//星期,日期,时间
        var mRadiusH, mRadiusM, mRadiusS;//三个同心圆的半径
        var mDegreeH, mDegreeM, mDegreeS;//时分秒旋转角度
        var mTextSizeCom;//通用字体大小
        var mTextSizeCenter;//中间字体大小
        var mOffset;//中间文字行间隔
        var mCenterHeight;//文字纵向居中高度
    
        //
        var canvas;
        var cxt;
        var hd, md, sd, av;
    
        window.onload = function () {
            initial();
        }
    
        //适应屏幕大小变化
        window.onresize = function () {
            calcSize();
        }
    
        function initial() {
            canvas = document.getElementById("container");
            cxt = canvas.getContext("2d");
            calcSize();
            curTime();
            startTask();
        }
    
        function calcSize() {
            mWidth = window.innerWidth;
            mHeight = window.innerHeight;
            //确保为正方形
            mWidth = mHeight = Math.min(mWidth, mHeight);
            canvas.width = mWidth;
            canvas.height = mHeight;
            // console.log(mWidth + " - " + mHeight);
    
            //字体大小
            mTextSizeCom = mWidth / 50;//通用字体大小
            mTextSizeCenter = mWidth / 35;//中间字体大小
            mOffset = mTextSizeCenter / 2;//中间文字间隔
            mCenterHeight = (mHeight - mTextSizeCom) / 2;
    
            //各圆半径
            mRadiusH = mWidth / 6;
            mRadiusM = mRadiusH + 4 * mTextSizeCom;//3+1
            mRadiusS = mRadiusM + 5 * mTextSizeCom;//4+1
            // console.log(mRadiusH + " - " + mRadiusM + " - " + mRadiusS);
        }
    
        function startTask() {
            setInterval(function () {
                curTime();
                //内嵌一个有限循环
                var times = 0;
                var interval = setInterval(function () {
                    if (times > 6) {
                        clearInterval(interval);
                    } else {
                        doAnimation();
                        times++;
                    }
                }, 10)
            }, INTERVAL_UPDATE);
        }
    
        //自定义线性循环动画
        function doAnimation() {
            //+ -> 顺时针; - -> 逆时针
            av = av - 0.9;//总偏移量/次数
    
            if (mM === 0 && mS === 0) {
                mDegreeH = hd + av * 5;//时圈旋转角度是分秒的5倍,线性的旋转30°
            }
    
            if (mS === 0) {
                mDegreeM = md + av;//线性的旋转6°
            }
    
            mDegreeS = sd + av;//线性的旋转6°
            drawCanvas();
        }
    
        //刷新背景
        function updateBg() {
            cxt.fillStyle = mColorBg;
            cxt.fillRect(0, 0, mWidth, mHeight);
        }
    
        //绘制中间时间/日期/星期
        function drawCenterInfo() {
            cxt.save();
            cxt.fillStyle = mColorCenetr;
            cxt.font = mTextSizeCenter + "px " + mFont;
    
            //draw time
            var len = (mTime.length -2) * mTextSizeCenter / 2;//数字大小只有文字的一半 //去掉两个符号
            cxt.fillText(mTime, (mWidth - len) / 2, mHeight / 2 - mTextSizeCenter * 3 / 2 - mOffset);
    
            //draw date
            var len2 = (mDate.length-2) * mTextSizeCenter / 2;//去掉两个符号
            cxt.fillText(mDate, (mWidth - len2) / 2, mHeight / 2 - mTextSizeCenter / 2);
    
            //draw week
            var len3 = mWeek.length * mTextSizeCenter;
            cxt.fillText(mWeek, (mWidth - len3) / 2, mHeight / 2 + mTextSizeCenter / 2 + mOffset);
            cxt.restore();
        }
    
        function drawHour() {
            cxt.save();
            //只能旋转角度,以原点(0,0)
            cxt.translate(mWidth / 2, mHeight / 2);
            cxt.rotate(mDegreeH * Math.PI / 180);//* Math.PI / 180
            cxt.translate(-mWidth / 2, -mHeight / 2);//旋转完之后需要移回去
    
            //时针圈: 360/12 = 30度
            for (var i = 0; i < HOURS.length; i++) {
                cxt.save();
                cxt.translate(mWidth / 2, mHeight / 2);
                cxt.rotate(30 * i * Math.PI / 180);
                cxt.translate(-mWidth / 2, -mHeight / 2);
    
                if (i + 1 === mH || (i === 11 && mH === 0)) {
                    //当前时
                    cxt.fillStyle = mColorCur;
                } else {
                    cxt.fillStyle = mColorCom;
                }
                cxt.font = mTextSizeCom + "px " + mFont;
                cxt.fillText(HOURS[i], mWidth / 2 + mRadiusH, mCenterHeight);
                cxt.restore();
            }
            cxt.restore();
        }
    
        function drawMinute() {
            cxt.save();
            //只能旋转角度,以原点(0,0)
            cxt.translate(mWidth / 2, mHeight / 2);
            cxt.rotate(mDegreeM * Math.PI / 180);//* Math.PI / 180
            cxt.translate(-mWidth / 2, -mHeight / 2);//旋转完之后需要移回去
    
            //分针圈: 360/60 = 6度
            for (var i = 0; i < MINUTES.length; i++) {
                cxt.save();
                cxt.translate(mWidth / 2, mHeight / 2);
                cxt.rotate(6 * i * Math.PI / 180);
                cxt.translate(-mWidth / 2, -mHeight / 2);
    
                if (i + 1 === mM) {
                    //当前时
                    cxt.fillStyle = mColorCur;
                } else {
                    cxt.fillStyle = mColorCom;
                }
                cxt.font = mTextSizeCom + "px " + mFont;
                cxt.fillText(MINUTES[i], mWidth / 2 + mRadiusM, mCenterHeight);
                cxt.restore();
            }
            cxt.restore();
        }
    
        function drawSecond() {
            cxt.save();
            //只能旋转角度,以原点(0,0)
            cxt.translate(mWidth / 2, mHeight / 2);
            cxt.rotate(mDegreeS * Math.PI / 180);//* Math.PI / 180
            cxt.translate(-mWidth / 2, -mHeight / 2);//旋转完之后需要移回去
    
            //秒针圈: 360/60 = 6度
            for (var i = 0; i < MINUTES.length; i++) {
                cxt.save();
                cxt.translate(mWidth / 2, mHeight / 2);
                cxt.rotate(6 * i * Math.PI / 180);
                cxt.translate(-mWidth / 2, -mHeight / 2);
    
                if (i + 1 === mS) {
                    //当前时
                    cxt.fillStyle = mColorCur;
                } else {
                    cxt.fillStyle = mColorCom;
                }
                cxt.font = mTextSizeCom + "px " + mFont;
                cxt.fillText(SECONDS[i], mWidth / 2 + mRadiusS, mCenterHeight);
                cxt.restore();
            }
            cxt.restore();
        }
    
        //刷新画布
        function drawCanvas() {
            updateBg();
            drawCenterInfo();
            drawHour();
            drawMinute();
            drawSecond();
        }
    
        //
        function curTime() {
            let date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();
            let week = date.getDay();
            mH24 = date.getHours();
            mM = date.getMinutes();
            mS = date.getSeconds();
            mH = mH24 >= 12 ? mH24 - 12 : mH24;
    
            mTime = formatTime(mH24) + ':' + formatTime(mM) + ':' + formatTime(mS);
            mDate = year + '/' + formatTime(month) + '/' + formatTime(day);
            mWeek = parseWeek(week);
            calculateDegree();
        }
    
        function calculateDegree() {
            //逆时针旋转
            mDegreeH = -360 / 12 * (mH - 1);
            mDegreeM = -360 / 60 * (mM - 1);
            mDegreeS = -360 / 60 * (mS - 1);
    
            hd = mDegreeH;
            md = mDegreeM;
            sd = mDegreeS;
            av = 6;//每次动画总偏移量
        }
    
        function formatTime(fn) {
            return fn < 10 ? "0" + fn : fn;
        }
    
        function parseWeek(week) {
            var mWeek;
            switch (week) {
                case 1:
                    mWeek = "星期一";
                    break;
                case 2:
                    mWeek = "星期二";
                    break;
                case 3:
                    mWeek = "星期三";
                    break;
                case 4:
                    mWeek = "星期四";
                    break;
                case 5:
                    mWeek = "星期五";
                    break;
                case 6:
                    mWeek = "星期六";
                    break;
                case 0:
                    mWeek = "星期日";
                    break;
            }
    
            return mWeek;
        }
    script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
  • 相关阅读:
    个人博客系统
    小马识途:如何稀释百科词条里的负面信息?
    DRM全解析 —— CRTC详解(4)
    GC暂停时间过长——排查分析
    Spring底层原理学习笔记--第四讲--(常见bean后处理器与@Autowired bean后处理器执行分析)
    应用没“积分”,系统就不让运行?Android 13部分功能曝光
    Python之json模块
    今天运气不错
    基于5G工业CPE打造智慧煤矿无人巡检监测应用
    【IDEA插件】这5款IDEA插件,堪称代码BUG检查神器!
  • 原文地址:https://blog.csdn.net/weixin_43738911/article/details/133948347