站长资源网络编程

javascript+HTML5 canvas绘制时钟功能示例

整理:jimmy2026/8/26浏览2
简介本文实例讲述了javascript+HTML5 canvas绘制时钟功能。分享给大家供大家参考,具体如下:效果如下:代码:www.jb51.net canvas绘</div> <div class="news_infos"><div id="MyContent"><p>本文实例讲述了javascript+HTML5 canvas绘制时钟功能。分享给大家供大家参考,具体如下:</p> <p>效果如下:</p> <p><a href="javascript:;" onclick="showimg('/UploadFiles/2021-04-02/201951593828860.gif');"><img src="/UploadFiles/2021-04-02/201951593828860.gif" alt="javascript+HTML5 canvas绘制时钟功能示例" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/></a></p> <p>代码:</p> <div class="htmlcode"> <pre class="brush:js;"> <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>www.jb51.net canvas绘制时钟</title> <style> div{text-align:center;margin-top:250px;} </style> </head> <body> <div> <canvas id="clock" width="300px" height="300px"></canvas> </div> <script> var dom=document.getElementById("clock"); var ctx=dom.getContext("2d"); var width=ctx.canvas.width; var height=ctx.canvas.height; var r=width/2; var rem=width/200; function drawBackground(){ ctx.save(); ctx.translate(r,r);//重新定义圆点到中心 ctx.beginPath(); ctx.lineWidth=10*rem; ctx.arc(0,0,r-5*rem,0,Math.PI*2,false);//圆点坐标,起始角0,结束角2π,顺时针 ctx.stroke(); var hourNum=[3,4,5,6,7,8,9,10,11,12,1,2];//数组存小时数 ctx.font="18px Arial"; ctx.textAlign="center"; ctx.textBaseline="middle"; hourNum.forEach(function (num,i) { var rad=2*Math.PI/12*i;//弧度 var x=(r-30*rem)*Math.cos(rad); var y=(r-30*rem)*Math.sin(rad); ctx.fillText(num,x,y); }); for(var i=0;i<60;i++){//画圆点 var rad=2*Math.PI/60*i; var x=(r-18*rem)*Math.cos(rad); var y=(r-18*rem)*Math.sin(rad); ctx.beginPath(); if(i%5==0){ ctx.fillStyle="#000"; ctx.arc(x,y,2,0,Math.PI*2,false); } else{ ctx.fillStyle="#ccc"; ctx.arc(x,y,2,0,Math.PI*2,false); } ctx.fill(); } ctx.closePath(); } function drawHour(hour,minute){//时针 ctx.save(); ctx.beginPath(); var rad=2*Math.PI/12*hour; var mrad=2*Math.PI/12/60*minute; ctx.rotate(rad+mrad); ctx.lineWidth=6*rem; ctx.moveTo(0,10*rem); ctx.lineTo(0,-r/2); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); } function drawMinute(minute,second){//分针 ctx.save(); ctx.beginPath(); var rad=2*Math.PI/60*minute; var srad=2*Math.PI/60/60*second; ctx.rotate(rad+srad); ctx.lineWidth=3*rem; ctx.moveTo(0,10*rem); ctx.lineTo(0,-r+30*rem); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); } function drawSecond(second){//秒针 ctx.save(); ctx.beginPath(); ctx.fillStyle="#c14543"; var rad=2*Math.PI/60*second; ctx.rotate(rad); ctx.moveTo(-2*rem,20*rem); ctx.lineTo(2*rem,20*rem); ctx.lineTo(1*rem,-r+18*rem); ctx.lineTo(-1*rem,-r+18*rem); ctx.fill(); ctx.restore(); } function drawDot(){//画中心圆圈 ctx.beginPath(); ctx.fillStyle="#fff"; ctx.arc(0,0,3*rem,0,2*Math.PI,false); ctx.fill(); } function draw(){ ctx.clearRect(0,0,width,height); var now=new Date(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.getSeconds(); drawBackground();//背景 drawHour(hour,minute);//小时 drawMinute(minute,second);//分钟 drawSecond(second);//秒钟 drawDot();//中心圆点 ctx.restore(); } setInterval(draw,1000); </script> </body> </html> </pre> </div> <p>其中的rem值是为了使canvas画布大小变化时时钟的样式能够保持</p> <p>感兴趣的朋友可使用<strong>在线HTML/CSS/JavaScript代码运行工具</strong>:http://tools.jb51.net/code/HtmlJsRun测试一下运行效果。</p> <p><strong>PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:</strong></p> <p><strong>在线日期/天数计算器:<br> </strong>http://tools.jb51.net/jisuanqi/date_jisuanqi</p> <p><strong>在线日期计算器/相差天数计算器:<br> </strong>http://tools.jb51.net/jisuanqi/datecalc</p> <p><strong>在线日期天数差计算器:<br> </strong>http://tools.jb51.net/jisuanqi/onlinedatejsq</p> <p><strong>Unix时间戳(timestamp)转换工具:<br> </strong>http://tools.jb51.net/code/unixtime</p> <p>更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript+HTML5特效与技巧汇总》、《JavaScript图形绘制技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》</p> <p>希望本文所述对大家JavaScript程序设计有所帮助。</p></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="http://m.paidiu.com/show/1/86202.html" title="小程序:授权、登录、session_key、unionId的详解">小程序:授权、登录、session_key、unionId的详解</a></p> <p>下一篇:<a href="http://m.paidiu.com/show/1/86204.html" title="详解小程序用户登录状态检查与更新实例">详解小程序用户登录状态检查与更新实例</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619123.html" one-link-mark="yes" title="群星《奔赴!万人现场 第2期》[FLAC/分轨][518.87MB]"><span>群星《奔赴!万人现场 第2期》[FLAC/分轨][518.8</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619122.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[320K/MP3][43.91MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[32</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619121.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FLAC/分轨][140.49MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FL</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619120.html" one-link-mark="yes" title="【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]"><span>【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619119.html" one-link-mark="yes" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]"><span>贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619118.html" one-link-mark="yes" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]"><span>小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619117.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]"><span>群星《欢迎来到我身边 电影原声专辑》[320K/MP3</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619116.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]"><span>群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619115.html" one-link-mark="yes" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]"><span>雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619114.html" one-link-mark="yes" title="群星《2024好听新歌42》AI调整音效【WAV分轨】"><span>群星《2024好听新歌42》AI调整音效【WAV分轨】</span></a> </li> </ul> </div> </div> <div class="sidebar"> </div> </article> <footer> <p style="font-size: 14px;">友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="http://www.ddrfans.com/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a></p> <p>Design by <a href="http://m.paidiu.com">黑松山资源网</a> <a href="/">http://m.paidiu.com</a></p> </footer> <script src="/images/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>