html5繪制時鐘動畫
發(fā)布時間:2014-12-15 15:59:34 作者:佚名
我要評論

這篇文章主要介紹了html5繪制時鐘動畫,需要的朋友可以參考下
<canvas id="clock" width="500" height="500" style="background-color: yellow"></canvas>
復(fù)制代碼
代碼如下: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();
//刻度
//時刻度
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();
}
//時針
cxt.save();
// 設(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();
//畫出時針,分針,秒針的交叉點(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);
相關(guān)文章
Html5 canvas實(shí)現(xiàn)粒子時鐘的示例代碼
這篇文章主要介紹了Html5 canvas實(shí)現(xiàn)粒子時鐘的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-06- 本文通過實(shí)例代碼給大家介紹了HTML寫一個網(wǎng)頁動態(tài)時鐘效果,需要的的朋友參考下吧2017-08-30
- 本文通過實(shí)例代碼給大家介紹了基于html5實(shí)現(xiàn)的可縮放時鐘代碼,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-08-28
用HTML5的canvas實(shí)現(xiàn)一個炫酷時鐘效果
下面小編就為大家?guī)硪黄肏TML5的canvas實(shí)現(xiàn)一個炫酷時鐘效果。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-20- 這篇文章主要介紹了使用html5 canvas 畫時鐘代碼實(shí)例分享的相關(guān)資料,需要的朋友可以參考下2015-11-11
- 這篇文章主要介紹了用HTML5制作數(shù)字時鐘的教程,主要利用HTML5中的Canvas API,需要的朋友可以參考下2015-05-11
- html5下實(shí)現(xiàn)的鬧鐘代碼,喜歡的朋友可以參考下。2010-10-22
- 看著有點(diǎn)黑科技的感覺就跟前段時間比較火的抖音動態(tài)時間那個一個效果,本文主要介紹了HTML 羅盤式時鐘的實(shí)現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們2021-05-20