html5 canvas 实现倒计时 功能

   2023-02-08 学习力0
核心提示:function showTime(a) {var b = {id: "showtime", //canvasidx: 60, //中心点坐标 X轴;y: 60, //中心点坐标 Y轴;radius: 60, //圆的半径angle: 0, //角度 无需设置linewidth: 6, //线的宽度backround: "#d65554", //倒计时背景色color: "#e4e4e4", //填充色da
        function showTime(a) {
                var b = {
                    id: "showtime", //canvasid
                    x: 60, //中心点坐标 X轴;
                    y: 60, //中心点坐标 Y轴;
                    radius: 60, //圆的半径
                    angle: 0, //角度 无需设置
                    linewidth: 6, //线的宽度
                    backround: "#d65554", //倒计时背景色
                    color: "#e4e4e4", //填充色
                    day: 0,
                    time: 0,
                    minute: 0,
                    seconds: 0
                }
          //若参数有更新则合并
if (a) { b = $.extend(b, a); } this.total = 0; this.id = b.id; this.x = b.x; this.y = b.y; this.radius = b.radius; this.angle = b.angle; this.linewidth = b.linewidth; this.backround = b.backround; this.color = b.color; this.time = b.time; this.day = b.day; this.minute = b.minute; this.seconds = b.seconds; var canvas = document.getElementById(this.id); if (canvas == null) { return false; } var ctx = canvas.getContext("2d"); this.creatText = function() { ctx.fillStyle = "#000"; ctx.font = "13px Arial"; ctx.fillText("剩余时间", 32, 38); ctx.fillStyle = "#333"; if (Number(this.day) > 0) { ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.day), 13, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("天", 38, 75); ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.time), 58, 75); ctx.font = "14px Arial"; ctx.fillStyle = "#333"; ctx.fillText("小时", 82, 75); } else if (Number(this.time) > 0) { ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.time), 11, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("小时", 33, 75); ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.minute), 61, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("分钟", 84, 75); } else if (Number(this.minute) > 0) { ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.minute), 13, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("分钟", 35, 75); ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.seconds), 65, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("秒", 90, 75); } else if (Number(this.seconds) > 0) { ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr(this.seconds), 40, 75); ctx.font = "13px Arial"; ctx.fillStyle = "#333"; ctx.fillText("秒", 65, 75); }else{ ctx.font = "22px Arial"; ctx.fillStyle = "#000"; ctx.fillText(getStr("00:00"), 31, 75); } function getStr(num) { return num.toString().length < 2 ? "0" + num : num; } }, showTime.prototype.creatEle = function() { var _w = canvas.getAttribute("width"); var _h = canvas.getAttribute("height"); ctx.clearRect(0, 0, _w, _h); //清楚canva绘制区域 ctx.save(); ctx.restore(); ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(-Math.PI / 2) if(this.angle == 360){ ctx.fillStyle = this.color; }else{ ctx.fillStyle = this.backround; } ctx.beginPath(); ctx.arc(0, 0, this.radius-0.5, Math.PI / 180 * 0, Math.PI * 2, true); ctx.lineTo(0, 0); ctx.closePath(); ctx.fill(); ctx.restore(); ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(-Math.PI / 2) ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(0, 0, this.radius, Math.PI / 180 * this.angle, Math.PI * 2, true); ctx.lineTo(0, 0); ctx.closePath(); ctx.fill(); ctx.restore(); ctx.save(); ctx.beginPath(); var linew = this.radius - this.linewidth; ctx.arc(this.x, this.y, linew, 0, 2 * Math.PI, true); ctx.closePath(); ctx.fillStyle = 'white'; ctx.fill(); ctx.restore(); this.creatText(); }; this.creatEle(); } var countdown = function(startTime, lastTime, nowTime, step) { this.startTime = startTime; //服务器开始时间 this.lastTime = lastTime; //服务器到期时间 this.nowTime = nowTime; //服务器当前时间 this.alltime = this.lastTime - this.startTime; //总时间段 this.step = step * 1000; //执行的阶段时间,一般是1秒 }; countdown.prototype = { atTime: function(a, b) { //参数说明:a:到期回调方法,b:倒计时回调方法 var that = this; //var timeold = parseFloat(Number(that.lastTime) - Number(that.startTime)); var timeold = that.lastTime - that.nowTime; var msPerDay = 24 * 60 * 60 * 1000; var e_daysold = timeold / msPerDay; var daysold = Math.floor(e_daysold); // var e_hrsold = (e_daysold - daysold) * 24; var hrsold = Math.floor(e_hrsold); //小时 var e_minsold = (e_hrsold - hrsold) * 60; var minsold = Math.floor((e_hrsold - hrsold) * 60); //分钟 var seconds = Math.round((e_minsold - minsold) * 60); // var msSeconds = Math.ceil(Math.round(((e_minsold - minsold) * 60 - seconds) * 1000) / 100) * 10; var totaltime = that.lastTime - that.nowTime; var timeangle = 360 - totaltime / (that.alltime / 360); if (msSeconds == 100) { msSeconds = 99; } if (that.nowTime > that.lastTime) { arguments[0](); } else { arguments[1](that.getStr(daysold), that.getStr(hrsold), that.getStr(minsold), that.getStr(seconds), that.getStr(msSeconds),Math.floor(timeangle)); that.nowTime = parseInt(that.nowTime) + that.step; window.setTimeout(function() { that.atTime(a,b); }, that.step); } }, getStr: function(num) { return num.toString().length < 2 ? "0" + num : num; } }; $(function() { var startTime = 1437765600000; //开始时间 var lastTime = 1437766880000; //结束时间 var nowTime = 1437766850000; //服务器当前时间 var showtime = new countdown(startTime, lastTime, nowTime,1) showtime.atTime(function(){},function(){ var one = new showTime({ id:"showtime", day:arguments[0], time:arguments[1], minute:arguments[2], seconds:arguments[3], angle:arguments[5] }) one.creatEle(); });       })

以上代码为一个  canvas的倒计时功能,拷贝粘贴即可使用,

使用方法如下:

            $(function() {
                var startTime = 1437765600000; //开始时间
                var lastTime =  1437766880000; //结束时间
                var nowTime =   1437766850000; //服务器当前时间
                var showtime = new countdown(startTime, lastTime, nowTime,1)
                showtime.atTime(function(){},function(){
                var one = new showTime({
                        id:"showtime",
                        day:arguments[0],
                        time:arguments[1],
                        minute:arguments[2],
                        seconds:arguments[3],
                        angle:arguments[5]
                    })
                    one.creatEle();
                });
      })

详细设置参数如下:

                    id: "showtime", //canvasid
                    x: 60, //中心点坐标 X轴;
                    y: 60, //中心点坐标 Y轴;
                    radius: 60, //圆的半径
                    angle: 0, //角度 无需设置
                    linewidth: 6, //线的宽度
                    backround: "#d65554", //倒计时背景色
                    color: "#e4e4e4", //填充色
                    day: 0, 
                    time: 0,
                    minute: 0,
                    seconds: 0

实现效果如下:html5 canvas  实现倒计时 功能 不满一天,显示,小时/分钟,不满一小时显示  分钟/秒  不满一分钟显示 / 秒,不满一秒显示 00:00

 

代码逻辑比较简单,当然还有很大的优化空间,比较忙暂不做优化,希望能对后来者有所帮助吧。

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • HTML中将背景颜色渐变 html设置背景颜色渐变
    通过使用 css3 渐变可以让背景两个或多个指定的颜色之间显示平稳的过渡,由于用到css3所以需要考虑下浏览器兼容问题,例如:从左到右的线性渐变,且带有透明度的样式:#grad {background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*
    03-08
  • html5 Canvas 如何自适应屏幕大小
    但是这样创建出的画布不能随着浏览器窗口大小的改变而动态的改变画布的大小。而这一点往往又非常重要, 因为我们会经常改变浏览器窗口大小,不会一直保持某个固定的大小。 html代码 canvas width="300" height="300" id="myCanvas"/canvas设置样式 * {
    03-08
  • Vue中出现Do not use built-in or reserved HTML elements as component id:footer等等vue warn问题
    Vue中出现Do not use built-in or reserved HTM
    错误示图:原因:是因为在本地项目对应文件的script中,属性name出现了错误的命名方式,导致浏览器控制台报错!  诸如: name: header 、  、 name: menu , 等等都属于错误的命名方式等 错误代码命名如下:解决办法:办法1: 如果我们采用正确命名
    03-08
  • HTML在网页中插入音频视频简单的滚动效果
    HTML在网页中插入音频视频简单的滚动效果
    每次上网,打开网页后大家都会看到在网页的标签栏会有个属于他们官网的logo,现在学了HTML了,怎么不会制作这个小logo呢,其实很简单,也不需要死记硬背,每当这行代码出现的时候能知道这是什么意思就ok1 link rel="shortcuticon" type="image/x-icon" href="
    03-08
  • HTML的video标签,不能下载视频代码
    !-- 在线视频不能下载代码 --!DOCTYPE html html headscript src="../Demo/demo/book/JQuery/jQuery v2.2.0.js"/script/headbody div style="text-align:center;"video src="../images/PreviewVideo.mp4" width="820"controls="controls&
    03-08
  • ThinkPHP报错 The requested URL /admin/index/login.html was not found on this server.
    ThinkPHP报错 The requested URL /admin/index/
           解决方案在入口文件夹public下查看.htaccess是否存在。不存在则新建,存在的话,那内容替换为下面这串代码 就可以解决Not Fund#IfModule mod_rewrite.c#Options +FollowSymlinks -Multiviews#RewriteEngine On##RewriteCond %{REQUEST_FILENAME
    03-08
  • HTML特殊字符、列表、表格总结 html特殊符号对
            HTML实体字符  在HTML中一些特殊的字符需要用特殊的方式才能显示出来,比如小于号、版权等,  在课堂上老师教了我们一个有点意思的:空格,在教材上字符实体是“nbsp”通过老师  的演示我们发现不同的浏览器他所显示的效果不同,有的比
    03-08
  • 【JavaScript】使用document.write输出覆盖HTML
    您只能在 HTML 输出中使用 document.write。如果您在文档加载后使用该方法,会覆盖整个文档。分析HTML输出流是指当前数据形式是HTML格式的数据,这部分数据正在被导出、传输或显示,所以称为“流”。通俗的来说就是HTML文档的加载过程,如果遇到document.writ
    03-08
  • ASP.Net MVC 控制@Html.DisplayFor日期显示格式
    在做一個舊表的查詢頁時,遇到一個問題:字段在db里存儲的是DATETIME,但保存的值只有日期,沒有時間數據,比如2018/2/26 0:00:00,顯示出來比較難看,當然也可以做一個ViewModel,在字段上添加Attribute定義來更改名稱和顯示名稱,如下:[Display(Name = "建
    03-08
  • html 基础代码
    title淄博汉企/title/headbody bgcolor="#00CC66" topmargin="200" leftmargin="200" bottommargin="200"a name="top"/a今天br /天气nbsp;nbsp;nbsp;nbsp;nbsp;不错br /font color="#CC0000"格式控制标签br /b 文字加粗方式1\bbr /str
    03-08
点击排行