【Canvas与艺术】绘制地平线
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>使用HTML5/Canvas绘制地平线</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> <img id="myImg" src="170.jpg" style="display:none;"/> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 高宽 const WIDTH=1024; 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 = "black"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); // 划线 for(var i=0;i<33;i++){ var x=-2000+i*125; var y=300; // 斜线 ctx.beginPath(); ctx.moveTo(0,-50); ctx.lineTo(x,y); ctx.lineWidth=1; ctx.strokeStyle="rgb(16,174,213)"; ctx.stroke(); // 横线 var k=350/-x; var y1=k*(-512)+50; ctx.beginPath(); ctx.moveTo(-512,y1-100); ctx.lineTo(512,y1-100); ctx.lineWidth=0.5; ctx.strokeStyle="rgb(16,174,213)"; ctx.stroke(); } // 线性渐变色填充 var lgrd=ctx.createLinearGradient(0,-HEIGHT/2,0,-HEIGHT/2+295); lgrd.addColorStop(0,"purple"); lgrd.addColorStop(1,"rgb(16,174,213)"); ctx.fillStyle=lgrd; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,295); for(var i=0;i<120;i++){ var x=-WIDTH/2+i*10; var y=-HEIGHT/2+295; ctx.beginPath(); ctx.moveTo(x,y+3); ctx.lineTo(x-200,y1-595); ctx.lineWidth=8.6; ctx.strokeStyle="black"; ctx.stroke(); } // 亮线 ctx.beginPath(); ctx.moveTo(-WIDTH/2,-HEIGHT/2+297); ctx.lineTo(WIDTH/2,-HEIGHT/2+297); ctx.lineWidth=2; ctx.strokeStyle="rgb(16,174,213)"; ctx.stroke(); // 地平线下线性渐变色 var lgrd=ctx.createLinearGradient(0,-HEIGHT/2+287,0,-HEIGHT/2+307); lgrd.addColorStop(0,"rgba(255,255,255,0.1)"); lgrd.addColorStop(0.5,"rgba(16,174,213,0.5)"); lgrd.addColorStop(1,"rgba(255,255,255,0.1)"); ctx.fillStyle=lgrd; ctx.fillRect(-WIDTH/2,-HEIGHT/2+295,WIDTH,30); // 文字 writeText(ctx,0,-20,"Canvas","96px Governor Shadow","lightblue"); writeText(ctx,0,18," is funny","36px Ink Free","fuchsia"); // is funny左翼 ctx.beginPath(); ctx.moveTo(-60,2); ctx.lineTo(-100,2); ctx.lineWidth=2; ctx.strokeStyle="fuchsia"; ctx.stroke(); // is funny右翼 ctx.beginPath(); ctx.moveTo(70,2); ctx.lineTo(110,2); ctx.lineWidth=2; ctx.strokeStyle="fuchsia"; ctx.stroke(); writeText(ctx,WIDTH/2-20,HEIGHT/2-5,"逆火原创","8px consolas","white");// 版权 } } /*---------------------------------------------------------- 函数:创建一个二维坐标点 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