【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 = "white"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); ctx.restore(); const R=210;// 基准尺寸 // 第一圈 ctx.save(); var r=R*1.00; ctx.shadowOffsetX=r/30; ctx.shadowOffsetY=r/30; ctx.shadowColor="grey"; ctx.shadowBlur=6; var gdnt=ctx.createLinearGradient(-r,-r,r,r); gdnt.addColorStop(0,"rgb(255,216,208)"); gdnt.addColorStop(1,"grey"); ctx.fillStyle=gdnt; drawRoundRect(ctx,0,0,2*r,2*r,R/21); ctx.fill(); ctx.restore(); // 第二圈木框 ctx.save(); var r=R*0.99; var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色 gdnt.addColorStop(0,"rgb(254,208,170)"); gdnt.addColorStop(0.5,"rgb(255,216,208)"); gdnt.addColorStop(1,"rgb(229,169,81)"); ctx.fillStyle=gdnt; drawRoundRect(ctx,0,0,2*r,2*r,R/21); ctx.fill(); ctx.restore(); // 第三 ctx.save(); var r=R*0.97; var gdnt=ctx.createLinearGradient(-r,-r,r,r); gdnt.addColorStop(0,"rgb(229,169,81)"); gdnt.addColorStop(1,"rgb(254,208,170)"); ctx.fillStyle=gdnt; drawRoundRect(ctx,0,0,2*r,2*r,R/21); ctx.fill(); ctx.restore(); // 白圈 ctx.save(); var r=R*0.96; var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色 ctx.fillStyle="lightgrey"; drawRoundRect(ctx,0,0,2*r,2*r,R/21); ctx.fill(); ctx.restore(); // 深绿底 ctx.save(); var r=R*0.90; var gdnt=ctx.createLinearGradient(-r,-r,r,r);// 整体渐变色 ctx.fillStyle="rgb(16,36,41)"; drawRoundRect(ctx,0,0,2*r,2*r,R/21); ctx.fill(); ctx.restore(); // 画铁艺图案 ctx.save(); var r=R*0.36; drawSixFish(ctx,r); ctx.restore(); // 玻璃光 var r=R*0.80; draw9Glasses(ctx,r); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制铁艺图案 ctx:绘图上下文 radius:图案基准半径 ----------------------------------------------------------*/ function drawSixFish(ctx,radius){ ctx.save(); const R=radius;// 六边形短边长 const D=R*1.2;// 六边形长边长 var N=12;// 边数 ctx.lineWidth=1; var gnt=ctx.createLinearGradient(-R,-R,R,R);// 左上到右下径向渐变色 gnt.addColorStop(0, "rgb(254,253,189)"); gnt.addColorStop(1, "rgb(204,154,69)"); ctx.strokeStyle=gnt; ctx.fillStyle=gnt; var r=R; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i; var a=createPt(0,0); r=R; var angle=theta-Math.PI/3; var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=D; angle=theta; var c=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle)); r=R; angle=theta+Math.PI/3; var d=createPt(c.x+r*Math.cos(angle),c.y+r*Math.sin(angle)); r=R; angle=theta+Math.PI/3*2; var e=createPt(d.x+r*Math.cos(angle),d.y+r*Math.sin(angle)); r=D; angle=theta+Math.PI; var f=createPt(e.x+r*Math.cos(angle),e.y+r*Math.sin(angle)); ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.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.closePath(); ctx.stroke(); } // 六叶 N=6; r=R; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i; var a=createPt(0,0); r=R; angle=theta; var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=R/2*Math.sqrt(6); angle=theta+Math.PI/12; var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=R; angle=theta+Math.PI/6; var d=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); ctx.save(); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.fill(); ctx.restore(); } // 六燕 N=6; r=R; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i+Math.PI/6; r=R*Math.sqrt(3); var angle=theta; var a=createPt(r*Math.cos(angle),r*Math.sin(angle)); r=D-R; angle=theta-Math.PI/6; var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=(D+R)/2*Math.sqrt(3)*Math.tan(Math.PI/12)-(D-R)/2; angle=theta-Math.PI/2; var c=createPt(b.x+r*Math.cos(angle),b.y+r*Math.sin(angle)); r=D+R; angle=theta; var d=createPt(r*Math.cos(angle),r*Math.sin(angle)); r=D-R; angle=theta+Math.PI/6; var f=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=(D+R)/2*Math.sqrt(3)*Math.tan(Math.PI/12)-(D-R)/2; angle=theta+Math.PI/2; var e=createPt(f.x+r*Math.cos(angle),f.y+r*Math.sin(angle)); ctx.save(); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.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.closePath(); ctx.fill(); ctx.restore(); } // 六点 N=6; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i+Math.PI/6; r=R*Math.sqrt(3)*0.8; var angle=theta; var pt=createPt(r*Math.cos(angle),r*Math.sin(angle)); drawSolidCircle(ctx,pt.x,pt.y,5,gnt); } ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 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:绘图上下文 radius:基准半径 ----------------------------------------------------------*/ function draw9Glasses(ctx,radius){ ctx.save(); const D=-radius/2; const W=-radius/8; const G=-radius/16; const color="rgba(190,190,190,0.6)"; // 上玻璃光 var alpha=Math.PI/12; var beta=Math.PI/6; for(var i=0;i<3;i++){ var s1=D+i*(W+G) var a=createPt(s1,s1*Math.tan(alpha)); var b=createPt(s1,s1*Math.tan(alpha+beta)); var s2=D+W+i*(W+G); var d=createPt(s2,s2*Math.tan(alpha)); var c=createPt(s2,s2*Math.tan(alpha+beta)); ctx.fillStyle=color; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.fill(); } // 上玻璃光 var alpha=-Math.PI/10; var beta=Math.PI/6; for(var i=0;i<3;i++){ var s1=D+i*(W+G) var a=createPt(s1,s1*Math.tan(alpha)); var b=createPt(s1,s1*Math.tan(alpha+beta)); var s2=D+W+i*(W+G); var d=createPt(s2,s2*Math.tan(alpha)); var c=createPt(s2,s2*Math.tan(alpha+beta)); ctx.fillStyle=color; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.fill(); } // 下玻璃光 var alpha=-Math.PI/4.1; var beta=Math.PI/8; for(var i=0;i<3;i++){ var s1=D+i*(W+G) var a=createPt(s1,s1*Math.tan(alpha)); var b=createPt(s1,s1*Math.tan(alpha+beta)); var s2=D+W+i*(W+G); var d=createPt(s2,s2*Math.tan(alpha)); var c=createPt(s2,s2*Math.tan(alpha+beta)); ctx.fillStyle=color; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.fill(); } ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制实心圆,用途是标记点以辅助作图 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="middle"; ctx.textAlign="center"; ctx.font = font; ctx.fillStyle=color; ctx.fillText(text,x,y); ctx.restore(); } /*------------------------------------------------------------- 所谓的中年危机, 真正让人焦虑的不是孤单,不是贫穷,更不是衰老, 而是人到中年,你才发现你从来都没有按照自己喜欢的方式活过, 这烟火人间事事值得,事事也遗憾呀! 该用多懂事的理智去压抑心中难过和不甘。 我们一直在应对生活中各种苦难和挑战,却忘记了按照自己喜欢的方式活着。 人只有到了中年阶段,才意识到为自己而活,过自己喜欢的生活, 才不会辜负此生。 --余华 --------------------------------------------------------------*/ //--> </script>
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-04-18 使用枚举类Enum作为callee和caller的约定,运用反射消除分支和重复代码在命令式程序中的应用
2018-04-18 【Nodejs】使用nimble串行化回调任务
2018-04-18 【js】利用闭包消除回调函数启动时值已经发生变化的影响