【Canvas与戳记】美国旗“Happy Presidents Day”戳记
【成图】





【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>美国旗 Happy Presidents Day 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.restore(); const R=220;//基准尺寸 var ct=createPt(0,0);// ct=center // #1 ctx.save(); var r=R*1.00; ctx.fillStyle="rgb(8,35,64)"; drawNStar(ctx,ct.x,ct.y,48,R*1.00,R*0.92,0); ctx.fill(); ctx.restore(); // #2 ctx.save(); var r=R*0.87; drawSolidCircle(ctx,ct.x,ct.y,r,"white"); ctx.restore(); // #3 ctx.save(); var r=R*0.84; drawSolidCircle(ctx,ct.x,ct.y,r,"rgb(8,35,64)"); ctx.restore(); // #4 ctx.save(); var r=R*0.82; drawSolidCircle(ctx,ct.x,ct.y,r,"white"); ctx.restore(); // #5 上下圈文字 ctx.save(); var r=R*0.60; loopText(ctx,ct.x,ct.y,r*1.11,-Math.PI/2,getRadian(60),"HAPPY PRESIDENTS DAY",r*0.20,"rgb(8,35,64)","Noto Sans SC Medium",false);// 上圈文字 loopText(ctx,ct.x,ct.y,r*1.305,Math.PI/2,getRadian(60),"HAPPY PRESIDENTS DAY",r*0.20,"rgb(8,35,64)","Noto Sans SC Medium",true);// 下圈文字 ctx.restore(); // #6 左右圆弧 ctx.save(); var angle=getRadian(25); ctx.fillStyle="rgb(8,35,64)"; drawFan(ctx,ct.x,ct.y,R*0.72,R*0.735,Math.PI-angle,Math.PI+angle);// 左 ctx.fill(); ctx.fillStyle="rgb(8,35,64)"; drawFan(ctx,ct.x,ct.y,R*0.72,R*0.735,0-angle,0+angle);// 右 ctx.fill(); ctx.restore(); // #7 ctx.save(); var r=R*0.63; drawSolidCircle(ctx,ct.x,ct.y,r,"rgb(8,35,64)"); ctx.restore(); // #8 ctx.save(); var r=R*0.62; drawSolidCircle(ctx,ct.x,ct.y,r,"white"); ctx.restore(); // #9 ctx.save(); var r=R*0.60; ctx.beginPath(); ctx.arc(ct.x,ct.y,r,0,Math.PI*2,false); ctx.clip();// 限制范围 drawFlagment(ctx,ct.x,ct.y,r,R); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*------------------------------------------------------------------------ 常用函数:写出环形文字的函数 ctx:绘图上下文 x:环中心点横坐标 y:环中心点纵坐标 r:环半径 centerAngle:文字中心角度 fanAngle:左右辐角 text:文字 textSize:文字大小 textColor:文字颜色 textFont:文字字体 clockwise: 顺逆时针 false=顺时针,true=逆时针 ------------------------------------------------------------------------*/ function loopText(ctx,x,y,r,centerAngle,fanAngle,text,textSize,textColor,textFont,clockwise){ var arr=text.split(""); var n=arr.length; var part=2*fanAngle/n; for(var i=0;i<arr.length;i++){ var theta; if (clockwise==false){ theta=centerAngle-fanAngle+part/2+i*part;// 顺时针的情况 }else{ theta=centerAngle+fanAngle-part/2-i*part;// 逆时针的情况 } var p=createPt2(x,y,r,theta); ctx.save(); ctx.translate(p.x,p.y); if(clockwise==false){ ctx.rotate(theta+Math.PI/2); }else{ ctx.rotate(theta-Math.PI/2); } //writeText(ctx,0,0,arr[i],textSize+"px "+textFont,textColor) ctx.save(); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = textSize+"px "+textFont; ctx.fillStyle=textColor; ctx.fillText(arr[i],0,0); //ctx.lineWidth=1; //ctx.strokeStyle="white"; //ctx.strokeText(arr[i],0,0); ctx.restore(); ctx.restore(); } } /*-------------------------------------------------- 函数:绘制标准正N角星轮廓,可描边,可填充 ctx:绘图上下文 x:轮廓中心横坐标 y:轮廓中心纵坐标 n:角数 rout:中心到外角尖的距离 rin:中心到内角尖的距离 initAngle:初始角度 ---------------------------------------------------*/ function drawNStar(ctx,x,y,n,rout,rin,initAngle){ var arr=new Array(2*n); for(var i=0;i<n;i++){ var theta=Math.PI*2/n*i+initAngle; var p=createPt2(x,y,rout,theta); arr[i*2]=p; } for(var i=0;i<n;i++){ var theta=Math.PI*2/n*i+Math.PI/n+initAngle; var p=createPt2(x,y,rin,theta); arr[i*2+1]=p; } ctx.beginPath(); for(var i=0;i<arr.length;i++){ ctx.lineTo(arr[i].x,arr[i].y); } ctx.closePath(); } // 绘制旗面 function drawFlagment(ctx,x,y,r,R){ const ct=createPt(x,y);// ct=center const RED="rgb(175,31,56)"; const WHITE="white"; const BLUE="rgb(8,35,64)"; var colors=[RED,WHITE,BLUE]; drawUsFlag(ctx,ct.x,ct.y+r/7/2*1.08,r*1.08,colors); } /*---------------------------------------------------------- 函数:指定一个中心点和半径,绘制标准的美国国旗 ctx:绘图上下文 x:中心横坐标 y:中心纵坐标 r:美国旗蓝色矩形的高度,是其它尺寸的基础 colors:存储红白蓝三色的数组 ----------------------------------------------------------*/ function drawUsFlag(ctx,x,y,r,colors){ ctx.save(); var blueBlockRightBottom=createPt(x,y);// 蓝色块的右下角 var c=r;// 左上角蓝色块的高度,按标准星条旗画法标记为c var a=c/7*13;// c占7条高度,整旗高是13条,a即星条旗高度 var b=a*19/10;// b是a的1.9倍,b即星条旗宽度 var d=b*2/5;// d为五分之二的国旗宽度,d为左上角蓝色块的宽度 // 绘制十三横条 var flagCenter=createPt(x-d+b/2,y-c+a/2);// 旗帜中心 fillHorizontalZebraRect(ctx,flagCenter.x,flagCenter.y,b,a,13,colors[0],colors[1]); // 绘制左上角蓝色方块 var blueBlockCenter=createPt(x-d/2,y-c/2);// 蓝色方块中心 ctx.fillStyle=colors[2]; drawRect(ctx,blueBlockCenter.x,blueBlockCenter.y,d,c); ctx.fill(); var h=d/12;// h为星星的横坐标间距 var e=c/10;// e为星星的纵坐标间距 var k=0.0616*a;// 左上角蓝色方块内白色星星的中心到尖角距离,即星星外接圆直径 // 第一批画五行六列星星 for(var i=0;i<6;i++){ for(var j=0;j<5;j++){ ctx.fillStyle = colors[1]; draw5Star(ctx,x-d+h+i*h*2,y-c+e+j*e*2,k/2,0); ctx.fill(); } } // 第一批画五行四列星星 for(var i=0;i<5;i++){ for(var j=0;j<4;j++){ ctx.fillStyle = colors[1]; draw5Star(ctx,x-d+h*2+i*h*2,y-c+e*2+j*e*2,k/2,0); ctx.fill(); } } ctx.restore(); } /*---------------------------------------------------------- 特定函数:用于以横向间隔色条带的方式填充矩形 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 count:条带数 color1:偶数色(从0开始) color2:奇数色 ----------------------------------------------------------*/ function fillHorizontalZebraRect(ctx,x,y,width,height,count,color1,color2){ ctx.save(); for(var i=0;i<count;i++){ ctx.fillStyle=(i % 2==0)?color1:color2; ctx.fillRect(x-width/2, y-height/2+height/count*i, width, height/count); } ctx.restore(); } /*-------------------------------------------------- 基本函数:绘制标准正五角星轮廓,可描边,可填充 ctx:绘图上下文 x:五角星中心横坐标 y:五角星中心纵坐标 R:五角星中心到顶点的距离 ---------------------------------------------------*/ function draw5Star(ctx,x,y,R){ var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7.0); 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:矩形中心纵坐标 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(); } /*---------------------------------------------------------- 基础函数:用于从角度取得弧度,便于微调 degree:角度值,如30,60,90 返回值:弧度值,如PI/6,PI/3,PI/2 ----------------------------------------------------------*/ function getRadian(degree){ return degree/180*Math.PI; } /*---------------------------------------------------------- 基础函数:用于取得两点间距离 p1:起点 p1:终点 ----------------------------------------------------------*/ function getDistance(p1,p2){ return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } /*---------------------------------------------------------- 基础函数:用于绘制扇形 ctx:绘图上下文 x:扇形圆心心横坐标 y:扇形圆心纵坐标 rIn:扇形内径 rOut:扇形外径 startAngle:起始角度 endAngle:中止角度 ----------------------------------------------------------*/ function drawFan(ctx,x,y,rIn,rOut,startAngle,endAngle){ var a=createPt(x+rIn*Math.cos(startAngle),y+rIn*Math.sin(startAngle)); var b=createPt(x+rOut*Math.cos(startAngle),y+rOut*Math.sin(startAngle)); var c=createPt(x+rOut*Math.cos(endAngle),y+rOut*Math.sin(endAngle)); var d=createPt(x+rIn*Math.cos(endAngle),y+rIn*Math.sin(endAngle)); ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.arc(x,y,rOut,startAngle,endAngle,false); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.arc(x,y,rIn,endAngle,startAngle,true); 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(); } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 base:基准点,入参必需有x和y两参数 radius:当前点到基准点的距离 theta:当前点到基准点的角度 Pt即Point ----------------------------------------------------------*/ function createPt3(base,radius,theta){ var retval={}; retval.x=base.x+radius*Math.cos(theta); retval.y=base.y+radius*Math.sin(theta); return retval; } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 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号