【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>377.铜圈紫底表盘</title> <style type="text/css"> .centerlize{ margin:0 auto; width:1200px; } </style> </head> <body οnlοad="init();"> <div class="centerlize"> <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;"> 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试. </canvas> <img id="myImg" src="377.jpg" style="display:none;"/> <img id="myImg2" src="377-2.jpg" style="display:none;"/> </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.fillStyle = "white"; ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); // 外圈 ctx.save(); ctx.lineWidth=2; ctx.beginPath(); ctx.arc(0,0,240,0,Math.PI*2,false); ctx.closePath(); ctx.strokeStyle="rgb(111,92,62)"; ctx.stroke(); ctx.restore(); // 铜截图圈 ctx.save(); ctx.rotate(Math.PI/4*3); ctx.beginPath(); var r=240; ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.clip(); var img=document.getElementById("myImg"); ctx.drawImage(img,0,83,500,500,-r,-r,2*r,2*r); ctx.restore(); // 内圈 ctx.save(); ctx.lineWidth=1; ctx.beginPath(); ctx.arc(0,0,220,0,Math.PI*2,false); ctx.closePath(); ctx.strokeStyle="rgb(239,194,132)"; ctx.stroke(); ctx.fillStyle="white"; ctx.fill(); ctx.restore(); // 底图截图圈 ctx.save(); ctx.beginPath(); var r=220; ctx.arc(0,0,r,0,Math.PI*2,false); ctx.closePath(); ctx.clip(); var img=document.getElementById("myImg2"); ctx.drawImage(img,0,0,800,800,-r,-r,2*r,2*r); ctx.restore(); // 刻度 var r=200; for(var i=0;i<60;i++){ var theta=i*Math.PI/30; var pt=createPt(r*Math.cos(theta),r*Math.sin(theta)); drawSolidCircle(ctx,pt.x,pt.y,(i%5==0)?3:1,"gold"); } // 小时数字 r=168; var hours=["Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ","Ⅺ","Ⅻ","Ⅰ","Ⅱ"]; for(var i=0;i<12;i++){ var theta=i*Math.PI/6; var pt=createPt(r*Math.cos(theta),r*Math.sin(theta)); ctx.save(); ctx.translate(pt.x,pt.y); ctx.rotate(theta+Math.PI/2); ctx.font="16px Microsoft YaHei UI"; ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.fillStyle="rgb(231,219,111)"; ctx.fillText(hours[i],0,0);// 12是经验值 ctx.restore(); } // 得到当前时间 var now=new Date(); var s=now.getSeconds(); var m=now.getMinutes(); var h=now.getHours()+m/60; // 画时针 ctx.save(); ctx.rotate(h*Math.PI/6-Math.PI/2); ctx.beginPath(); ctx.moveTo(140,0); ctx.lineTo(130,1); ctx.lineTo(0,4); ctx.lineTo(-20,0); ctx.lineTo(0,-4); ctx.lineTo(130,-1); ctx.closePath(); ctx.fillStyle="rgb(231,219,111)"; ctx.fill(); ctx.restore(); // 画分针 ctx.save(); ctx.rotate(m*Math.PI/30-Math.PI/2); ctx.beginPath(); ctx.moveTo(170,0); ctx.lineTo(160,1); ctx.lineTo(0,3); ctx.lineTo(-20,0); ctx.lineTo(0,-3); ctx.lineTo(160,-1); ctx.closePath(); ctx.fillStyle="rgb(231,219,111)"; ctx.fill(); ctx.restore(); // 画秒针 ctx.save(); ctx.rotate(s*Math.PI/30-Math.PI/2); ctx.beginPath(); ctx.moveTo(200,0); ctx.lineTo(-20,2); ctx.lineTo(-22,3); ctx.lineTo(-42,3); ctx.lineTo(-42,-3); ctx.lineTo(-22,-3); ctx.lineTo(-20,-2); ctx.lineTo(200,0); ctx.closePath(); ctx.fillStyle="rgb(231,219,111)"; ctx.fill(); ctx.restore(); // 画中心小圆点 ctx.beginPath(); ctx.arc(0,0,2,0,Math.PI*2,true); ctx.closePath(); ctx.fillStyle="navy"; ctx.fill(); 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>
【底图】
377.jpg
377-2.jpg