js計(jì)時(shí)事件實(shí)現(xiàn)圓形時(shí)鐘
本文實(shí)例為大家分享了js圓形時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>時(shí)鐘</title> <style> *{margin: 0; padding: 0;} div.dd{ margin: 0 auto; width: 400px; padding-top: 100px; } </style> </head> <body> <div class="dd"> <canvas id="clock" height="400px" width="400px">你的瀏覽器不支持canvas</canvas> </div> <script type="text/javascript"> (function(doc){ var can = doc.getElementById("clock"); var con = can.getContext("2d");//創(chuàng)建一個(gè)2d上下文 con.translate(200, 200);//重新確定坐標(biāo)原點(diǎn)(確定圓心位置) //外圓 con.beginPath(); con.fillStyle = '#fff'; con.arc(0, 0, 200, 0, Math.PI*2, false); con.fill(); //內(nèi)圓 con.beginPath(); con.fillStyle = '#aaa'; con.arc(0, 0, 195, 0, Math.PI*2, false); con.fill(); //繪制刻度 con.beginPath(); con.font = "bold 20px sans-serif"; con.textAlign = "center"; con.textBaseline = "middle" con.fillStyle = '#000000'; con.fillText("12", 0, -170); con.fillText("3",170,0); con.fillText("6",0,170); con.fillText("9",-170,0); con.fillText("1",84,-147.224); con.fillText("2",147.224,-84); con.fillText("4",147.224,84); con.fillText("5",84,147.224); con.fillText("7",-84,147.224); con.fillText("8",-147.224,84); con.fillText("10",-147.224,-84); con.fillText("11",-84,-147.224); //獲取當(dāng)前時(shí)間 var d = new Date(), time ={}; time.H = d.getHours()%12; time.M = d.getMinutes(); time.S = d.getSeconds(); function getTime(){ time.S++; if(time.S > 59){ time.S = 0; time.M++; if(time.M > 60){ time.M = 0; time.H++; if(time.H > 11){ time.H = 0; } } } canvasTimeLine(); } //將角度轉(zhuǎn)為弧度 function numToDeg(num){ return ((num*6 - 90)*0.0174444444444444); } //確定刻度位置 function computePositionByLenDeg(len, deg){ return { x: len*Math.cos(deg), y: len*Math.sin(deg) } } //繪制刻度 function canvasLineMintus(){ for(var i = 0;i<60; i++){ var rec = 180; con.beginPath(); con.lineWidth = 4; if(i%5 != 0){ con.lineWidth = 1; rec = 184 } var beinDeg = numToDeg(i), beginPot = computePositionByLenDeg(rec, beinDeg), endPot = computePositionByLenDeg(190, beinDeg); con.moveTo(beginPot.x, beginPot.y); con.lineTo(endPot.x, endPot.y); con.stroke(); } } function canvasTimeLine(){ var sDeg = numToDeg(time.S), mDeg = numToDeg(time.M), hDeg = numToDeg(time.H*5 + Math.floor(time.M/12)), sPot = computePositionByLenDeg(150, sDeg), mPot = computePositionByLenDeg(135, mDeg), hPot = computePositionByLenDeg(110, hDeg); //時(shí)鐘表面 con.beginPath(); con.fillStyle = '#ddd'; con.arc(0, 0, 156, 0, Math.PI*2, false); con.fill(); //時(shí)針 con.beginPath(); con.moveTo(0, 0); con.lineTo(hPot.x, hPot.y); con.lineWidth = 6; con.strokeStyle = "#333"; con.stroke(); //分針 con.beginPath(); con.moveTo(0, 0); con.lineTo(mPot.x, mPot.y); con.lineWidth = 4; con.strokeStyle = "#333"; con.stroke(); //秒針 con.beginPath(); con.moveTo(0, 0); con.lineTo(sPot.x, sPot.y); con.lineWidth = 2; con.strokeStyle = "#ff0000"; con.stroke(); //針心 con.beginPath(); con.fillStyle = '#aaaaaa'; con.arc(0, 0, 8, 0, Math.PI*2, false); con.fill(); con.beginPath(); con.fillStyle = '#f00'; con.arc(0, 0, 4, 0, Math.PI*2, false); con.fill(); } canvasLineMintus(); getTime(); setInterval(getTime, 1000); })(document); </script> </body> </html>
更多JavaScript時(shí)鐘特效點(diǎn)擊查看:JavaScript時(shí)鐘特效專題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中html字符串轉(zhuǎn)化為jquery dom對(duì)象的方法
最近項(xiàng)目需求要開(kāi)發(fā)百度地圖相關(guān)的一個(gè)應(yīng)用,需要從硬編碼的html字符串中提取自己想要的元素以及屬性信息,由于在js中或者jq中操作元素節(jié)點(diǎn)以及屬性都是使用dom對(duì)象或者jq對(duì)象。下面介紹javascript中html字符串轉(zhuǎn)化為jquery dom對(duì)象的方法,需要的朋友可以參考下2015-08-08JS實(shí)現(xiàn)壓縮上傳圖片base64長(zhǎng)度功能
這篇文章主要介紹了js實(shí)現(xiàn)壓縮上傳圖片base64長(zhǎng)度功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12功能強(qiáng)大的Bootstrap組件(結(jié)合js)
這篇文章主要介紹了功能強(qiáng)大的Bootstrap組件,介紹js結(jié)合Bootstrap組件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08JavaScript中箭頭函數(shù)與普通函數(shù)的區(qū)別詳解
這篇文章主要介紹了JavaScript中箭頭函數(shù)與普通函數(shù)的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08微信小程序五星評(píng)分效果實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序五星評(píng)分效果實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04bootstrap如何讓dropdown menu按鈕式下拉框長(zhǎng)度一致
bootstrap框架提供了下拉菜單組件(dropdown),即點(diǎn)擊一個(gè)元素或按鈕,觸發(fā)隱藏的列表顯示出來(lái)。下面通過(guò)本文給大家介紹bootstrap如何讓dropdown menu按鈕式下拉框長(zhǎng)度一致,需要的朋友可以參考下2017-04-04