html5繪制時(shí)鐘動(dòng)畫
來(lái)源:易賢網(wǎng) 閱讀:1040 次 日期:2016-07-09 11:46:03
溫馨提示:易賢網(wǎng)小編為您整理了“html5繪制時(shí)鐘動(dòng)畫”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了html5繪制時(shí)鐘動(dòng)畫,需要的朋友可以參考下

代碼如下:

var clock=document.getElementById("clock");

var cxt=clock.getContext("2d");

function drawNow(){

var now=new Date();

var hour=now.getHours();

var min=now.getMinutes();

var sec=now.getSeconds();

hour=hour>12?hour-12:hour;

hour=hour+min/60;

//表盤(藍(lán)色)

cxt.lineWidth=10;

cxt.strokeStyle="blue"

cxt.beginPath();

cxt.arc(250,250,200,0,360,false);

cxt.closePath();

cxt.stroke();

//刻度

//時(shí)刻度

for(var i=0;i<12;i++){

cxt.save();

cxt.lineWidth=7;

cxt.strokeStyle="black";

cxt.translate(250,250);

cxt.rotate(i*30*Math.PI/180);//旋轉(zhuǎn)角度 角度*Math.PI/180=弧度

cxt.beginPath();

cxt.moveTo(0,-170);

cxt.lineTo(0,-190);

cxt.closePath();

cxt.stroke();

cxt.restore();

}

//分刻度

for(var i=0;i<60;i++){

cxt.save();

//設(shè)置分刻度的粗細(xì)

cxt.lineWidth=5;

//重置畫布原點(diǎn)

cxt.translate(250,250);

//設(shè)置旋轉(zhuǎn)角度

cxt.rotate(i*6*Math.PI/180);

//畫分針刻度

cxt.strokeStyle="black";

cxt.beginPath();

cxt.moveTo(0,-180);

cxt.lineTo(0,-190);

cxt.closePath();

cxt.stroke();

cxt.restore();

}

//時(shí)針

cxt.save();

// 設(shè)置時(shí)針風(fēng)格

cxt.lineWidth=7;

cxt.strokeStyle="black";

cxt.translate(250,250);

cxt.rotate(hour*30*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,-140);

cxt.lineTo(0,10);

cxt.closePath();

cxt.stroke();

cxt.restore();

//分針

cxt.save();

cxt.lineWidth=5;

cxt.strokeStyle="black";

//設(shè)置異次元空間分針畫布的中心

cxt.translate(250,250);

cxt.rotate(min*6*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,-160);

cxt.lineTo(0,15);

cxt.closePath();

cxt.stroke()

cxt.restore();

//秒針

cxt.save();

//設(shè)置秒針的風(fēng)格

//顏色:紅色

cxt.strokeStyle="red";

cxt.lineWidth=3;

//重置原點(diǎn)

cxt.translate(250,250);

//設(shè)置角度

//cxt.rotate(330*Math.PI/180);

cxt.rotate(sec*6*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,-170);

cxt.lineTo(0,20);

cxt.closePath();

cxt.stroke();

//畫出時(shí)針,分針,秒針的交叉點(diǎn)

cxt.beginPath();

cxt.arc(0,0,5,0,360,false);

cxt.closePath();

//設(shè)置填充

cxt.fillStyle="gray";

cxt.fill();

//cxt.strokeStyle="red";

cxt.stroke();

//畫出秒針的小圓點(diǎn)

cxt.beginPath();

cxt.arc(0,-140,5,0,360,false);

cxt.closePath();

//設(shè)置填充

cxt.fillStyle="gray";

cxt.fill();

//cxt.strokeStyle="red";

cxt.stroke();</p> <p> cxt.restore();</p> <p>}

function drawClock(){

cxt.clearRect(0,0,500,500);

drawNow();

}

drawNow();

setInterval(drawClock,1000);

更多信息請(qǐng)查看網(wǎng)頁(yè)制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:html5繪制時(shí)鐘動(dòng)畫
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
相關(guān)閱讀網(wǎng)頁(yè)制作
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)