【Canvas与logo】安布雷拉伞公司方形胸标
【成图】





【代码】
<!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;//基准尺寸 var ct=createPt(0,0);// ct=center // #1 灰边 ctx.save(); var r=R*1.00; var w=2*r; var h=w/5; ctx.fillStyle="grey"; drawRoundRect(ctx,ct.x,ct.y,w,h,h/10); ctx.fill(); ctx.restore(); // #2 外圈 ctx.save(); w-=r*0.02; h-=r*0.02; var top=createPt2(ct.x,ct.y,h/2,-Math.PI/2); var bottom=createPt2(ct.x,ct.y,h/2,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(250,250,250)"); gnt.addColorStop(1,"rgb(230,222,217)"); ctx.fillStyle=gnt; drawRoundRect(ctx,ct.x,ct.y,w,h,h/10-r*0.01); ctx.fill(); ctx.restore(); // #3 黑底 ctx.save(); w-=r*0.02; h-=r*0.02; var top=createPt2(ct.x,ct.y,h/2,-Math.PI/2); var bottom=createPt2(ct.x,ct.y,h/2,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(250,250,250)"); gnt.addColorStop(1,"rgb(230,222,217)"); ctx.fillStyle="black"; drawRoundRect(ctx,ct.x,ct.y,w,h,h/10-r*0.02); ctx.fill(); ctx.restore(); // #4 伞logo ctx.save(); var center=createPt2(ct.x,ct.y,w/2*0.8,Math.PI); var r=h*0.4; ctx.fillStyle="white";// 白轮廓 drawAmbrella(ctx,center.x,center.y,r,Math.PI/8,r/10); ctx.fill(); var r=h*0.4*0.90; ctx.lineWidth=R/220*1; ctx.strokeStyle="black";// 边框 drawAmbrella(ctx,center.x,center.y,r,Math.PI/8,r/10); ctx.stroke(); var r=h*0.4*0.85; drawAmbrella(ctx,center.x,center.y,r,Math.PI/8,r/10); ctx.clip(); // 限制范围 var r=h*0.4*0.80; for(var i=0;i<8;i++){// 红白瓣 var theta=Math.PI/4*i+Math.PI/8; var a=createPt2(center.x,center.y,r,theta); var b=createPt2(center.x,center.y,r,theta+Math.PI/4); ctx.fillStyle=(i%2==0)?gnt:"rgb(177,4,32)"; ctx.beginPath(); ctx.moveTo(center.x,center.y); ctx.lineTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.closePath(); ctx.fill(); } for(var i=0;i<4;i++){// 分瓣线 var theta=Math.PI/4*i+Math.PI/8; var a=createPt2(center.x,center.y,r,theta); var b=createPt2(center.x,center.y,r,theta+Math.PI); ctx.strokeStyle="black"; ctx.beginPath(); ctx.lineTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.stroke(); } ctx.restore(); // #5 文字 ctx.save(); var r=R*1.00; ctx.scale(1.3,1); writeText(ctx,ct.x+r*0.12,ct.y+r*0.05,"UMBRELLA",r*0.20+"px Noto Sans SC Black",gnt); writeText(ctx,ct.x+r*0.12,ct.y+r*0.15,"Corporation",r*0.1+"px Noto Sans SC Light",gnt); ctx.restore(); // #6 玻璃光 ctx.save(); w-=r*0.02; h-=r*0.02; drawRoundRect(ctx,ct.x,ct.y,w,h,h*0.04); ctx.clip(); var top=createPt2(ct.x,ct.y,h/2,-Math.PI/2); var bottom=createPt2(ct.x,ct.y,h/2,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgba(255,255,255,0.4)"); gnt.addColorStop(0.1,"rgba(255,255,255,0.3)"); gnt.addColorStop(0.2,"rgba(255,255,255,0.2)"); gnt.addColorStop(0.3,"rgba(255,255,255,0.1)"); gnt.addColorStop(0.4,"rgba(255,255,255,0.0)"); gnt.addColorStop(1,"rgba(255,255,255,0.0)"); ctx.fillStyle=gnt; drawRect(ctx,ct.x,ct.y,w,h); ctx.fill(); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制安布雷拉伞外轮廓 ctx:绘图上下文 x:轮廓中心横坐标 y:轮廓中心纵坐标 R:伞中心到尖端半径 initAngle:初始角度 round:圆角半径 ----------------------------------------------------------*/ function drawAmbrella(ctx,x,y,R,initAngle,round){ var ct=createPt(x,y);// ct=center var arr=[];// 放端点的数组 for(var i=0;i<8;i++){ var theta=Math.PI/4*i+initAngle; var a=createPt2(ct.x,ct.y,R,theta);// 当前角 var b=createPt2(ct.x,ct.y,R,theta+Math.PI/4);// 下一角 var c=createPt(0,0);// 以ab为对称轴,ct的镜像点,即圆弧的圆心 c.x=a.x+b.x-ct.x; c.y=a.y+b.y-ct.y; var startAngle=theta-Math.PI/4*3;// 起始角 var endAngle=theta-Math.PI;// 结束角 var unit={"o":c,r:R,"startAngle":startAngle,"endAngle":endAngle,}; arr.push(unit); } var biasAngle=round/R;// 圆角半径占据的偏移角 ctx.beginPath(); for(var i=0;i<arr.length;i++){ var unit=arr[i]; ctx.arc(unit.o.x,unit.o.y,unit.r,unit.startAngle-biasAngle,unit.endAngle+biasAngle,true);// 画圆弧 var a=createPt2(unit.o.x,unit.o.y,unit.r,unit.endAngle+biasAngle);// 贝塞尔曲线起点 var b=createPt2(unit.o.x,unit.o.y,unit.r,unit.endAngle);// 贝塞尔曲线终点 var nextUnitIdx=(i+1)%(arr.length); var nextUnit=arr[nextUnitIdx];// 得到下一个顶点 var c=createPt2(nextUnit.o.x,nextUnit.o.y,nextUnit.r,nextUnit.startAngle-biasAngle);// 贝塞尔曲线控制点 ctx.quadraticCurveTo(b.x,b.y,c.x,c.y);// 用贝塞尔曲线光滑连接圆弧 } ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 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号