【Canvas与艺术】绘制黄底红台白芯辐射Radiation标志
【成果图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>绘制黄底红台白芯辐射标志</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> <img id="myImg" src="125.png" style="display:none;"/> </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){ window.requestAnimationFrame(animate); } } // 舞台类 function Stage(){ // 初始化 this.init=function(){ } // 更新 this.update=function(){ } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 // 黄底 ctx.fillStyle="rgb(255,211,78)"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); ctx.save(); ctx.rotate(Math.PI/4); // 红台 drawRoundRect(ctx,0,0,270,270,8); ctx.fillStyle="RGB(212,13,18)"; ctx.fill(); // 白芯 drawRoundRect(ctx,0,0,210,210,6); ctx.fillStyle="RGB(248,252,253)"; ctx.fill(); ctx.restore(); // 三瓣 var R=75,r=30; for(var i=0;i<3;i++){ var start=Math.PI*2/3*i-Math.PI/3; var end=start+Math.PI/3; var s1=createPt(r*Math.cos(start),r*Math.sin(start)); var s2=createPt(R*Math.cos(start),R*Math.sin(start)); var e2=createPt(R*Math.cos(end),R*Math.sin(end)); var e1=createPt(r*Math.cos(end),r*Math.sin(end)); ctx.beginPath(); ctx.moveTo(s1.x,s1.y); ctx.lineTo(s2.x,s2.y); ctx.arc(0,0,R,start,end,false); ctx.lineTo(e2.x,e2.y); ctx.lineTo(e1.x,e1.y); ctx.arc(0,0,r,end,start,true); ctx.closePath(); ctx.fillStyle="rgb(51,51,51)"; ctx.fill(); } // 中心圆点 ctx.beginPath(); ctx.arc(0,0,20,0,Math.PI*2,true); ctx.closePath(); ctx.fillStyle="rgb(51,51,51)"; ctx.fill(); // 版权 ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = "8px consolas"; ctx.fillStyle="black"; ctx.fillText("逆火原创",WIDTH/2-40,HEIGHT/2-10); } // 画前景 this.paintFg=function(ctx){ } } /*---------------------------------------------------------- 函数:创建一个二维坐标点 x:横坐标 y:纵坐标 Pt即Point ----------------------------------------------------------*/ function createPt(x,y){ var retval={}; retval.x=x; retval.y=y; return retval; } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 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(); } /*-------------------------------------------------- 老鼠偷了人类的大米,人们说它狡猾; 人类投了蜜蜂的蜂蜜,却说它很勤劳。 ---------------------------------------------------*/ //--> </script>
END