国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS html時鐘制作代碼分享

2019-11-19 17:19:44
字體:
來源:轉載
供稿:網友

時鐘效果圖:


閑來無聊做了一個網頁的時鐘,效果模擬傳統時鐘的運行方式,

運用了html 的畫布實現指針,背景圖片引用了網絡圖片。

具體原理:

首先將時鐘分為四個不同區域,對每個區域計算cos,sin 來確實指針頂點位置。在通過畫布來繪畫出指針。
通過setInterval 每秒刷新指針位置實現 傳統機械表針的動態跳動。

本人是JS開發程序員,從業1年多。閑來無聊的簡單頁面,

歡迎大家提問,或者建議。共同進步

代碼部分,直接復制HTML 文件中即可查看效果:

<!DOCTYPE html><html><head>  <meta charset=utf-8>  <title>clock</title></head><body style="color:green; <!--background-image:url('http://image.lxway.com/upload/b/f0/bf0d97dcee487096548e6edbc89d4963_thumb.png');--> background-size:100%; background-repeat: no-repeat; background-attachment:fixed">  <div style="width: 900px; height: 900px; margin-top: 50px; margin-left: 50px;">    <div style="position: absolute; left:119px;top: 193px; width: 900px; height: 900px; background-image:url('http://image.lxway.com/upload/b/f0/bf0d97dcee487096548e6edbc89d4963_thumb.png'); background-repeat: no-repeat; z-index: -1;">    </div>    <canvas id="t" width="800" height="800"></canvas>    <div style="position: absolute; left:118px;top: 464px; width: 100px; height: 200px; background-color: white; background-repeat: no-repeat; z-index: 10;">    </div>  </div>  <script language="javascript">        var s = setInterval(moveI, 1000);        function moveI() {          var c = document.getElementById("t");          var pc = c.getContext("2d");          c.height = c.height;          //秒          pc.lineWidth = 3;          pc.strokeStyle = 'rgba(255,0,0,0.8)';          var now = new Date();          var sindex = getxy(150, now.getSeconds());          pc.moveTo(400, 400);          pc.lineTo(sindex.x, sindex.y);          pc.stroke();          pc.beginPath();          //分          pc.lineWidth = 7;          pc.strokeStyle = 'rgba(50,50,50,0.8)';          var mindex = getxy(120, now.getMinutes() + (now.getSeconds() / 60));          pc.moveTo(400, 400);          pc.lineTo(mindex.x, mindex.y);          pc.stroke();          pc.beginPath();          //時          pc.lineWidth = 10;          pc.strokeStyle = 'rgba(0,0,0,0.8)';          var hindex = getxy(80, ((now.getHours() > 12 ? now.getHours() - 12 : now.getHours()) + (now.getMinutes() / 60)) * 5);          pc.moveTo(400, 400);          pc.lineTo(hindex.x, hindex.y);          pc.stroke();        };        function getxy(r, t) {          //計算分區 0,1,2,3          var a = parseInt(t / 15);          //分區角度          t = t - 15 * a;          var y;          var x;          //基于分區的坐標計算          switch (a) {            case 0:              y = r - (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15)));              x = r + (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15)));              break;            case 1:              y = r + (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15)));              x = r + (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15)));              break;            case 2:              y = r + (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15)));              x = r - (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15)));              break;            case 3:              y = r - (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15)));              x = r - (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15)));              break;            default:              break;          }          y = (400 - r) + y;          x = (400 - r) + x;          return {            'x': x,            'y': y          };        };  </script></body></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孟津县| 凉城县| 锡林浩特市| 和硕县| 四会市| 西乌珠穆沁旗| 泽普县| 新安县| 乌恰县| 青岛市| 固安县| 大冶市| 吉木萨尔县| 巴塘县| 曲周县| 文山县| 林周县| 永福县| 浪卡子县| 巴林右旗| 曲阜市| 鹰潭市| 绥中县| 紫云| 麻栗坡县| 贺州市| 潍坊市| 志丹县| 正安县| 苏尼特左旗| 泸定县| 蛟河市| 台南县| 谷城县| 南昌县| 巴塘县| 仪陇县| 绵竹市| 台中县| 易门县| 连城县|