【Canvas与电脑桌面】白黄色蜂巢电脑壁纸
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>黄白色蜂巢电脑壁纸 1920x1080 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=1920; const HEIGHT=1080; // 舞台对象 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 = "rgb(63,47,14)"; ctx.fillRect(0,0,WIDTH,HEIGHT); const D=120;// 中心距 const R=D/2;// 作图半径(最远的勾画距离) const COLUMN_CNT=Math.ceil(WIDTH*2/D)+1;// 列数 const ROW_CNT=Math.ceil(HEIGHT*2/D)+1;// 行数 for(var col=0;col<COLUMN_CNT;col++){ for(var row=0;row<ROW_CNT;row++){ var p=createPt(col*D/Math.sqrt(3),row*D*2); if(col%2==0){ p.y-=D; drawYellowHiveUnit(ctx,p.x,p.y,D*0.63); }else{ drawWhiteHiveUnit(ctx,p.x,p.y,D*0.63); } } } // 蒙版 ctx.fillStyle="rgba(9,9,9,0.1)"; ctx.fillRect(0,0,WIDTH,HEIGHT); writeText(ctx,WIDTH-30,HEIGHT-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制蜂巢的一个白色单元 ctx:绘图上下文 x:单元中心横坐标 y:单元中心纵坐标 r:单元中心到顶点的距离 ----------------------------------------------------------*/ function drawWhiteHiveUnit(ctx,x,y,R){ var ct=createPt(x,y);// ct=center ctx.save(); var r=R*1.00;// 外圈 var left=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,Math.PI); var right=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,0); var gnt=ctx.createLinearGradient(left.x,left.y,right.x,right.y); gnt.addColorStop(0,"rgb(203,203,203)"); gnt.addColorStop(0.25,"rgb(157,157,157)"); gnt.addColorStop(0.5,"rgb(200,200,200)"); gnt.addColorStop(0.75,"rgb(157,157,157)"); gnt.addColorStop(1,"rgb(190,190,190)"); ctx.fillStyle=gnt; drawPolygon(ctx,6,ct.x,ct.y,r,Math.PI/6); ctx.fill(); var r=R*0.94;// 内圈 var left=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,Math.PI); var right=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,0); var gnt=ctx.createLinearGradient(left.x,left.y,right.x,right.y); gnt.addColorStop(0,"rgb(80,60,47)"); gnt.addColorStop(1,"rgb(232,232,232)"); ctx.fillStyle=gnt; drawPolygon(ctx,6,ct.x,ct.y,r,Math.PI/6); ctx.fill(); // 上半部分 var a=createPt2(ct.x,ct.y,r,Math.PI-Math.PI/6); var b=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6); var c=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/2); var d=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/2+Math.PI/3); var gnt=ctx.createLinearGradient(b.x,b.y,d.x,d.y); gnt.addColorStop(0,"rgb(196,191,183)"); gnt.addColorStop(0.25,"rgb(160,152,139)"); gnt.addColorStop(0.5,"rgb(132,123,106)"); gnt.addColorStop(0.75,"rgb(147,140,124)"); gnt.addColorStop(1,"rgb(179,173,162)"); 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(); // 六颗螺钉 var r=R*0.84; var radius=r*0.05; for(var i=0;i<6;i++){ var theta=i*Math.PI/3+Math.PI/6; var p=createPt2(ct.x,ct.y,r,theta); var gnt=ctx.createRadialGradient(p.x,p.y,0,p.x,p.y,radius); gnt.addColorStop(0,"rgb(124,124,124)"); gnt.addColorStop(1,"rgb(66,63,55)"); drawSolidCircle(ctx,p.x,p.y,radius,gnt); } ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制蜂巢的一个黄色单元 ctx:绘图上下文 x:单元中心横坐标 y:单元中心纵坐标 r:单元中心到顶点的距离 ----------------------------------------------------------*/ function drawYellowHiveUnit(ctx,x,y,R){ var ct=createPt(x,y);// ct=center ctx.save(); var r=R*1.00;// 外圈 ctx.fillStyle="rgb(237,145,34)"; drawPolygon(ctx,6,ct.x,ct.y,r,Math.PI/6); ctx.fill(); var r=R*0.94;// 内圈 var left=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,Math.PI); var right=createPt2(ct.x,ct.y,r*Math.sqrt(3)/2,0); var gnt=ctx.createLinearGradient(left.x,left.y,right.x,right.y); gnt.addColorStop(0,"rgb(90,60,26)"); gnt.addColorStop(1,"rgb(244,171,69)"); ctx.fillStyle=gnt; drawPolygon(ctx,6,ct.x,ct.y,r,Math.PI/6); ctx.fill(); // 玻璃光 var a=createPt2(ct.x,ct.y,r,Math.PI-Math.PI/6); var b=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/6); var c=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/2); var d=createPt2(ct.x,ct.y,r,Math.PI+Math.PI/2+Math.PI/3); ctx.fillStyle="rgba(190,190,190,0.3)"; 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:正多边形的边数 x:正多边形中心的横坐标 y:正多边形中心的纵坐标 r:正多边形中心到顶点的距离 initAngle:初始角度 ----------------------------------------------------------*/ function drawPolygon(ctx,n,x,y,r,initAngle){ var polyArr=[]; for(var i=0;i<n;i++){ var theta=Math.PI*2/n*i+initAngle; var pt={}; pt.x=r*Math.cos(theta)+x; pt.y=r*Math.sin(theta)+y; polyArr.push(pt); } ctx.beginPath(); for(let i=0;i<polyArr.length;i++){ ctx.lineTo(polyArr[i].x,polyArr[i].y); } ctx.closePath(); } /*---------------------------------------------------------- 基础函数:用于绘制圆角矩形 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(); } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 baseX:基准点横坐标 baseY:基准点纵坐标 radius:当前点到基准点的距离 theta:当前点到基准点的角度 Pt即Point ----------------------------------------------------------*/ function createPt2(baseX,baseY,radius,theta){ var retval={}; retval.x=baseX+radius*Math.cos(theta); retval.y=baseY+radius*Math.sin(theta); return retval; } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 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(); } /*------------------------------------------------------------- 官G办媒体的核心战略意图是:使民不知国。 --------------------------------------------------------------*/ //--> </script>
END