溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在html5中實(shí)現(xiàn)一個(gè)時(shí)鐘功能

發(fā)布時(shí)間:2021-04-16 17:14:20 來(lái)源:億速云 閱讀:158 作者:Leah 欄目:web開(kāi)發(fā)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在html5中實(shí)現(xiàn)一個(gè)時(shí)鐘功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

<!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"> //獲取繪圖對(duì)象 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; //畫(huà)大圓  context.beginPath(); context.strokeStyle="#009999"; context.arc(width/2,height/2,width/2-1,0,Math.PI*2,true); context.stroke(); context.closePath(); //畫(huà)中間點(diǎn) context.beginPath(); context.fillStyle="#000"; context.arc(width/2,height/2,3,0,Math.PI*2,true); context.fill(); context.closePath();  //畫(huà)小刻度 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);  //這里是用來(lái)將刻度的另一點(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>

上述就是小編為大家分享的怎么在html5中實(shí)現(xiàn)一個(gè)時(shí)鐘功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI