【Canvas与徽章】铁网黑蓝白大卫之星徽章
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>铁网黑蓝白大卫之星徽章 Draft1</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 r=R*0.99; var gnt=ctx.createLinearGradient(-r,0,r,0); gnt.addColorStop(0,"rgb(169,178,187)"); gnt.addColorStop(0.13,"rgb(126,134,137)"); gnt.addColorStop(0.4,"rgb(91,103,103)"); gnt.addColorStop(0.5,"rgb(91,100,105)"); gnt.addColorStop(0.6,"rgb(222,223,227)"); gnt.addColorStop(0.87,"rgb(117,122,128)"); gnt.addColorStop(1,"rgb(169,178,187)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #3 ctx.save(); var r=R*0.95; drawSolidCircle(ctx,0,0,r,"black"); ctx.restore(); // #4 ctx.save(); var r=R*0.94; drawSolidCircle(ctx,0,0,r,"white"); ctx.restore(); // #5 交错正方形网格 ctx.save(); var r=R*0.94; var N=3; for(var i=0;i<N;i++){ var theta=i*Math.PI/6; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); var b=createPt(r*Math.cos(theta+Math.PI/2),r*Math.sin(theta+Math.PI/2)); var c=createPt(r*Math.cos(theta+Math.PI),r*Math.sin(theta+Math.PI)); var d=createPt(r*Math.cos(theta+Math.PI/2*3),r*Math.sin(theta+Math.PI/2*3)); ctx.lineWidth=R*0.02; ctx.strokeStyle="black"; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.stroke(); } ctx.restore(); // #6 ctx.save(); var r=R*0.67; drawSolidCircle(ctx,0,0,r,"black"); ctx.restore(); // #7 ctx.save(); var r=R*0.65; drawSolidCircle(ctx,0,0,r,"white"); ctx.restore(); // #8 ctx.save(); var r=R*0.62; drawSolidCircle(ctx,0,0,r,"black"); ctx.restore(); // #9 ctx.save(); var r=R*0.61; var gnt=ctx.createLinearGradient(-r,0,r,0); gnt.addColorStop(0,"rgb(169,178,187)"); gnt.addColorStop(0.13,"rgb(126,134,137)"); gnt.addColorStop(0.4,"rgb(91,103,103)"); gnt.addColorStop(0.5,"rgb(91,100,105)"); gnt.addColorStop(0.6,"rgb(222,223,227)"); gnt.addColorStop(0.87,"rgb(117,122,128)"); gnt.addColorStop(1,"rgb(169,178,187)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #10 ctx.save(); var r=R*0.57; drawSolidCircle(ctx,0,0,r,"black"); ctx.restore(); // #11 ctx.save(); var r=R*0.56; drawSolidCircle(ctx,0,0,r,"white"); ctx.restore(); // #12 辉光渐变圈 ctx.save(); var r=R*0.53; var gnt=ctx.createRadialGradient(0,0,0,0,0,r); gnt.addColorStop(0,"white"); gnt.addColorStop(0.125,"rgb(214,232,232)"); gnt.addColorStop(0.25,"rgb(184,218,219)"); gnt.addColorStop(0.5,"rgb(115,198,216)"); gnt.addColorStop(0.8,"rgb(87,183,199)"); gnt.addColorStop(0.85,"rgb(48,102,164)"); gnt.addColorStop(0.9,"rgb(25,43,127)"); gnt.addColorStop(0.95,"rgb(17,14,59)"); gnt.addColorStop(1,"black"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #12 大卫之星 ctx.save(); var r=R*0.20; var gap=r/6; var thickness=2*gap; var N=6; 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)); var angle=theta+Math.PI*2/3; var b=createPt(a.x+gap*Math.cos(angle),a.y+gap*Math.sin(angle)); angle=theta+Math.PI*2/3; var l=2*r; var c=createPt(a.x+l*Math.cos(angle),a.y+l*Math.sin(angle)); angle=theta+Math.PI+Math.PI*1/3; var l=r-gap-thickness; var d=createPt(c.x+l*Math.cos(angle),c.y+l*Math.sin(angle)); angle=theta+Math.PI; var l=thickness; var e=createPt(d.x+l*Math.cos(angle),d.y+l*Math.sin(angle)); angle=theta+Math.PI/3; var l=r+thickness-gap; var f=createPt(e.x+l*Math.cos(angle),e.y+l*Math.sin(angle)); angle=theta-Math.PI/3; var l=2*(r+thickness)-thickness-gap; var g=createPt(f.x+l*Math.cos(angle),f.y+l*Math.sin(angle)); angle=theta-Math.PI/3*2; var l=thickness; var h=createPt(g.x+l*Math.cos(angle),g.y+l*Math.sin(angle)); ctx.fillStyle="black"; ctx.strokeStyle="white"; ctx.lineWidth=R*0.02; ctx.beginPath(); ctx.moveTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.lineTo(e.x,e.y); ctx.lineTo(f.x,f.y); ctx.lineTo(g.x,g.y); ctx.lineTo(h.x,h.y); ctx.closePath(); ctx.stroke(); ctx.fill(); } ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制实心圆 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 r:圆半径 style:填充圆的方案 ----------------------------------------------------------*/ function drawSolidCircle(ctx,x,y,r,style){ ctx.fillStyle=style; 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(); } /*------------------------------------------------------------- 98年水浒谱好而未用之词 按:好词无曲,实不忍任其埋没于故纸堆中,故而录上网络, 实望某日有大家能为其谱曲,众人传唱开来。 水浒惊涛卷 英雄怒火燃 民间血和泪 朝廷骗和瞒 梁山兄弟都有苦 大宋官员多姓贪 苛政猛于虎 冤案积如山 效忠无门路 仗义受刁难 忠也难 义也难 好人一生不平安 悲歌慷慨啊 悲无用 借酒浇愁啊 愁更愁 反也难 顺也难 委曲求全也枉然 忍忍忍,忍无可忍 逼逼逼,逼上梁山! --------------------------------------------------------------*/ //--> </script>
END