【Canvas与诗词】木兰辞节选
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>金边钢底徽章</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=180;// 基准尺寸 // 竖向渐变外框 ctx.save(); var r=R*1.31; var gnt=ctx.createLinearGradient(-r,0,r,0); gnt.addColorStop(0, "rgb(238,230,165)"); gnt.addColorStop(0.25,"rgb(222,183,62)"); gnt.addColorStop(0.5,"rgb(255,255,190)"); gnt.addColorStop(0.6,"rgb(152,97,33)"); gnt.addColorStop(0.8,"rgb(252,238,165)"); gnt.addColorStop(1, "rgb(217,173,74)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 上半圈渐变外框 ctx.save(); var ro=R*1.30;// outer radius var ri=R*1.20;// inner radius var gnt=ctx.createLinearGradient(0,-ro,0,0); gnt.addColorStop(0, "rgb(253,248,183)"); gnt.addColorStop(0.5,"rgb(234,196,99)"); gnt.addColorStop(1,"rgb(254,238,161)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(ri,0); ctx.lineTo(ro,0); ctx.arc(0,0,ro,0,-Math.PI,true); ctx.lineTo(-ri,0); ctx.arc(0,0,ri,-Math.PI,0,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 下半圈渐变外框 ctx.save(); var ro=R*1.30;// outer radius var ri=R*1.20;// inner radius var gnt=ctx.createLinearGradient(0,0,0,ro); gnt.addColorStop(0, "rgb(254,238,161)"); gnt.addColorStop(0.05, "rgb(110,74,48)"); gnt.addColorStop(0.5,"rgb(255,255,195)"); gnt.addColorStop(1,"rgb(238,181,73)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.moveTo(ri,0); ctx.lineTo(ro,0); ctx.arc(0,0,ro,0,Math.PI,false); ctx.lineTo(-ri,0); ctx.arc(0,0,ri,Math.PI,0,true); ctx.closePath(); ctx.fill(); ctx.restore(); // 竖向渐变外框 ctx.save(); var r=R*1.20; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(254,255,191)"); gnt.addColorStop(1, "rgb(198,142,57)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 竖向渐变外框 ctx.save(); var r=R*1.15; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(233,191,89)"); gnt.addColorStop(0.125, "rgb(254,252,191)"); gnt.addColorStop(0.250, "rgb(198,142,57)"); gnt.addColorStop(0.375, "rgb(253,240,170)"); gnt.addColorStop(0.5, "rgb(199,164,96)"); gnt.addColorStop(0.625, "rgb(254,233,126)"); gnt.addColorStop(0.75, "rgb(197,163,57)"); gnt.addColorStop(0.875, "rgb(253,233,164)"); gnt.addColorStop(1, "rgb(160,109,43)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 竖向渐变外框 ctx.save(); var r=R*1.05; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(199,143,58)"); gnt.addColorStop(1, "rgb(255,254,195)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 酱色内圈 ctx.save(); var r=R*1.00; ctx.fillStyle="rgb(92,58,23)"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 竖向渐变内圈 ctx.save(); var r=R*0.98; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0, "rgb(238,230,165)"); gnt.addColorStop(0.25,"rgb(222,183,62)"); gnt.addColorStop(0.5,"rgb(255,255,190)"); gnt.addColorStop(0.6,"rgb(152,97,33)"); gnt.addColorStop(0.8,"rgb(252,238,165)"); gnt.addColorStop(1, "rgb(217,173,74)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 白色内圈 ctx.save(); var r=R*0.97; ctx.fillStyle="white"; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 十字纹 ctx.save(); r=R*0.97; var N=3600; for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); var alpha=Math.abs(Math.cos(theta*2))*0.3; ctx.strokeStyle="rgba(188,187,189,"+alpha+")"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(a.x,a.y); ctx.stroke(); } // backslash蒙版 ctx.save(); r=R*0.97; var gnt=ctx.createLinearGradient(-r,-r,r,r); gnt.addColorStop(0,"rgba(0,0,0,0.15)"); gnt.addColorStop(0.4,"rgba(125,115,116,0.15)"); gnt.addColorStop(0.5,"rgba(155,147,144,0.15)"); gnt.addColorStop(0.6,"rgba(125,115,116,0.15)"); gnt.addColorStop(1,"rgba(0,0,0,0.15)"); ctx.fillStyle=gnt; ctx.beginPath(); ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); ctx.restore(); // 诗正文 var r=R*0.96; var color="rgb(92,58,23)"; var sz=R/5.61; writeText(ctx,0,-r/4*2.5,"木兰辞节选",sz+"32px 方正宋刻本秀楷简体",color); var sz=R/11.2; writeText(ctx,0,-r/4*2.0," ",sz+"16px 方正宋刻本秀楷简体",color); // 诗歌正文 var start=createPt(0,-r/3.5); var gap=r/2.5; var sz=R/6.6; writeText(ctx,start.x,start.y,"万里赴戎机 关山度若飞",sz+"px 方正宋刻本秀楷简体",color); writeText(ctx,start.x,start.y+gap,"朔气传金柝 寒光照铁衣",sz+"px 方正宋刻本秀楷简体",color); writeText(ctx,start.x,start.y+2*gap, "将军百战死 壮士十年归",sz+"px 方正宋刻本秀楷简体",color); 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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)