91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JavaScript如何實現簡單的倒計時效果

發布時間:2022-06-20 09:18:49 來源:億速云 閱讀:131 作者:iii 欄目:開發技術

這篇文章主要介紹了JavaScript如何實現簡單的倒計時效果的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇JavaScript如何實現簡單的倒計時效果文章都會有所收獲,下面我們一起來看看吧。

具體代碼如下

<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="utf8">
        <meta http-equiv="X-UA-Compatible" cotent="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=2.0 initial-scale=1.0, user-scalable=yes">
        <meta name="keywords" content="count down">
        <meta name="description" content="count down">
        <title>倒計時(Count Down)</title>
    </head>
    <body>
        <script>
            var halfPI = Math.PI / 2;
            var doublePI = Math.PI * 2;
            var ctx;

            //畫布大小
            var canvasSize = 200;
            var halfCanvasSize = canvasSize / 2;
            //畫布的外切圓半徑
            var excircleRadius = Math.SQRT2 * halfCanvasSize;
            //線條寬度
            var lineWidth = 2;
            //蒙版顏色
            var maskColor = "rgba(90,90,90,0.4)";
            //單個數字的字體
            var singleNumFont = halfCanvasSize * 1.6 + "px Calibri";
            //兩個數字的字體
            var doubleNumFont = halfCanvasSize * 1.2 + "px Calibri";
            //每秒刷新的幀數
            var stepCount = 30;
            //定時器執行間隔時間
            var interval = 1000 / stepCount;
            //每幀前進的弧度
            var radianStep = doublePI / stepCount;
            //定時器id
            var intervalId = 0;
            //倒計時
            var countdownTime = 0;
            //當前角度
            var radian = 0;

            function init() {
                var canvas = document.createElement("canvas");
                canvas.innerHTML = "Your browser can not support canvas";
                canvas.width = canvasSize;
                canvas.height = canvasSize;
                canvas.style.backgroundColor = "darkkhaki";
                canvas.style.display = "block";

                var restartBtn = document.createElement("button");
                restartBtn.innerHTML = "restart";
                //注意:canvas的width不能寫單位, 而按鈕的width必須寫單位, 否則無效
                restartBtn.style.width = "200px";
                restartBtn.addEventListener("click", function() {
                    restart();
                });

                document.body.appendChild(canvas);
                document.body.appendChild(restartBtn);

                ctx = canvas.getContext("2d");
                ctx.lineWidth = lineWidth;
                ctx.textAlign = "center";
                ctx.textBaseline = "middle";
                restart();
            }

            function strokeLine(x1, y1, x2, y2) {
                ctx.beginPath();
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
                ctx.stroke();
            }

            function strokeCircle(cx, cy, r) {
                ctx.beginPath();
                ctx.arc(cx, cy, r, 0, doublePI);
                ctx.stroke();
            }

            //渲染背景
            function renderBg() {
                ctx.clearRect(0, 0, canvasSize, canvasSize);
                //繪制橫豎線
                ctx.strokeStyle = "black";
                strokeLine(halfCanvasSize, 0, halfCanvasSize, canvasSize);
                strokeLine(0, halfCanvasSize, canvasSize, halfCanvasSize);
                //繪制兩個內圈
                ctx.strokeStyle = "Silver";
                strokeCircle(halfCanvasSize, halfCanvasSize, halfCanvasSize * 0.9);
                strokeCircle(halfCanvasSize, halfCanvasSize, halfCanvasSize * 0.7);
            }

            //渲染蒙版
            function renderMask(radian) {
                //繪制扇形的邊界線
                var x = halfCanvasSize + excircleRadius * Math.sin(radian); 
                var y = halfCanvasSize - excircleRadius * Math.cos(radian);
                ctx.strokeStyle = "black";
                strokeLine(halfCanvasSize, halfCanvasSize, x, y);
                //繪制扇形
                ctx.fillStyle = maskColor;
                ctx.beginPath();
                ctx.moveTo(halfCanvasSize, halfCanvasSize);
                ctx.arc(halfCanvasSize, halfCanvasSize, excircleRadius, -halfPI, -halfPI + radian);
                ctx.closePath();
                ctx.fill();
            }

            //渲染數字
            function renderNum(num) {
                ctx.fillStyle = "black";
                ctx.font = num < 10 ? singleNumFont : doubleNumFont;
                ctx.fillText(num, halfCanvasSize, halfCanvasSize);
            }

            //渲染
            function render() {
                renderBg();
                renderMask(radian);
                renderNum(countdownTime);
            }

            //更新
            function update() {
                radian += radianStep;
                if(radian >= (doublePI + radianStep)) {
                    radian = 0;
                    countdownTime--;
                }
                if(countdownTime < 0) {
                    stop();
                }
            }

            //循環
            function loop() {
                render();
                update();
            }

            //停止
            function stop() {
                if(intervalId === 0) {
                    return;
                }
                clearInterval(intervalId);
                intervalId = 0;

            }

            //開始倒計時
            function restart() {
                stop();
                countdownTime = 10;
                radian = 0;
                intervalId = setInterval(loop, interval);
            }

            init();
        </script>
    </body>
</html>

JavaScript如何實現簡單的倒計時效果

關于“JavaScript如何實現簡單的倒計時效果”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“JavaScript如何實現簡單的倒計時效果”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

库伦旗| 八宿县| 平陆县| 邮箱| 镇江市| 迭部县| 苗栗市| 五家渠市| 沁水县| 金昌市| 闸北区| 沁源县| 澄迈县| 南昌市| 喀喇| 山东| 红安县| 宁南县| 墨江| 清涧县| 西青区| 庄浪县| 玛多县| 道孚县| 祥云县| 安乡县| 浦城县| 禄劝| 浦北县| 长泰县| 卢龙县| 河北省| 新田县| 呼伦贝尔市| 屏东市| 华阴市| 辽宁省| 苍梧县| 华池县| 苗栗市| 原平市|