使用js和canvas實(shí)現(xiàn)時鐘效果
使用js和canvas寫一個時鐘,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>`<html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title></head><body> <canvas id=’canvas’ width=’600’ height=’600’ style='border: 1px solid red;'></canvas> <script> /** @type {HTMLCanvasElement} */ let canvas = document.querySelector('#canvas'); let ctx = canvas.getContext('2d'); let deg = Math.PI / 180; let HourR = 100; let MinutesR = 135; let SecondsR = 170; setInterval(function () { canvas.width = canvas.width; ctx.arc(300, 300, 200, 0, Math.PI * 2) ctx.fillStyle = ’rgba(10,100,30,0.2)’ ctx.strokeStyle = ’red’ //獲取當(dāng)前時間 let dt = new Date() let Hour = dt.getHours() let Minutes = dt.getMinutes() let Seconds = dt.getSeconds() //時鐘 ctx.moveTo(300, 300); let xx = HourR * (Math.sin(Hour * 30 * deg)) let yy = HourR * (Math.cos(Hour * 30 * deg)) ctx.lineTo((300 + xx), (300 - yy)) //分鐘和秒鐘 function move(time, R) {ctx.moveTo(300, 300);xx = R * (Math.sin(time * 6 * deg))yy = R * (Math.cos(time * 6 * deg))ctx.lineTo((300 + xx), (300 - yy)) } //小時指針 for (let m = 0; m < 12; m++) {let xx = 190 * (Math.sin(m * 30 * deg))let yy = 190 * (Math.cos(m * 30 * deg))let xx1 = 200 * (Math.sin(m * 30 * deg))let yy1 = 200 * (Math.cos(m * 30 * deg))ctx.moveTo((300 + xx), (300 - yy));ctx.lineTo((300 + xx1), (300 - yy1)) } move(Seconds, SecondsR) move(Minutes, MinutesR) ctx.fill() ctx.stroke() }, 1000) </script></body></html>
更多JavaScript時鐘特效點(diǎn)擊查看:JavaScript時鐘特效專題
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
