【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=220;//基准尺寸 const ct=createPt(0,0);// ct=center // #1 ctx.save(); var r=R*1.00; var top=createPt2(ct.x,ct.y,r,-Math.PI/2); var bottom=createPt2(ct.x,ct.y,r,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(235,194,114)"); gnt.addColorStop(1,"rgb(167,118,39)"); ctx.fillStyle=gnt; drawRoundSixPolygon(ctx,ct.x,ct.y,r,r/10,Math.PI/6); ctx.fill(); ctx.restore(); // #2 ctx.save(); var r=R*0.94; ctx.fillStyle="black"; drawRoundSixPolygon(ctx,ct.x,ct.y,r,r/20,Math.PI/6); ctx.fill(); ctx.restore(); // #3 ctx.save(); var r=R*0.93; var top=createPt2(ct.x,ct.y,r,-Math.PI/2); var bottom=createPt2(ct.x,ct.y,r,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(214,168,33)"); gnt.addColorStop(1,"rgb(209,159,38)"); ctx.fillStyle=gnt; drawRoundSixPolygon(ctx,ct.x,ct.y,r,r/24,Math.PI/6); ctx.fill(); ctx.restore(); // #4 点边 ctx.save(); var r=R*0.90; for(var i=0;i<6;i++){ var theta=Math.PI/3*i+Math.PI/6; var a=createPt2(ct.x,ct.y,r,theta); var b=createPt2(ct.x,ct.y,r,theta+Math.PI/3); var n=16; var xOffset=(b.x-a.x)/n; var yOffset=(b.y-a.y)/n; for(var j=0;j<n;j++){ var p=createPt(a.x+xOffset*j,a.y+yOffset*j); drawSolidCircle(ctx,p.x,p.y,R/220*2,"black"); } } ctx.restore(); // #5 黑底 ctx.save(); var r=R*0.87; //var gnt=ctx.createRadialGradient(0,0,0,0,0,r); //gnt.addColorStop(0,"rgb(254,240,7)"); //gnt.addColorStop(1,"rgb(255,147,2)"); ctx.fillStyle="rgb(10,6,5)"; drawRoundSixPolygon(ctx,ct.x,ct.y,r,0,Math.PI/6); ctx.fill(); ctx.restore(); // #6 竖线 ctx.save(); var r=R*0.87; /*drawRoundSixPolygon(ctx,ct.x,ct.y,r,0,Math.PI/6); ctx.clip(); var w=r*Math.sqrt(3); var h=r*2; var n=10; for(var i=0;i<n;i++){ var x=-w/2+i*w/n; ctx.strokeStyle="white"; ctx.lineWidth=R/220; ctx.beginPath(); ctx.moveTo(x,-h/2); ctx.lineTo(x,+h/2); ctx.stroke(); }*/ ctx.restore(); // #7 双横线 ctx.save(); var r=R*0.87; var top=createPt2(ct.x,ct.y,r*0.4,-Math.PI/2); var topLeft=createPt2(top.x,top.y,r*0.6,Math.PI); var topRight=createPt2(top.x,top.y,r*0.6,0); ctx.strokeStyle="rgb(224,193,110)"; ctx.lineWidth=R/220*2; ctx.beginPath(); ctx.moveTo(topLeft.x,topLeft.y); ctx.lineTo(topRight.x,topRight.y); ctx.stroke(); var bottom=createPt2(ct.x,ct.y,r*0.4,Math.PI/2); var bottomLeft=createPt2(bottom.x,bottom.y,r*0.6,Math.PI); var bottomRight=createPt2(bottom.x,bottom.y,r*0.6,0); ctx.strokeStyle="rgb(224,193,110)"; ctx.lineWidth=R/220*2; ctx.beginPath(); ctx.moveTo(bottomLeft.x,bottomLeft.y); ctx.lineTo(bottomRight.x,bottomRight.y); ctx.stroke(); ctx.restore(); // #8 文字 ctx.save(); var r=R*0.87; var color="rgb(199,176,96)"; writeText(ctx,ct.x,ct.y-r*0.05,"大丈夫生于天地之间",r*0.16+"px 方正宋刻本秀楷简体",color); writeText(ctx,ct.x,ct.y+r*0.20,"岂能郁郁乎久居人下",r*0.16+"px 方正宋刻本秀楷简体",color); ctx.restore(); // #9 三星 ctx.save(); var r=R*0.87; var top=createPt2(ct.x,ct.y,r*0.65,-Math.PI/2); ctx.fillStyle="rgb(218,189,113)"; draw5Star(ctx,top.x,top.y,r/8); ctx.fill(); var topLeft=createPt2(top.x,top.y,r*0.3,Math.PI-Math.PI/36); ctx.fillStyle="rgb(218,189,113)"; draw5Star(ctx,topLeft.x,topLeft.y,r/12); ctx.fill(); var topRight=createPt2(top.x,top.y,r*0.3,Math.PI/36); ctx.fillStyle="rgb(218,189,113)"; draw5Star(ctx,topRight.x,topRight.y,r/12); ctx.fill(); var bottom=createPt2(ct.x,ct.y,r*0.65,Math.PI/2); var bottomLeft=createPt2(bottom.x,bottom.y,r*0.3,Math.PI+Math.PI/36); var bottomRight=createPt2(bottom.x,bottom.y,r*0.3,-Math.PI/36); ctx.strokeStyle="rgb(218,189,113)"; ctx.lineWidth=R/220*2; ctx.beginPath(); ctx.moveTo(bottomLeft.x,bottomLeft.y); ctx.lineTo(bottomRight.x,bottomRight.y); ctx.stroke(); ctx.restore(); // #10 玻璃光 ctx.save(); var r=R*1.00; drawRoundSixPolygon(ctx,ct.x,ct.y,r,r/10,Math.PI/6); ctx.clip(); var arr=[]; for(var i=0;i<6;i++){ var theta=Math.PI/3*i+Math.PI/6; var p=createPt2(ct.x,ct.y,r,theta); arr.push(p); } var a=arr[2]; var b=arr[3]; var c=arr[4]; var d=createPt(c.x/2+arr[5].x/2,c.y/2+arr[5].y/2); ctx.fillStyle="rgba(190,190,190,0.3)"; 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.quadraticCurveTo(ct.x,ct.y,a.x,a.y); ctx.fill(); 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:六边形半径 r:圆角半径 initAngle:初始角度 ----------------------------------------------------------*/ function drawRoundSixPolygon(ctx,x,y,R,r,initAngle){ // 取点 const N=6;// 六边形边数 const ROUND=r;// 圆角半径 var arr=[];// 放顶点三点的数组 for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i+initAngle; var r=R; var a=createPt(x+r*Math.cos(theta),y+r*Math.sin(theta)); r=ROUND/Math.sqrt(3); var angle=theta+Math.PI/3*2; var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=ROUND/Math.sqrt(3); angle=theta-Math.PI/3*2; var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); var arrInner=[b,a,c]; arr.push(arrInner); } // 绘图 ctx.save(); ctx.beginPath(); for(var i=0;i<arr.length;i++){ var b=arr[i][0]; var a=arr[i][1]; var c=arr[i][2]; ctx.lineTo(b.x,b.y); ctx.quadraticCurveTo(a.x,a.y,c.x,c.y); ctx.lineTo(c.x,c.y); } ctx.closePath(); ctx.restore(); } /*---------------------------------------------------------- 基本函数:用于绘制实心圆 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(); } /*---------------------------------------------------------- 基本函数:用于绘制矩形 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(); } /*---------------------------------------------------------- 基本函数:创建一个二维坐标点 baseX:基准点横坐标 baseY:基准点纵坐标 radius:当前点到基准点的距离 theta:当前点到基准点的角度 Pt即Point ----------------------------------------------------------*/ function createPt2(baseX,baseY,radius,theta){ var retval={}; retval.x=baseX+radius*Math.cos(theta); retval.y=baseY+radius*Math.sin(theta); return retval; } /*---------------------------------------------------------- 基本函数:创建一个二维坐标点 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