温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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