【Canvas与图标】压力表图标
【成图】
120*120的png图标:
大小图:
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>688.压力表图标</title> <style type="text/css"> .centerlize{ margin:0 auto; width:1200px; } </style> </head> <body onload="init();"> <div class="centerlize"> <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;"> 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试. </canvas> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 高宽 const WIDTH=512; const HEIGHT=512; // 舞台对象 var stage; //------------------------------- // 初始化 //------------------------------- function init(){ // 获得canvas对象 var canvas=document.getElementById('myCanvas'); canvas.width=WIDTH; canvas.height=HEIGHT; // 初始化canvas的绘图环境 ctx=canvas.getContext('2d'); ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移 // 准备 stage=new Stage(); stage.init(); // 开幕 animate(); } // 播放动画 function animate(){ stage.update(); stage.paintBg(ctx); stage.paintFg(ctx); // 循环 if(true){ //sleep(100); window.requestAnimationFrame(animate); } } // 舞台类 function Stage(){ // 初始化 this.init=function(){ } // 更新 this.update=function(){ } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 } // 画前景 this.paintFg=function(ctx){ // 底色 ctx.save(); ctx.fillStyle = "white"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); ctx.restore(); const R=210;// 基准尺寸 // 第1圈 ctx.save(); var r=R*1.00; drawSolidCircle(ctx,0,0,r,"black"); ctx.restore(); // 第2圈 颜色 ctx.save(); var rOut=R*0.95; var rIn=R*0.85; var N=22; const PART=Math.PI/N; var colors=["rgb(1,115,55)","rgb(0,140,69)","rgb(0,160,80)","rgb(77,183,72)", "rgb(140,198,62)","rgb(255,234,197)","rgb(216,223,32)","rgb(255,230,129)", "rgb(255,210,3)","rgb(253,184,19)","rgb(250,158,28)","rgb(246,129,33)", "rgb(243,101,35)","rgb(239,64,35)","rgb(226,27,34)","rgb(207,23,31)", "rgb(197,22,29)","rgb(178,17,23)","rgb(157,11,14)","rgb(139,3,5)", "rgb(121,1,2)","rgb(107,1,1)",]; for(var i=0;i<N;i++){ var theta=i*PART+Math.PI+0.01; var startAngle=theta; var endAngle=startAngle+PART-0.02; var a=createPt(rIn*Math.cos(startAngle),rIn*Math.sin(startAngle)); var b=createPt(rOut*Math.cos(startAngle),rOut*Math.sin(startAngle)); var c=createPt(rOut*Math.cos(endAngle),rOut*Math.sin(endAngle)); var d=createPt(rIn*Math.cos(endAngle),rIn*Math.sin(endAngle)); ctx.fillStyle=colors[i]; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.arc(0,0,rOut,startAngle,endAngle,false); ctx.lineTo(d.x,d.y); ctx.arc(0,0,rIn,endAngle,startAngle,true); ctx.closePath(); ctx.fill(); } ctx.restore(); // 第3圈 刻度 ctx.save(); var r=R*0.80; var N=25; for(var i=0;i<N;i++){ var theta=Math.PI/N*i+Math.PI+Math.PI/N/2; var rad1=r/20*20; var rad2=r/20*18; var pt=createPt(rad1*Math.cos(theta),rad1*Math.sin(theta)); var pt2=createPt(rad2*Math.cos(theta),rad2*Math.sin(theta)); ctx.lineWidth=R*0.02; ctx.strokeStyle=(i%4==0)?"blue":"rgb(40,40,100)"; ctx.beginPath(); ctx.moveTo(pt.x,pt.y); ctx.lineTo(pt2.x,pt2.y); ctx.stroke(); } ctx.restore(); // 指针 ctx.save(); var r=R*0.70; var theta=-Math.PI/3; var alpha=Math.asin(1/8); var ratio=10; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); var b=createPt(r/ratio*Math.cos(theta+(Math.PI/2-alpha)),r/ratio*Math.sin(theta+(Math.PI/2-alpha))); var c=createPt(r/ratio*Math.cos(theta-(Math.PI/2-alpha)),r/ratio*Math.sin(theta-(Math.PI/2-alpha))); ctx.lineWidth=1; ctx.fillStyle="rgb(253,184,19)"; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.arc(0,0,r/ratio,(theta+(Math.PI/2-alpha)),(theta-(Math.PI/2-alpha)),false); ctx.lineTo(c.x,c.y); ctx.closePath(); ctx.fill(); ctx.restore(); // 中间文字 ctx.save(); var r=R*1.00; var color="rgb(244,161,23)"; var start=createPt(0,r*0.6); var sz=R*0.23; writeText(ctx,start.x,start.y,"PRESSURE",sz+"px Stencil Std",color); ctx.restore(); // 上下两线 ctx.save(); var r=R*1.00; ctx.lineWidth=R/210*4; ctx.strokeStyle="rgb(194,22,36)"; ctx.beginPath(); ctx.moveTo(-r*0.6,r*0.28); ctx.lineTo(+r*0.6,r*0.28); ctx.stroke(); ctx.beginPath(); ctx.moveTo(-r*0.6,r*0.64); ctx.lineTo(+r*0.6,r*0.64); ctx.stroke(); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制矩形 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 ----------------------------------------------------------*/ function drawRect(ctx,x,y,width,height){ ctx.beginPath(); ctx.moveTo(x-width/2,y-height/2); ctx.lineTo(x+width/2,y-height/2); ctx.lineTo(x+width/2,y+height/2); ctx.lineTo(x-width/2,y+height/2); ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制实心圆,用途是标记点以辅助作图 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 r:圆半径 color:填充圆的颜色 ----------------------------------------------------------*/ function drawSolidCircle(ctx,x,y,r,color){ ctx.fillStyle=color; ctx.beginPath(); ctx.arc(x,y,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); } /*---------------------------------------------------------- 函数:创建一个二维坐标点 x:横坐标 y:纵坐标 Pt即Point ----------------------------------------------------------*/ function createPt(x,y){ var retval={}; retval.x=x; retval.y=y; return retval; } /*---------------------------------------------------------- 函数:延时若干毫秒 milliseconds:毫秒数 ----------------------------------------------------------*/ function sleep(milliSeconds) { const date = Date.now(); let currDate = null; while (currDate - date < milliSeconds) { currDate = Date.now(); } } /*---------------------------------------------------------- 函数:书写文字 ctx:绘图上下文 x:横坐标 y:纵坐标 text:文字 font:字体 color:颜色 ----------------------------------------------------------*/ function writeText(ctx,x,y,text,font,color){ ctx.save(); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = font; ctx.fillStyle=color; ctx.fillText(text,x,y); ctx.restore(); } /*------------------------------------------------------------- 经济学家付鹏内部分享摘录: 我200万买的房子,600万卖给年轻人,我拿走的就是年轻人未来40年 青春的当期现金折现,我可以为我的未来40年潇洒了, 他背上这40年的债务,他要还的, 如果没有收入的增长,他要硬生生地还40年! 他就是死去了,我替他多活40年,就这么简单...... --------------------------------------------------------------*/ //--> </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)