【Canvas与诗词】男儿何不带吴钩收取关山五十州
【成图】




【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>男儿何不带吴钩收取关山五十州 Draft1</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;//基准尺寸 // 第1圈 ctx.save(); ctx.shadowOffsetX=R/210*4; // 阴影 ctx.shadowOffsetY=R/210*4; ctx.shadowColor="lightgrey"; ctx.shadowBlur=R/210*4; var r=R*1.00; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0,"rgb(154,83,8)"); gnt.addColorStop(0.25,"rgb(214,148,53)"); gnt.addColorStop(0.40,"rgb(160,86,7)"); gnt.addColorStop(0.5,"rgb(252,244,164)"); gnt.addColorStop(0.75,"rgb(239,226,146)"); gnt.addColorStop(1,"rgb(225,197,112)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // 第2圈 ctx.save(); var r=R*0.98; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0,"rgb(222,197,112)"); gnt.addColorStop(0.25,"rgb(236,217,137)"); gnt.addColorStop(0.5,"rgb(254,248,175)"); gnt.addColorStop(0.60,"rgb(164,110,27)"); gnt.addColorStop(0.80,"rgb(220,156,65)"); gnt.addColorStop(1,"rgb(164,97,24)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // 第3圈 ctx.save(); var r=R*0.88; var gnt=ctx.createLinearGradient(0,-r,0,r); gnt.addColorStop(0,"rgb(150,84,15)"); gnt.addColorStop(0.25,"rgb(231,163,61)"); gnt.addColorStop(0.4,"rgb(150,80,14)"); gnt.addColorStop(0.5,"rgb(255,254,183)"); gnt.addColorStop(0.75,"rgb(241,223,140)"); gnt.addColorStop(1,"rgb(239,214,126)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // 第4圈 ctx.save(); var r=R*0.86; var gnt=ctx.createRadialGradient(0,0,0,0,0,r); gnt.addColorStop(0,"rgb(242,221,82)"); gnt.addColorStop(0.3,"rgb(242,221,82)"); gnt.addColorStop(0.5,"rgb(242,221,82)"); gnt.addColorStop(0.75,"rgb(242,221,82)"); gnt.addColorStop(0.95,"rgb(242,221,82)"); gnt.addColorStop(0.97,"rgb(179,171,128)"); gnt.addColorStop(1,"rgb(77,78,82)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // 彩带1 ctx.save(); var r=R*0.86; ctx.arc(0,0,r,0,Math.PI*2,false); ctx.clip(); ctx.rotate(-Math.PI/4); var colors=["rgb(0,97,148)","rgb(255,255,255)","rgb(191,0,31)"]; var w=r*2/3; var h=2*r; for(var i=0;i<3;i++){ ctx.fillStyle=colors[i]; drawRect(ctx,(i-1)*w,0,w,h); ctx.fill(); } ctx.restore(); // 诗歌 ctx.save(); var r=R*0.93; var start=createPt(0,-r/3.05); var gap=r/3.5; var sz=R/5.6; var sentence="男儿何不带吴钩"; printWords(ctx,start.x,start.y,sz,R/210*3,"男儿何不带吴钩"); printWords(ctx,start.x,start.y+gap,sz,R/210*3,"收取关山五十州"); printWords(ctx,start.x,start.y+2*gap,sz,R/210*3,"请君暂上凌烟阁"); printWords(ctx,start.x,start.y+3*gap,sz,R/210*3,"若个书生万户侯"); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } function printWords(ctx,x,y,size,lineWidth,words){ ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = size+"px 方正宋刻本秀楷简体"; ctx.lineWidth=lineWidth; ctx.strokeStyle="white"; ctx.strokeText(words,x,y); ctx.fillStyle="black"; ctx.fillText(words,x,y); } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 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:矩形中心纵坐标 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(); } /*---------------------------------------------------------- 函数:创建一个二维坐标点 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号