【Canvas与艺术】绘制磨砂黄铜材质Premium Quality徽章
【关键点】
渐变色的使用、斜纹的实现、底图的寻觅
【成果图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>磨砂黄铜材质Premium Quality徽章</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> <img id="myImg" src="124.jpg" style="display:none;"/> </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){ window.requestAnimationFrame(animate); } } // 舞台类 function Stage(){ // 初始化 this.init=function(){ } // 更新 this.update=function(){ } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 // 底色 var lgrd=ctx.createLinearGradient(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); lgrd.addColorStop(0,"rgba(168,159,154,0.8)"); lgrd.addColorStop(0.28,"rgba(7,12,41,0.8)"); lgrd.addColorStop(1,"rgba(7,12,41,0.8)"); ctx.fillStyle=lgrd; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); // 斜向条纹 ctx.save(); ctx.rotate(Math.PI/6); for(var i=0;i<100;i++){ var y=-HEIGHT+i*10; ctx.beginPath();// 不加此句线条粗劣 ctx.moveTo(-WIDTH,y); ctx.lineTo(WIDTH,y); ctx.lineWidth=0.2; var lgrd=ctx.createLinearGradient(-WIDTH,y,WIDTH,y); lgrd.addColorStop(0,"rgb(250,250,250)"); lgrd.addColorStop(1,"rgb(7,12,41)"); ctx.strokeStyle=lgrd; ctx.stroke(); } ctx.restore(); // 外缘 var arr=createWaveCircleArray(48,141,12); ctx.beginPath(); for(var j=0;j<arr.length-2;j+=2){ var pt1=arr[j]; var pt2=arr[j+1];// 控制点 var pt3=arr[j+2]; ctx.lineTo(pt1.x,pt1.y); ctx.quadraticCurveTo(pt2.x,pt2.y,pt3.x,pt3.y); } ctx.closePath(); ctx.strokeStyle="rgb(231,170,79)"; ctx.stroke(); // 铜底图 ctx.save(); var arr=createWaveCircleArray(48,140,12); ctx.beginPath(); for(var j=0;j<arr.length-2;j+=2){ var pt1=arr[j]; var pt2=arr[j+1];// 控制点 var pt3=arr[j+2]; ctx.lineTo(pt1.x,pt1.y); ctx.quadraticCurveTo(pt2.x,pt2.y,pt3.x,pt3.y); } ctx.closePath(); ctx.clip(); var img=document.getElementById("myImg"); ctx.drawImage(img,0,0,320,320,-160,-160,320,320); ctx.restore(); // 暗圈 ctx.beginPath(); ctx.arc(0,0,122,0,Math.PI*2,true); ctx.closePath(); ctx.lineWidth=1; ctx.strokeStyle="rgb(229,174,94)"; ctx.stroke(); // 蓝圈 ctx.beginPath(); ctx.arc(0,0,120,0,Math.PI*2,true); ctx.closePath(); ctx.lineWidth=2; ctx.strokeStyle="rgb(19,28,57)"; ctx.stroke(); // 亮圈 ctx.beginPath(); ctx.arc(0,0,118,0,Math.PI*2,true); ctx.closePath(); ctx.lineWidth=1; ctx.strokeStyle="rgb(231,170,79)"; ctx.stroke(); // 上半圈文字 var words="PREMIUM QUALITY"; var arr=words.split(""); for(var i=0;i<arr.length;i++){ var letter=arr[i]; var theta=i*Math.PI/180*8.5+Math.PI+Math.PI/180*34; var x=84*Math.cos(theta); var y=84*Math.sin(theta); ctx.save(); ctx.translate(x,y); ctx.rotate(theta+Math.PI/2); ctx.scale(1,1.2); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = "20px Stencil Std"; ctx.fillStyle="rgb(19,28,57)"; ctx.fillText(letter,0,0); ctx.restore(); } // 下半圈文字 var words="PREMIUM QUALITY"; var arr=words.split(""); for(var i=0;i<arr.length;i++){ var letter=arr[i]; var theta=-i*Math.PI/180*8.5+Math.PI-Math.PI/180*28; var x=113*Math.cos(theta); var y=113*Math.sin(theta); ctx.save(); ctx.translate(x,y); ctx.rotate(theta-Math.PI/2); ctx.scale(1,1.2); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = "20px Stencil Std"; ctx.fillStyle="rgb(19,28,57)"; ctx.fillText(letter,0,0); ctx.restore(); } draw5Star(ctx,-100,0,13,"rgb(19,28,57)"); draw5Star(ctx, 100,0,13,"rgb(19,28,57)"); // 蓝圈 ctx.beginPath(); ctx.arc(0,0,80,0,Math.PI*2,true); ctx.closePath(); ctx.lineWidth=1; ctx.strokeStyle="rgb(19,28,57)"; ctx.stroke(); // 亮圈 ctx.beginPath(); ctx.arc(0,0,80.5,0,Math.PI*2,true); ctx.closePath(); ctx.lineWidth=0.5; ctx.strokeStyle="rgb(231,170,79)"; ctx.stroke(); // 中心文字 ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.fillStyle="rgb(19,28,57)"; ctx.font = "36px Ink Free";// 手写字体 ctx.fillText("BEST",0,-10); // 加两根俏皮的横线 ctx.beginPath(); ctx.moveTo(-40,-10); ctx.lineTo(40,-15); ctx.lineWidth=0.8; ctx.strokeStyle="rgb(19,28,57)"; ctx.stroke(); ctx.beginPath(); ctx.moveTo(-32,-8); ctx.lineTo(38,-11); ctx.lineWidth=0.5; ctx.strokeStyle="rgb(19,28,57)"; ctx.stroke(); // 正规字体 ctx.font = "36px Bahnschrift SemiBold"; ctx.fillText("CHOICE",0,40); // 作者 ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = "8px consolas"; ctx.fillStyle="white"; ctx.fillText("逆火原创",WIDTH/2-40,HEIGHT/2-10); } // 画前景 this.paintFg=function(ctx){ } } //-------------------------------------------------- // 函数:创建波浪式环形需要的数组 // n:浪头峰谷个数 // radius:环形半径 // waveHeight:浪高 // 返回:包含浪高中低点坐标的数组 //-------------------------------------------------- function createWaveCircleArray(n,radius,waveHeight){ var arr=[]; const LEN=n+2;// 数组长度比浪头峰谷数多两个以在绘图时形成闭环 for(var i=0;i<LEN;i++){ var theta=i*Math.PI*2/n; var r=radius+Math.sin(Math.PI/2*i)*waveHeight;// 造成涨落 var pt={}; pt.x=r*Math.cos(theta); pt.y=r*Math.sin(theta); arr.push(pt); } return arr; } // 画实心五角星的函数 function draw5Star(ctx,x,y,r,color){ ctx.save() ctx.translate(x-r,y-r); ctx.beginPath(); ctx.moveTo(r, 0); ctx.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r); ctx.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r); ctx.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r); ctx.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r); ctx.lineTo(r, 0); ctx.closePath(); ctx.fillStyle=color; ctx.fill(); ctx.restore(); } /*--------------------------------------------- 如果动物吃不胖,它肚子里一定有寄生虫; 如果百姓勤劳而不能致富,那社会里一定有寄生虫! ----------------------------------------------*/ //--> </script>
【底图】
上文用到的底图 124.jpg
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)
2016-03-04 【Canvas技法】绘制绘制正五边形、正五角星
2016-03-04 VI使用技巧
2016-03-04 【Canvas与数学】绘制圆形中运动的包络线