【Canvas与玻璃光】粉框黄铜之眼
【成图】





【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>粉框黄铜之眼 Draft3</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 = "navy"; //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; ctx.fillStyle="rgb(137,27,26)"; drawRoundRect(ctx,ct.x,ct.y,2*r,2*r,r/6); ctx.fill(); ctx.restore(); // #2 上半框 ctx.save(); drawRoundRect(ctx,ct.x,ct.y,2*r,2*r,r/6); ctx.clip();// 限制绘制范围 var top=createPt3(ct,r,-Math.PI/2); var bottom=createPt3(ct,r,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(254,97,99)"); gnt.addColorStop(1,"rgb(228,74,74)"); var w=2*r;//半框宽 var gap=r/40;// 上下半框间距的一半 var h=r-gap;// 半框高 var radius=r*0.8;// 半框内圆半径 var theta=Math.asin(gap/radius);// a,b两点升起的角度 var round=radius/12;// 圆角半径 var roundAngle=round/radius;// a2\b1两点升起的角度 var a=createPt3(ct,radius,-theta); var b=createPt3(ct,radius,Math.PI+theta); var c=createPt(ct.x-w/2,ct.y-gap); var d=createPt(ct.x-w/2,ct.y-h-gap); var e=createPt(ct.x+w/2,ct.y-h-gap); var f=createPt(ct.x+w/2,ct.y-gap); var a1=createPt3(a,round,0); var a2=createPt3(ct,radius,-theta-roundAngle); var b1=createPt3(ct,radius,Math.PI+theta+roundAngle); var b2=createPt3(b,round,Math.PI); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(a2.x,a2.y); ctx.arc(ct.x,ct.y,radius,-theta-roundAngle,Math.PI+theta+roundAngle,true); ctx.quadraticCurveTo(b.x,b.y,b2.x,b2.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.lineTo(e.x,e.y); ctx.lineTo(f.x,f.y); ctx.lineTo(a1.x,a1.y); ctx.quadraticCurveTo(a.x,a.y,a2.x,a2.y); ctx.closePath(); ctx.fill(); ctx.restore(); // #2-上半框内圈 ctx.save(); var a=createPt3(ct,radius,-theta); var b=createPt3(ct,radius,Math.PI+theta); var c=createPt(ct.x-w/2,ct.y-gap); var d=createPt(ct.x-w/2,ct.y-h-gap); var e=createPt(ct.x+w/2,ct.y-h-gap); var f=createPt(ct.x+w/2,ct.y-gap); var a1=createPt3(a,round,0); var a2=createPt3(ct,radius,-theta-roundAngle); var b1=createPt3(ct,radius,Math.PI+theta+roundAngle); var b2=createPt3(b,round,Math.PI); ctx.beginPath(); ctx.moveTo(a2.x,a2.y); ctx.arc(ct.x,ct.y,radius,-theta-roundAngle,Math.PI+theta+roundAngle,true); ctx.quadraticCurveTo(b.x,b.y,b2.x,b2.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.lineTo(e.x,e.y); ctx.lineTo(f.x,f.y); ctx.lineTo(a1.x,a1.y); ctx.quadraticCurveTo(a.x,a.y,a2.x,a2.y); ctx.closePath(); ctx.clip(); drawSolidCircle(ctx,ct.x,ct.y,radius+round,"rgb(235,83,80)"); ctx.restore(); // #3 下半框 ctx.save(); drawRoundRect(ctx,ct.x,ct.y,2*r,2*r,r/6); ctx.clip();// 限制绘制范围 var a=createPt3(ct,radius,theta); var b=createPt3(ct,radius,Math.PI-theta); var c=createPt(ct.x-w/2,ct.y+gap); var d=createPt(ct.x-w/2,ct.y+h+gap); var e=createPt(ct.x+w/2,ct.y+h+gap); var f=createPt(ct.x+w/2,ct.y+gap); var a1=createPt3(a,round,0); var a2=createPt3(ct,radius,theta+roundAngle); var b1=createPt3(ct,radius,Math.PI-theta-roundAngle); var b2=createPt3(b,round,Math.PI); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(a2.x,a2.y); ctx.arc(ct.x,ct.y,radius,+theta+roundAngle,Math.PI-theta-roundAngle,false); ctx.quadraticCurveTo(b.x,b.y,b2.x,b2.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.lineTo(e.x,e.y); ctx.lineTo(f.x,f.y); ctx.lineTo(a1.x,a1.y); ctx.quadraticCurveTo(a.x,a.y,a2.x,a2.y); ctx.closePath(); ctx.fill(); ctx.strokeStyle="white";// 下框左白线 ctx.lineWidth=R/220*1; ctx.beginPath(); ctx.moveTo(b2.x,b2.y); ctx.lineTo(c.x,c.y); ctx.stroke(); ctx.strokeStyle="white";// 下框右白线 ctx.lineWidth=R/220*1; ctx.beginPath(); ctx.moveTo(f.x,f.y); ctx.lineTo(a1.x,a1.y); ctx.stroke(); ctx.restore(); // #3-下半框内圈 ctx.save(); var a=createPt3(ct,radius,theta); var b=createPt3(ct,radius,Math.PI-theta); var c=createPt(ct.x-w/2,ct.y+gap); var d=createPt(ct.x-w/2,ct.y+h+gap); var e=createPt(ct.x+w/2,ct.y+h+gap); var f=createPt(ct.x+w/2,ct.y+gap); var a1=createPt3(a,round,0); var a2=createPt3(ct,radius,theta+roundAngle); var b1=createPt3(ct,radius,Math.PI-theta-roundAngle); var b2=createPt3(b,round,Math.PI); ctx.beginPath(); ctx.moveTo(a2.x,a2.y); ctx.arc(ct.x,ct.y,radius,+theta+roundAngle,Math.PI-theta-roundAngle,false); ctx.quadraticCurveTo(b.x,b.y,b2.x,b2.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.lineTo(e.x,e.y); ctx.lineTo(f.x,f.y); ctx.lineTo(a1.x,a1.y); ctx.quadraticCurveTo(a.x,a.y,a2.x,a2.y); ctx.closePath(); ctx.clip(); drawSolidCircle(ctx,ct.x,ct.y,radius+round,"rgb(251,98,93)"); ctx.restore(); // #4 黄铜圈 ctx.save(); var r=R*0.75; var top=createPt3(ct,r,-Math.PI/2); var bottom=createPt3(ct,r,Math.PI/2); var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y); gnt.addColorStop(0,"rgb(242,209,149)"); gnt.addColorStop(1,"rgb(203,146,39)"); drawSolidCircle(ctx,ct.x,ct.y,r,gnt); ctx.restore(); // #5 黑圈 ctx.save(); var r=R*0.58; drawSolidCircle(ctx,ct.x,ct.y,r,"black"); ctx.restore(); // #6 ctx.save(); var r=R*0.575; drawSolidCircle(ctx,ct.x,ct.y,r,"rgb(99,67,29)"); ctx.restore(); // #7 ctx.save(); var r=R*0.45; drawSolidCircle(ctx,ct.x,ct.y,r,"black"); ctx.restore(); // #8 ctx.save(); var r=R*0.445; drawSolidCircle(ctx,ct.x,ct.y,r,"rgb(101,69,30)"); ctx.restore(); // #9 ctx.save(); var r=R*0.40; drawSolidCircle(ctx,ct.x,ct.y,r,"black"); ctx.restore(); // #10 ctx.save(); var r=R*0.39; drawSolidCircle(ctx,ct.x,ct.y,r,"rgb(45,28,8)"); ctx.restore(); // #11 ctx.save(); var r=R*0.23; drawSolidCircle(ctx,ct.x,ct.y,r,"black"); ctx.restore(); // #12 反光点 ctx.save(); var r=R*0.06; drawSolidCircle(ctx,ct.x+1.5*r,ct.y-1.6*r,r*0.9,"rgb(218,189,157)"); ctx.restore(); // #13 玻璃光 ctx.save(); var r=R*0.75; var a=createPt3(ct,r,-Math.PI/2+Math.PI/4); var b=createPt3(ct,r,-Math.PI/2-Math.PI/4); var c=createPt(a.x+b.x-ct.x,a.y+b.y-ct.y); ctx.fillStyle="rgba(190,190,190,0.3)"; ctx.beginPath(); ctx.arc(ct.x,ct.y,r,-Math.PI/2+Math.PI/4,-Math.PI/2-Math.PI/4,true); ctx.arc(c.x,c.y,r,Math.PI/2+Math.PI/4,Math.PI/2-Math.PI/4,true); ctx.fill(); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 基础函数:用于从角度取得弧度,便于微调 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:矩形中心纵坐标 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:矩形中心纵坐标 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:矩形中心纵坐标 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(); } /*------------------------------------------------------------- 很多事情 要轻轻地做 这是一个几乎可以做成任何事的顶级心法。做事不能太用力,所谓欲速则不达,很多事情你把它看得太重,反而拿不起来,也做不成。 之前看到过一个观点,靠消耗意志力做的事情,是不会长久的,因为人的心力是有限的,人这辈子能提起劲的次数是有限的,就像一罐煤气,总开大火,很快就烧干了。很多同学高中拼命学了三年之后,大学就直接躺平摆烂了,我愿称之为是因为高中时期“学伤了”,学得太用力过猛了,心力和体力都透支了,之后就只能懈怠了。真正能持续做事的人,不是靠咬牙切齿的自律,而是轻轻地把事接住,慢慢把它融入生活。 在心理学中有个“耶克斯-多德森定律”,讲的是动机强度和做事效率之间的关系呈一个倒U型,当动机水平适中时,效率才是最高的,动机强度太高,反而会对行为产生阻碍作用,也就是我说的这件事“拿不起来”。只有保持适度水平,细水长流,在不紧不慢中找到属于自己的节奏,张弛有度,持续精进,保持一定的松弛感,就会慢慢实现自己的目标。 我自己写作就是这样,也经历过“写不动”的阶段。后来我学会一件事:不逼自己非得写出一篇完整的文章。我只要求自己,打开文档,写下一句话。就一句。而往往,写下第一句,第二句就跟着来了。就像推一个静止的轮子,启动最难,轻轻一拨,它自己就会转。 再比如庄子里“庖丁解牛”的故事,庖丁的刀用了十九年还像新的一样,因为他解牛时“以无厚入有间,恢恢乎其于游刃必有余地矣”,他不是去砍、去劈,而是顺着牛的肌理,轻轻地游走。他做的不是最费力的,却是最有效的。成语中有一个对应的词,叫“举重若轻”,道理也是如此。 如果你想坚持下去并且成事,那我建议你目标要往低定,每天从容的开始。比如不要强制要求自己一天看50页书、背50个单词,而是想着今天翻两三页书就可以了,甚至拿起书本就可以了,一旦你轻轻的开始,不再赋予行动沉重的意义,你反而更容易坚持下来。 以上转载自网络
--------------------------------------------------------------*/ //--> </script>
END
浙公网安备 33010602011771号