【Canvas与徽章】金圈银底建国75周年徽章
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>庆祝建国75周年</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;// 基准尺寸 // 最外层竖向渐变外框 ctx.save(); var r=R*1.00; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(238,230,165)"); gnt.addColorStop(0.25,"rgb(222,183,62)"); gnt.addColorStop(0.5,"rgb(255,255,190)"); gnt.addColorStop(0.6,"rgb(152,97,33)"); gnt.addColorStop(0.8,"rgb(252,238,165)"); gnt.addColorStop(1, "rgb(217,173,74)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 上半圈渐变外框 ctx.save(); var ro=R*0.99;// outer radius var ri=R*0.93;// inner radius var gnt=ctx.createLinearGradient(0,-ro,0,0); gnt.addColorStop(0, "rgb(252,247,183)"); gnt.addColorStop(0.5,"rgb(231,192,91)"); gnt.addColorStop(0.95,"rgb(220,208,156)"); gnt.addColorStop(1,"rgb(207,195,145)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(ri,0); ctx.lineTo(ro,0); ctx.arc(0,0,ro,0,-Math.PI,true); ctx.lineTo(-ri,0); ctx.arc(0,0,ri,-Math.PI,0,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 下半圈渐变外框 ctx.save(); var ro=R*0.99;// outer radius var ri=R*0.93;// inner radius var gnt=ctx.createLinearGradient(0,0,0,ro); gnt.addColorStop(0, "rgb(209,197,145)"); gnt.addColorStop(0.25, "rgb(98,60,37)"); gnt.addColorStop(0.75,"rgb(243,223,146)"); gnt.addColorStop(1,"rgb(229,184,81)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(ri,0); ctx.lineTo(ro,0); ctx.arc(0,0,ro,0,Math.PI,false); ctx.lineTo(-ri,0); ctx.arc(0,0,ri,Math.PI,0,true); ctx.closePath(); ctx.fill(); ctx.restore(); // 细圈 ctx.save(); var r=R*0.93; ctx.fillStyle="rgb(217,173,74)"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 内层周年圈 ctx.save(); var r=R*0.92; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(254,253,189)"); gnt.addColorStop(1, "rgb(204,154,69)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 左右五星 ctx.save(); var r=R*0.75; var N=2; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); ctx.fillStyle="rgb(68,41,11)"; ctx.strokeStyle="gold"; draw5Star(ctx,a.x,a.y,r/10); ctx.stroke(); ctx.fill(); } ctx.restore(); // 上半圈文字 var words="CELEBRATING"; var arr=words.split(""); const UP_ANGLE=Math.PI/3*2.2;// 上张角 const UP_OFFSET=UP_ANGLE/(arr.length-1); var r=R*0.65;// 文字半径 for(var i=0;i<arr.length;i++){ var letter=arr[i]; var theta=-Math.PI/2-UP_ANGLE/2+i*UP_OFFSET; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); // 辅助线 /*ctx.save(); ctx.lineWidth=1; ctx.strokeStyle="black"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(a.x,a.y); ctx.stroke(); ctx.restore();*/ ctx.save(); ctx.translate(a.x,a.y); ctx.rotate(theta+Math.PI/2); ctx.scale(1,0.8); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = r/3+"px Stencil Std"; ctx.fillStyle="rgb(68,41,11)"; ctx.fillText(letter,0,0); ctx.strokeStyle="gold"; ctx.strokeText(letter,0,0); ctx.restore(); } // 下半圈文字 var words="aniversary"; var arr=words.split(""); const DOWN_ANGLE=Math.PI/3*2.0;// 下张角 const DOWN_OFFSET=DOWN_ANGLE/(arr.length-1); var r=R*0.88;// 文字半径 for(var i=0;i<arr.length;i++){ var letter=arr[i]; var theta=Math.PI/2+DOWN_ANGLE/2-i*DOWN_OFFSET; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); // 辅助线 /*ctx.save(); ctx.lineWidth=1; ctx.strokeStyle="black"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(a.x,a.y); ctx.stroke(); ctx.restore();*/ ctx.save(); ctx.translate(a.x,a.y); ctx.rotate(theta-Math.PI/2); ctx.scale(1,0.8); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = r/3.8+"px Stencil Std"; ctx.fillStyle="rgb(68,41,11)"; ctx.fillText(letter,0,0); ctx.strokeStyle="gold"; ctx.strokeText(letter,0,0); ctx.restore(); } // 细圈 ctx.save(); var r=R*0.61; ctx.fillStyle="rgb(250,239,174)"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 内层竖向渐变外框 ctx.save(); var r=R*0.60; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(204,154,67)"); gnt.addColorStop(1, "rgb(254,253,189)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 酱圈 ctx.save(); var r=R*0.55; ctx.fillStyle="rgb(68,41,11)"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 白底 ctx.save(); var r=R*0.54; ctx.fillStyle="white"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 十字纹 ctx.save(); r=R*0.54; var N=3600; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); // 用锯齿函数实现周期变换,0.3为可调常数项 var alpha=(((i)%600)/600); if(alpha>0.5){ alpha=(1-alpha); } ctx.strokeStyle="rgba(188,187,189,"+alpha+")";// 控制直线的半透明度以调节明暗 ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(a.x,a.y); ctx.stroke(); } ctx.restore(); // 75 ctx.save(); r=R*0.5; var x=-r/11,y=r/11*6; var word="75"; ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = r/1.1+"px Stencil Std"; ctx.fillStyle="rgb(68,41,11)"; ctx.fillText(word,x,y); ctx.strokeStyle="gold"; ctx.strokeText(word,x,y); ctx.restore(); // th ctx.save(); r=R*0.5; var x=r/1.6,y=-r/11; var word="th"; ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = r/3.2+"px consolas"; ctx.fillStyle="rgb(68,41,11)"; ctx.fillText(word,x,y); ctx.strokeStyle="gold"; ctx.strokeText(word,x,y); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*-------------------------------------------------- 函数:绘制正五角星的推荐画法 ctx:绘图上下文 x:五角星中心横坐标 y:五角星中心纵坐标 R:五角星中心到顶点的距离 ---------------------------------------------------*/ function draw5Star(ctx,x,y,R){ var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7); var arr=[0,0,0,0,0,0,0,0,0,0]; // 顶五点 for(var i=0;i<5;i++){ var theta=i*Math.PI/5*2-Math.PI/10; var x1=R*Math.cos(theta)+x; var y1=R*Math.sin(theta)+y; arr[i*2]=createPt(x1,y1); } // 内五点 for(var i=0;i<5;i++){ var theta=i*Math.PI/5*2+Math.PI/10; var x1=r*Math.cos(theta)+x; var y1=r*Math.sin(theta)+y; arr[i*2+1]=createPt(x1,y1); } ctx.beginPath(); for(var i=0;i<arr.length;i++){ ctx.lineTo(arr[i].x,arr[i].y); } 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(); } /*------------------------------------------------------------- 我是真爱咱的国啊,可他妈谁爱我啊!---《茶馆》中常四爷语 作者老舍 --------------------------------------------------------------*/ //--> </script>
END