【Canvas与文字】一双布鞋欺天下五万一年赚人夸
【成图】





【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>铝圈黑灯 Draft2 加一双布鞋欺天下</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 = "rgb(101,101,89)"; //ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); //ctx.restore(); const R=220;//基准尺寸 // #1 偏心黑圈 ctx.save(); var r=R*1.00; drawSolidCircle(ctx,0,-r*1/40,r*0.98,"black"); ctx.restore(); // #2 中亮边暗铝圈 ctx.save(); var r=R*1.00; var gnt=ctx.createLinearGradient(-r,0,r,0); gnt.addColorStop(0,"rgb(158,158,158)"); gnt.addColorStop(0.3,"rgb(231,231,231)"); gnt.addColorStop(0.5,"rgb(255,255,255)"); gnt.addColorStop(0.7,"rgb(231,231,231)"); gnt.addColorStop(1,"rgb(158,158,158)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #3 下半暗圈 ctx.save(); var r=R*1.00; var theta=Math.PI/18; var a=createPt2(0,0,r,-theta); var b=createPt2(0,0,r,Math.PI+theta); var gnt=ctx.createLinearGradient(0,b.y,0,r); gnt.addColorStop(0,"rgb(6,6,6)"); gnt.addColorStop(0.25,"rgb(92,92,92)"); gnt.addColorStop(0.5,"rgb(156,156,156)"); gnt.addColorStop(0.95,"rgb(238,238,238)"); gnt.addColorStop(1,"rgb(245,245,245)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.arc(0,0,r,-theta,Math.PI+theta,false); ctx.lineTo(b.x,b.y); ctx.quadraticCurveTo(0,0,a.x,a.y); ctx.closePath(); ctx.fill(); ctx.restore(); // #4 分隔圈 ctx.save(); var r=R*0.855; drawSolidCircle(ctx,0,0,r,"white"); ctx.restore(); // #5 上黑下白圈 ctx.save(); var r=R*0.85; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0,"rgb(1,1,1)"); gnt.addColorStop(1,"rgb(254,254,254)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #6 分隔圈 ctx.save(); var r=R*0.815; drawSolidCircle(ctx,0,0,r,"white"); ctx.restore(); // #7 外黑内白圈 ctx.save(); var r=R*0.81; var gnt=ctx.createRadialGradient(0,0,0,0,0,r); gnt.addColorStop(0,"rgb(145,145,145)"); gnt.addColorStop(0.9,"rgb(54,54,54)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // #8 诗词 ctx.save(); var r=R*0.81; var w=2*r; var h=w; const MARGIN_H=r*0.14; // 上下留白 const MARGIN_W=r*0.45; // 左右留白 h-=2*MARGIN_H; // 高 w-=2*MARGIN_W; // 宽 var cols=4; // 列数 var xOffSet=w/cols; // 横向偏移值 var rows=7; // 行数 var yOffSet=h/rows; // 竖向偏移值 var str="一双布鞋欺天下五万一年赚人夸七个娃来五个妈离岸公司美国家";// 文字 var arr=str.split("");// 存储单个文字的数组 for(var i=0;i<cols;i++){ for(var j=0;j<rows;j++){ var a=createPt(w/2-(i+0.5)*xOffSet,-h/2+(j+0.5)*yOffSet); writeText(ctx,a.x,a.y+r*0.10,arr[i*rows+j],r/6+"px 方正宋刻本秀楷简体","white"); } } ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 radius:圆角半径 ----------------------------------------------------------*/ function drawRoundRect(ctx,x,y,width,height,radius){ ctx.beginPath(); ctx.moveTo(x-width/2+radius,y-height/2); ctx.lineTo(x+width/2-radius,y-height/2); ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius); ctx.lineTo(x+width/2,y-height/2+radius); ctx.lineTo(x+width/2,y+height/2-radius); ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius); ctx.lineTo(x+width/2-radius,y+height/2); ctx.lineTo(x-width/2+radius,y+height/2); ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius); ctx.lineTo(x-width/2,y+height/2-radius); ctx.lineTo(x-width/2,y-height/2+radius); ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius); ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制矩形 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:圆半径 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(); } /*---------------------------------------------------------- 函数:创建一个二维坐标点 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
浙公网安备 33010602011771号