【Canvas与艺术】四叶花
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>386.四叶花</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.fillStyle = "white"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); var colors=[ ["rgb(215,16,23)","rgb(231,32,37)","rgb(165,199,86)","rgb(196,217,126)"], ["rgb(254,194,0)","rgb(255,213,49)","rgb(134,43,138)","rgb(156,72,150)"], ["rgb(0,152,220)","rgb(19,169,230)","rgb(223,98,14)","rgb(238,120,24)"], ["rgb(0,162,145)","rgb(47,182,175)","rgb(183,8,125)","rgb(205,33,141)"], ] const D=10;// 中间方孔边长 const R=40;// 圆弧半径 const G=4;// 中间间隔 const N=4;// 个数 ctx.lineWidth=1; ctx.strokeStyle="black"; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i-Math.PI/4; var r=G*Math.sqrt(2)/2; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); var angle=theta-Math.PI/4; r=R+D; var a1=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); angle=theta+Math.PI/4; r=R; var a2=createPt(a1.x+r*Math.cos(angle),a1.y+r*Math.sin(angle)); angle=theta+Math.PI/4*3; r=D; var a3=createPt(a2.x+r*Math.cos(angle),a2.y+r*Math.sin(angle)); angle=theta-Math.PI/4; r=R; // 圆心 var a4=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); ctx.fillStyle=colors[i][0]; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(a1.x,a1.y); ctx.lineTo(a2.x,a2.y); ctx.lineTo(a3.x,a3.y); ctx.arc(a4.x,a4.y,R,theta+Math.PI/4,theta+Math.PI/4*3,false); ctx.closePath(); ctx.fill(); var b=createPt(a1.x,a1.y); angle=theta+Math.PI/4; r=R; var b1=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));//圆心 angle=theta+Math.PI/4; r=R+D; var b2=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle)); angle=theta+Math.PI/4; r=R*2+D; var b3=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle));//圆心 angle=theta; r=R*Math.sqrt(2); var b4=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle)); ctx.fillStyle=colors[i][1]; ctx.beginPath(); ctx.moveTo(b.x,b.y); ctx.lineTo(b1.x,b1.y); ctx.lineTo(b2.x,b2.y); ctx.arc(b3.x,b3.y,R,theta-Math.PI/4*3,theta-Math.PI/4,false); ctx.lineTo(b4.x,b4.y); ctx.arc(b1.x,b1.y,R,theta-Math.PI/4*1,theta-Math.PI/4*3,true); ctx.closePath(); ctx.fill(); var c=createPt(b2.x,b2.y); angle=theta+Math.PI/4*3; r=D; var c1=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle)); angle=theta+Math.PI/4*1; r=R; var c2=createPt(c1.x+r*Math.cos(angle),c1.y+r*Math.sin(angle)); angle=theta-Math.PI/4*1; r=D; var c3=createPt(c2.x+r*Math.cos(angle),c2.y+r*Math.sin(angle));//圆心 angle=theta-Math.PI/4*1; r=R+D; var c4=createPt(c2.x+r*Math.cos(angle),c2.y+r*Math.sin(angle)); ctx.fillStyle=colors[i][2]; ctx.beginPath(); ctx.moveTo(c.x,c.y); ctx.lineTo(c1.x,c1.y); ctx.lineTo(c2.x,c2.y); ctx.lineTo(c4.x,c4.y); ctx.arc(c3.x,c3.y,R,theta-Math.PI/4*1,theta-Math.PI/4*3,true); ctx.closePath(); ctx.fill(); var d=createPt(c2.x,c2.y); angle=theta-Math.PI/4*3; r=R; var d1=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle));// 圆心 angle=theta+Math.PI/4*3; r=R; var d4=createPt(d1.x+r*Math.cos(angle),d1.y+r*Math.sin(angle)); angle=theta-Math.PI/4*3; r=R+D; var d2=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle)); angle=theta-Math.PI/4*3; r=2*R+D; var d3=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle));// 圆心 ctx.fillStyle=colors[i][3]; ctx.beginPath(); ctx.moveTo(d.x,d.y); ctx.lineTo(d1.x,d1.y); ctx.lineTo(d2.x,d2.y); ctx.arc(d3.x,d3.y,R,theta+Math.PI/4*1,theta+Math.PI/4*3,false); ctx.lineTo(d4.x,d4.y); ctx.arc(d1.x,d1.y,R,theta+Math.PI/4*3,theta+Math.PI/4*1,true); ctx.closePath(); ctx.fill(); } writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火原创","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制实心圆,用途是标记点以辅助作图 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 r:圆半径 color:填充圆的颜色 ----------------------------------------------------------*/ function drawSolidCircle(ctx,x,y,r,color){ ctx.fillStyle=color; ctx.beginPath(); ctx.arc(x,y,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); } /*---------------------------------------------------------- 函数:创建一个二维坐标点 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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步