html5寫的一個時鐘

來源:互聯網
上載者:User

標籤:

看到的一個html5寫的時鐘

<!doctype><html><head>  <script>   window.onload=function(){    var canvas=document.getElementById("myCanvas");    var c=canvas.getContext("2d");//getContenText  返回一個繪圖的環境,其中2d是目前唯一合法的字元  指的是         var drawClock=function(){          c.clearRect(0,0,500,500);//清除畫布,          var now =new Date();     var sec=now.getSeconds();     var min=now.getMinutes();     var hour=now.getHours();     hour=hour+min/60;     hour=hour>12?hour-12:hour;//21:36:21 把24小時轉12小時制               //錶盤     c.lineWidth=10;     c.strokeStyle="blue";     c.beginPath();     c.arc(250,250,200,0,360,false);     c.closePath();     c.stroke();          //1.刻度          //1.1 時刻度     c.lineWidth=7;     c.strokeStyle="#000";     for(var i=0;i<12;i++){           c.save();      c.translate(250,250);      c.rotate(i*30*Math.PI/180);      c.beginPath();      c.moveTo(0,-170);      c.lineTo(0,-190);      c.closePath();      c.stroke();      c.restore();     }          //1.2 分刻度     c.lineWidth=5;     c.strokeStyle="#000";     for(var i=0;i<60;i++){           c.save();      c.translate(250,250);      c.rotate(i*6*Math.PI/180);      c.beginPath();      c.moveTo(0,-170);      c.lineTo(0,-180);      c.closePath();      c.stroke();      c.restore();     }          //2.1 時針         c.save();     c.lineWidth=7;     c.strokeStyle="#000";     c.translate(250,250);     c.rotate(hour*30*Math.PI/180);     c.beginPath();     c.moveTo(0,-100);     c.lineTo(0,10);     c.closePath();     c.stroke();     c.restore();          //2.2 分針         c.save();     c.lineWidth=5;     c.strokeStyle="#333";     c.translate(250,250);     c.rotate( min*6*Math.PI/180);     c.beginPath();     c.moveTo(0,-140);     c.lineTo(0,15);     c.closePath();     c.stroke();     c.restore();          //2.3 秒針         c.save();     c.lineWidth=2;     c.strokeStyle="red";     c.translate(250,250);     c.rotate(sec * 6 *Math.PI/180);     c.beginPath();     c.moveTo(0,-160);     c.lineTo(0,20);     c.closePath();     c.stroke();          //圓心加個圓圈     c.beginPath();     c.arc(0,0,5,0,360,false);     c.closePath();     c.fillStyle="green";     c.fill();     //在秒針前端加個圓點     c.beginPath();     c.arc(0,-140,10,0,360,false);          c.closePath();          c.fillStyle="red";     c.fill();     c.restore();          //3 盤外時刻數字         for(var i=1;i<13;i++){           c.save();      c.lineWidth=5;      c.strokeStyle="blue";      c.font="40px 黑體";      c.translate(250,250);      c.rotate(i*30*Math.PI/180);      c.beginPath();      c.strokeText(""+i,-20,-210);      c.closePath();      c.stroke();      c.restore();     }         }    drawClock();    setInterval(drawClock,1000);   }  </script></head><body>  <canvas width="500" height="500" id="myCanvas" style="background-color:yellow">     垃圾瀏覽器不支援canvas標籤      </canvas> </body></html>

html5寫的一個時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.