JS+Canvas繪制時(shí)鐘效果
本文實(shí)例為大家分享了使用canvas繪制時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
1. clock.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Description" content="">
<title>canvas時(shí)鐘</title>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script>
var canvas=document.getElementById("canvas");
var context=canvas.getContext('2d');
//draw();
function draw(){
//得到當(dāng)前系統(tǒng)的時(shí)分秒
var now=new Date();
var sec=now.getSeconds();
var min=now.getMinutes();
var hour=now.getHours();
hour>=12&&(hour=hour-12);
var radius = Math.min(canvas.width/2,canvas.height/2);
//初始化畫布
context.save();
context.clearRect(0,0,canvas.width,canvas.height);
context.translate(canvas.width/2,canvas.height/2);
context.rotate(-Math.PI/2);
context.save();
//表框
//小時(shí)刻度
context.strokeStyle="black";
context.fillStyle="black";
context.lineWidth=3;
context.lineCap="round";
context.beginPath();
for(var i=0;i<12;i++){
context.rotate(Math.PI/6);
context.moveTo(radius-30,0);
context.lineTo(radius-10,0);
}
context.stroke();
context.restore();
context.save();
//分鐘刻度
context.lineWidth=2;
context.beginPath();
for(var i=0;i<60;i++){
if(!i%5==0){
context.moveTo(radius-15,0);
context.lineTo(radius-10,0);
}
context.rotate(Math.PI/30);
}
context.stroke();
context.restore();
context.save();
//畫上時(shí)針
context.rotate((Math.PI/6)*hour+(Math.PI/360)*min+(Math.PI/21600)*sec);
context.lineWidth=6;
context.beginPath();
context.moveTo(-10,0);
context.lineTo(radius*0.5,0);
context.stroke();
context.restore();
context.save();
context.rotate((Math.PI/30)*min+(Math.PI/1800)*sec);
context.strokeStyle="#29A8DE";
context.lineWidth=4;
context.lineCap="butt";
context.beginPath();
context.moveTo(-20,0);
context.lineTo(radius*0.7,0);
context.stroke();
context.restore();
context.save();
context.rotate(sec*Math.PI/30);
context.strokeStyle="red";
context.lineWidth=2;
context.lineCap="butt";
context.beginPath();
context.moveTo(-30,0);
context.lineTo(radius*0.9,0);
context.stroke();
context.restore();
context.save();
context.lineWidth=4;
context.strokeStyle="gray";
context.beginPath();
context.arc(0,0,radius,0,Math.PI*2,true);
context.stroke();
context.restore();
context.restore();
}
window.onload=function(){
setInterval(draw,1000)
}
</script>
</body>
</html>
2. JavaScript代碼
<script>
var canvas=document.getElementById("canvas");
var context=canvas.getContext('2d');
//draw();
function draw(){
//得到當(dāng)前系統(tǒng)的時(shí)分秒
var now=new Date();
var sec=now.getSeconds();
var min=now.getMinutes();
var hour=now.getHours();
hour>=12&&(hour=hour-12);
var radius = Math.min(canvas.width/2,canvas.height/2);
//初始化畫布
context.save();
context.clearRect(0,0,canvas.width,canvas.height);
context.translate(canvas.width/2,canvas.height/2);
context.rotate(-Math.PI/2);
context.save();
//表框
//小時(shí)刻度
context.strokeStyle="black";
context.fillStyle="black";
context.lineWidth=3;
context.lineCap="round";
context.beginPath();
for(var i=0;i<12;i++){
context.rotate(Math.PI/6);
context.moveTo(radius-30,0);
context.lineTo(radius-10,0);
}
context.stroke();
context.restore();
context.save();
//分鐘刻度
context.lineWidth=2;
context.beginPath();
for(var i=0;i<60;i++){
if(!i%5==0){
context.moveTo(radius-15,0);
context.lineTo(radius-10,0);
}
context.rotate(Math.PI/30);
}
context.stroke();
context.restore();
context.save();
//畫上時(shí)針
context.rotate((Math.PI/6)*hour+(Math.PI/360)*min+(Math.PI/21600)*sec);
context.lineWidth=6;
context.beginPath();
context.moveTo(-10,0);
context.lineTo(radius*0.5,0);
context.stroke();
context.restore();
context.save();
context.rotate((Math.PI/30)*min+(Math.PI/1800)*sec);
context.strokeStyle="#29A8DE";
context.lineWidth=4;
context.lineCap="butt";
context.beginPath();
context.moveTo(-20,0);
context.lineTo(radius*0.7,0);
context.stroke();
context.restore();
context.save();
context.rotate(sec*Math.PI/30);
context.strokeStyle="red";
context.lineWidth=2;
context.lineCap="butt";
context.beginPath();
context.moveTo(-30,0);
context.lineTo(radius*0.9,0);
context.stroke();
context.restore();
context.save();
context.lineWidth=4;
context.strokeStyle="gray";
context.beginPath();
context.arc(0,0,radius,0,Math.PI*2,true);
context.stroke();
context.restore();
context.restore();
}
window.onload=function(){
setInterval(draw,1000)
}
</script>
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- js Canvas實(shí)現(xiàn)圓形時(shí)鐘教程
- JS+Canvas繪制動(dòng)態(tài)時(shí)鐘效果
- javascript結(jié)合Canvas 實(shí)現(xiàn)簡易的圓形時(shí)鐘
- js+html5實(shí)現(xiàn)canvas繪制網(wǎng)頁時(shí)鐘的方法
- JavaScript html5 canvas繪制時(shí)鐘效果
- JavaScript學(xué)習(xí)小結(jié)之使用canvas畫“哆啦A夢(mèng)”時(shí)鐘
- JS+H5 Canvas實(shí)現(xiàn)時(shí)鐘效果
- js Canvas繪制圓形時(shí)鐘效果
- JS+Canvas實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
相關(guān)文章
js控件Kindeditor實(shí)現(xiàn)圖片自動(dòng)上傳功能
這篇文章主要為大家詳細(xì)介紹了js控件Kindeditor實(shí)現(xiàn)圖片自動(dòng)上傳功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
js數(shù)值計(jì)算時(shí)使用parseInt進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換(jquery)
這篇文章主要介紹了js數(shù)值計(jì)算時(shí)使用parseInt進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換(jquery),需要的朋友可以參考下2014-10-10
textarea焦點(diǎn)的用法實(shí)現(xiàn)獲取焦點(diǎn)清空失去焦點(diǎn)提示效果
這篇文章主要介紹了textarea焦點(diǎn)的用法實(shí)現(xiàn)獲取焦點(diǎn)清空失去焦點(diǎn)提示效果,需要的朋友可以參考下2014-05-05
簡單實(shí)現(xiàn)js選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了簡單實(shí)現(xiàn)js選項(xiàng)卡切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

