html5時(shí)鐘實(shí)現(xiàn)代碼
發(fā)布時(shí)間:2010-10-22 15:40:15 作者:佚名
我要評論

html5下實(shí)現(xiàn)的鬧鐘代碼,喜歡的朋友可以參考下。
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
canvas{position:absolute;top:0px;left:0px;}
</style>
<title>時(shí)鐘</title>
</head>
<body>
<canvas id="canvas" width="200" height="200"></canvas>
<canvas id="p_canvas" width="200" height="200"></canvas>
<script type="text/javascript">
//獲取繪圖對象
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var p_canvas = document.getElementById('p_canvas');
var p_context = p_canvas.getContext('2d');
var height=200,width=200;
//畫大圓
context.beginPath();
context.strokeStyle="#009999";
context.arc(width/2,height/2,width/2-1,0,Math.PI*2,true);
context.stroke();
context.closePath();
//畫中間點(diǎn)
context.beginPath();
context.fillStyle="#000";
context.arc(width/2,height/2,3,0,Math.PI*2,true);
context.fill();
context.closePath();
//畫小刻度
var angle = 0,radius = width/2 - 4;
for(var i=0;i<60;i++){
context.beginPath();
context.strokeStyle="#000";
//確認(rèn)刻度的起始點(diǎn)
var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle);
context.moveTo(x,y);
//這里是用來將刻度的另一點(diǎn)指向中心點(diǎn),并給予正確的角度
//PI是180度,正確的角度就是 angle+180度,正好相反方向
var temp_angle = Math.PI +angle;
context.lineTo(x +3*Math.cos(temp_angle),y+3*Math.sin(temp_angle));
context.stroke();
context.closePath();
angle+=6/180*Math.PI;
}
//大刻度
angle = 0,radius = width/2 - 4;
context.textBaseline = 'middle';
context.textAlign = 'center';
context.lineWidth = 2;
for(var i=0;i<12;i++){
var num = i+3>12? i+3-12:i+3 ;
context.beginPath();
context.strokeStyle="#FFD700";
var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle);
context.moveTo(x,y);
var temp_angle = Math.PI +angle;
context.lineTo(x +8*Math.cos(temp_angle),y+8*Math.sin(temp_angle));
context.stroke();
context.closePath();
//大刻度 文字
context.fillText(num,x+16*Math.cos(temp_angle),y+16*Math.sin(temp_angle));
angle+=30/180*Math.PI;
}
function Pointer(){
var p_type = [['#000',70,1],['#ccc',60,2],['red',50,3]];
function drwePointer(type,angle){
type = p_type[type];
angle = angle*Math.PI*2 - 90/180*Math.PI;
var length= type[1];
p_context.beginPath();
p_context.lineWidth = type[2];
p_context.strokeStyle = type[0];
p_context.moveTo(width/2,height/2);
p_context.lineTo(width/2 + length*Math.cos(angle),height/2 + length*Math.sin(angle));
p_context.stroke();
p_context.closePath();
}
setInterval(function (){
p_context.clearRect(0,0,height,width);
var time = new Date();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
h = h>12?h-12:h;
h = h+m/60;
h=h/12;
m=m/60;
s=s/60;
drwePointer(0,s);
drwePointer(1,m);
drwePointer(2,h);
},500);
}
var p = new Pointer();
</script>
</body>
</html>
相關(guān)文章
Html5 canvas實(shí)現(xiàn)粒子時(shí)鐘的示例代碼
這篇文章主要介紹了Html5 canvas實(shí)現(xiàn)粒子時(shí)鐘的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-06HTML寫一個(gè)網(wǎng)頁動(dòng)態(tài)時(shí)鐘
本文通過實(shí)例代碼給大家介紹了HTML寫一個(gè)網(wǎng)頁動(dòng)態(tài)時(shí)鐘效果,需要的的朋友參考下吧2017-08-30HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼
本文通過實(shí)例代碼給大家介紹了基于html5實(shí)現(xiàn)的可縮放時(shí)鐘代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-08-28用HTML5的canvas實(shí)現(xiàn)一個(gè)炫酷時(shí)鐘效果
下面小編就為大家?guī)硪黄肏TML5的canvas實(shí)現(xiàn)一個(gè)炫酷時(shí)鐘效果。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-20使用html5 canvas 畫時(shí)鐘代碼實(shí)例分享
這篇文章主要介紹了使用html5 canvas 畫時(shí)鐘代碼實(shí)例分享的相關(guān)資料,需要的朋友可以參考下2015-11-11- 這篇文章主要介紹了用HTML5制作數(shù)字時(shí)鐘的教程,主要利用HTML5中的Canvas API,需要的朋友可以參考下2015-05-11
- 這篇文章主要介紹了html5繪制時(shí)鐘動(dòng)畫,需要的朋友可以參考下2014-12-15
- 看著有點(diǎn)黑科技的感覺就跟前段時(shí)間比較火的抖音動(dòng)態(tài)時(shí)間那個(gè)一個(gè)效果,本文主要介紹了HTML 羅盤式時(shí)鐘的實(shí)現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們2021-05-20