【Canvas与钟表】硬朗风格十二棱表表盘
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>正十二边内嵌齿轮花样表盘</title> <style type="text/css"> .centerlize{ margin:0 auto; width:1200px; } </style> </head> <body onload="init();"> <div class="centerlize"> <canvas id="myCanvas" width="512px" height="512px" style="border:1px dotted black;"> 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试. </canvas> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 边长 const LENGTH=512; // 舞台对象 var stage; //------------------------------- // 初始化 //------------------------------- function init(){ // 获得canvas对象 var canvas=document.getElementById('myCanvas'); // 初始化canvas的绘图环境 ctx=canvas.getContext('2d'); ctx.translate(LENGTH/2,LENGTH/2);// 原点平移到画布中央 // 准备 stage=new Stage(); stage.init(); } // 每一秒调用paintClock函数 setInterval(paintClock,1000); //------------------------------- // 绘制时钟 //------------------------------- function paintClock(){ stage.update(); stage.paintBg(ctx); stage.paintFg(ctx); } // 舞台类 function Stage(){ // 初始化 this.init=function(){ } // 更新 this.update=function(){ } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-LENGTH/2,-LENGTH/2,LENGTH,LENGTH);// 清屏 // 准备正十二边形 var polyArr=[]; for(var i=0;i<12;i++){ var theta=Math.PI/6*i; var pt={}; pt.x=Math.cos(theta); pt.y=Math.sin(theta); polyArr.push(pt); } // 最外圈 ctx.beginPath(); for(let i=0;i<polyArr.length;i++){ ctx.lineTo(250*polyArr[i].x,250*polyArr[i].y); } ctx.closePath(); var linegrad=ctx.createLinearGradient(-250,-250,250,250); linegrad.addColorStop(0,"rgb(103,103,103)"); linegrad.addColorStop(1,"rgb(32,32,32)"); ctx.fillStyle=linegrad; ctx.fill(); // 第二圈 ctx.beginPath(); for(let i=0;i<polyArr.length;i++){ ctx.lineTo(240*polyArr[i].x,240*polyArr[i].y); } ctx.closePath(); var linegrad=ctx.createLinearGradient(-240,-240,240,240); linegrad.addColorStop(0,"rgb(43,43,43)"); linegrad.addColorStop(1,"rgb(184,184,184)"); ctx.fillStyle=linegrad; ctx.fill(); // 第三圈 ctx.beginPath(); for(let i=0;i<polyArr.length;i++){ ctx.lineTo(230*polyArr[i].x,230*polyArr[i].y); } ctx.closePath(); ctx.fillStyle="rgb(49,49,49)"; ctx.fill(); // 十二时辰 var hours=["03","04","05","06","07","08","09","10","11","12","01","02"]; for(var i=0;i<12;i++){ var theta=i*Math.PI/6; var x3=190*Math.cos(theta); var y3=190*Math.sin(theta); ctx.save(); ctx.translate(x3,y3); ctx.rotate(theta+Math.PI/2); ctx.font="36px Microsoft YaHei UI"; ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.fillStyle="rgb(238,176,75)"; ctx.fillText(hours[i],0,0); ctx.restore(); } // 内圈刻度 for(var i=0;i<60;i++){ var theta=i*Math.PI/30; var x1=180*Math.cos(theta); var y1=180*Math.sin(theta); var x2=160*Math.cos(theta); var y2=160*Math.sin(theta); ctx.beginPath(); ctx.moveTo(x1,y1); ctx.lineTo(x2,y2); ctx.closePath(); ctx.lineWidth=(i%5==0)?3:1; ctx.strokeStyle="rgb(231,219,111)"; ctx.stroke(); } // 准备齿轮数组 var gearArr=[]; for(var i=0;i<36;i++){ var theta=Math.PI/18*i-Math.PI/36; var x1=150*Math.cos(theta); var y1=150*Math.sin(theta); var x2=140*Math.cos(theta); var y2=140*Math.sin(theta); if(i%2==0){ gearArr.push({x:x1,y:y1}); gearArr.push({x:x2,y:y2}); }else{ gearArr.push({x:x2,y:y2}); gearArr.push({x:x1,y:y1}); } } // 画齿轮 ctx.beginPath(); for(var i=0;i<gearArr.length;i++){ ctx.lineTo(gearArr[i].x,gearArr[i].y); } ctx.closePath(); ctx.fillStyle="rgb(255,255,255)"; ctx.fill(); // 转0.5°再画一圈齿轮,造成明暗效果 ctx.save(); ctx.rotate(Math.PI/360); ctx.beginPath(); for(var i=0;i<gearArr.length;i++){ ctx.lineTo(gearArr[i].x,gearArr[i].y); } ctx.closePath(); ctx.fillStyle="rgb(49,49,49)"; ctx.fill(); ctx.restore(); // logo ctx.font="12px 仿宋"; ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.fillStyle="white"; ctx.fillText("逆火原创",0,-70); } // 画前景 this.paintFg=function(ctx){ // 得到当前时间 var now=new Date(); var s=now.getSeconds(); var m=now.getMinutes(); var h=now.getHours()+m/60; // 日期 var tMonth = now.getMonth()+1; var tDate = now.getDate(); ctx.font="24px Microsoft YaHei UI"; ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.fillStyle="white"; ctx.fillText(tMonth+"/"+tDate,0,70); // 星期 ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.fillStyle="rgb(238,176,75)"; ctx.fillText(getWeekDay(now.getDay()),0,100); // 画时针 ctx.save(); ctx.rotate(h*Math.PI/6-Math.PI/2); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-20,0); ctx.lineTo(-20,4); ctx.lineTo(120,4); ctx.lineTo(130,0); ctx.closePath(); ctx.fillStyle="rgb(171,172,166)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-20,0); ctx.lineTo(-20,-4); ctx.lineTo(120,-4); ctx.lineTo(130,0); ctx.closePath(); ctx.fillStyle="rgb(252,252,250)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(80,1); ctx.lineTo(110,1); ctx.lineTo(110,-1); ctx.lineTo(80,-1); ctx.closePath(); ctx.fillStyle="rgb(49,49,49)"; ctx.fill(); ctx.restore(); // 画分针 ctx.save(); ctx.rotate(m*Math.PI/30-Math.PI/2); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-25,0); ctx.lineTo(-25,4); ctx.lineTo(130,4); ctx.lineTo(140,0); ctx.closePath(); ctx.fillStyle="rgb(171,172,166)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-25,0); ctx.lineTo(-25,-4); ctx.lineTo(130,-4); ctx.lineTo(140,0); ctx.closePath(); ctx.fillStyle="rgb(252,252,250)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(90,1); ctx.lineTo(120,1); ctx.lineTo(120,-1); ctx.lineTo(90,-1); ctx.closePath(); ctx.fillStyle="rgb(49,49,49)"; ctx.fill(); ctx.restore(); // 画秒针 ctx.save(); ctx.rotate(s*Math.PI/30-Math.PI/2); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-30,0); ctx.lineTo(-30,2); ctx.lineTo(130,2); ctx.lineTo(130,6); ctx.lineTo(160,0); ctx.closePath(); ctx.fillStyle="rgb(171,172,166)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(-30,0); ctx.lineTo(-30,-2); ctx.lineTo(130,-2); ctx.lineTo(130,-6); ctx.lineTo(160,0); ctx.closePath(); ctx.fillStyle="rgb(252,252,250)"; ctx.fill(); ctx.beginPath(); ctx.moveTo(134,2); ctx.lineTo(146,0); ctx.lineTo(134,-2); ctx.closePath(); ctx.fillStyle="rgb(251,143,56)"; ctx.fill(); ctx.restore(); // 画中心小圆点 ctx.beginPath(); ctx.arc(0,0,6,0,Math.PI*2,true); ctx.closePath(); ctx.fillStyle="rgb(251,143,56)"; ctx.fill(); ctx.beginPath(); ctx.arc(0,0,2,0,Math.PI*2,true); ctx.closePath(); ctx.fillStyle="rgb(49,49,49)"; ctx.fill(); } } // 取当天是星期几 function getWeekDay(idx){ const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; return days[idx]; } /*--------------------------------------------- 笼鸡有粮刀汤近,野鹤无粮天地宽。 ----------------------------------------------*/ //--> </script>
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-08-30 永远的飞翔 张广天
2013-08-30 在Foreda8中整合Apche httpd2.4.6和Tomcat7.0.42(使用tomcat-connectors-1.2.37)
2013-08-30 Foreda8上安装Ant1.9.2
2013-08-30 【Canvas与函数极值】求函数 y=(x^2-2x-3)/(2x^2+2x+1) 的极值点