canvas实现点状数字时间时钟效果代码

代码语言:html

所属分类:其他

代码描述:canvas实现点状数字时间时钟效果代码

代码标签: canvas 点状 数字 时钟 时间

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

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

<head>

    <meta charset="UTF-8">




    <style>
        html,
        body {
          height: 100vh;
          width: 100vw;
        }
        
        body,
        .root,
        .app {
          position: relative;
        }
        
        body,
        .root {
          overflow: hidden;
        }
        
        .root {
          height: 100%;
          width: 100%;
          display: grid;
          place-items: center;
          background: #101219;
        }
        
        .root > .app {
          height: 100%;
          width: 100%;
        }
    </style>



</head>

<body>
    <div id="root" class="root"></div>


    <script >
        "use strict";
        window.addEventListener('load', onLoad);
        /**
         * It creates a canvas element and appends it to the parent element. It also returns a context and a
         * function that resizes the canvas to the size of the parent element
         * @param {HTMLElement} parent - The parent element where the canvas will be appended to.
         * @returns {[Context, ResizeCanvas]} The canvas context and a function that resizes the canvas to the size of the parent
         * element.
         */
        function createCanvas(parent) {
            /**
             * It resizes the canvas to the size of the parent element.
             * @returns {Point} The canvas dimensions.
             */
            function resizeCanvas() {
                const { clientHeight, clientWidth } = parent;
                canvas.height = clientHeight;
                canvas.width = clientWidth;
                return [clientWidth, clientHeight];
            }
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            resizeCanvas();
            parent.appendChild(canvas);
            return [ctx, resizeCanvas];
        }
        /**
         * It creates a context function object that has the functions save, draw and clear.
         * @param {Context} ctx - Context - The context of the canvas.
         * @returns {ContextFunctions} `ContextFunctions` object. {@link ContextFunctions}
         */
        function getContextFunctions(ctx) {
            /**
             * It saves the current state of the canvas and then calls the callback function.
             * @param callback - A function that will be called after the context is saved.
             * @returns {void} None
             */
            function save(callback) {
                ctx.save();
                callback();
                ctx.restore();
            }
            /**
             * It draws a shape.
             * @param callback - A function that will be called to draw the shape.
             * @returns {void} None
             */
            function draw(callback) {
                ctx.beginPath();
                callback();
                ctx.closePath();
            }
            /**
             * Clear the canvas by clearing the rectangle that represents it
             * @returns {void} None
             */
            function clear() {
                const { width, height } = ctx.canvas;
                ctx.clearRect(0, 0, width, height);
            }
            return {
                save,
                draw,
                clear
            };
        }
        function dotMatrixDigit(n) {
            function createZero() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((y === 0 || y === 4) || (x === 0 || x === 2))
                            row.push(1);
                        else
                            row.push(0);
                    }
                    values.push(row);
                }
                return values;
            }
            function createOne() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1)
                        row.push(x === 2 ? 1 : 0);
                    values.push(row);
                }
                return values;
            }
            function createTwo() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((y % 2 === 0) ||
                            (y === 1 && x === 2) ||
                            (y === 3 && x === 0))
                            row.push(1);
                        else
                            row.push(0);
                    }
                    values.push(row);
                }
                return values;
            }
            function createThree() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((y % 2 === 0) || (x === 2))
                            row.push(1);
                        else
                            row.push(0);
                    }
                    values.push(row);
                }
                return values;
            }
            function createFour() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((x === 2) ||
                            (y === 2) ||
                            (y < 3 && x === 0))
                            row.push(1);
                        else
                            row.push(0);
                    }
                    values.push(row);
                }
                return values;
            }
            function createFive() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((y % 2 === 0) ||
                            (y === 3 && x === 2) ||
                            (y === 1 && x === 0))
                            row.push(1);
                        else
                            row.push(0);
                    }
                    values.push(row);
                }
                return values;
            }
            function createSix() {
                const values = [];
                for (let y = 0; y < 5; y += 1) {
                    const row = [];
                    for (let x = 0; x < 3; x += 1) {
                        if ((y % 2 === 0) ||
                            (y === 1 && x === 0) ||
                            (y === 3 && (x === 0 || x === 2)))
                            row.push(1);
                        else
                            row..........完整代码请登录后点击上方下载按钮下载查看

网友评论0