【Canvas技法】绘制横向多色旗和竖向多色旗
【需求】
各国国旗中不少为横向和纵向多色旗,需要两个函数简化代码。
【成图】
【关键函数】
/*---------------------------------------------------------- 函数:用于绘制竖向多色旗,依赖createPt函数 ctx:绘图上下文 x:旗中心横坐标 y:旗中心纵坐标 w:旗宽 h:旗高 colors:颜色数组 ----------------------------------------------------------*/ function drawVerticalColorsFlag(ctx,x,y,w,h,colors){ ctx.save(); var n=colors.length; var leftTop=createPt(x-w/2,y-h/2); for(var i=0;i<n;i++){ ctx.fillStyle=colors[i]; ctx.fillRect(leftTop.x+i*w/n,leftTop.y,w/n,h); } ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制横向多色旗,依赖createPt函数 ctx:绘图上下文 x:旗中心横坐标 y:旗中心纵坐标 w:旗宽 h:旗高 colors:颜色数组 ----------------------------------------------------------*/ function drawHorizontalColorsFlag(ctx,x,y,w,h,colors){ ctx.save(); var n=colors.length; var leftTop=createPt(x-w/2,y-h/2); for(var i=0;i<n;i++){ ctx.fillStyle=colors[i]; ctx.fillRect(leftTop.x,leftTop.y+i*h/n,w,h/n); } ctx.restore(); }
【所有代码】
<!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 colors=["rgb(255,219,0)","rgb(12,18,141)","rgb(216,30,2)"];// 哥伦比亚旗黑红黄三色 drawHorizontalColorsFlag(ctx,-r/2,-r/2,r*0.9,r*0.9*3/5,colors); var colors=["rgb(234,40,57)","rgb(26,32,109)","rgb(255,213,0)","rgb(0,165,81)"];// 毛里求斯四色旗 drawHorizontalColorsFlag(ctx,+r/2,-r/2,r*0.9,r*0.9*3/5,colors); var colors=["rgb(0,70,174)","rgb(255,210,0)","rgb(204,9,47)"];// 摩尔多瓦竖向三色旗 drawVerticalColorsFlag(ctx,-r/2,+r/2,r*0.9,r*0.9*3/5,colors); var colors=["red","orange","yellow","green","cyan"];// 红橙黄绿青五色 drawVerticalColorsFlag(ctx,+r/2,+r/2,r*0.9,r*0.9*3/5,colors); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制竖向多色旗,依赖createPt函数 ctx:绘图上下文 x:旗中心横坐标 y:旗中心纵坐标 w:旗宽 h:旗高 colors:颜色数组 ----------------------------------------------------------*/ function drawVerticalColorsFlag(ctx,x,y,w,h,colors){ ctx.save(); var n=colors.length; var leftTop=createPt(x-w/2,y-h/2); for(var i=0;i<n;i++){ ctx.fillStyle=colors[i]; ctx.fillRect(leftTop.x+i*w/n,leftTop.y,w/n,h); } ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制横向多色旗,依赖createPt函数 ctx:绘图上下文 x:旗中心横坐标 y:旗中心纵坐标 w:旗宽 h:旗高 colors:颜色数组 ----------------------------------------------------------*/ function drawHorizontalColorsFlag(ctx,x,y,w,h,colors){ ctx.save(); var n=colors.length; var leftTop=createPt(x-w/2,y-h/2); for(var i=0;i<n;i++){ ctx.fillStyle=colors[i]; ctx.fillRect(leftTop.x,leftTop.y+i*h/n,w,h/n); } 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