欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS+canvas繪制的動(dòng)態(tài)機(jī)械表動(dòng)畫效果

 更新時(shí)間:2017年09月12日 11:42:53   作者:ITzhongzi  
這篇文章主要介紹了JS+canvas繪制的動(dòng)態(tài)機(jī)械表動(dòng)畫效果,涉及javascript結(jié)合HTML5 canvas簡(jiǎn)單數(shù)值計(jì)算與動(dòng)態(tài)繪圖相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了JS+canvas繪制的動(dòng)態(tài)機(jī)械表動(dòng)畫效果。分享給大家供大家參考,具體如下:

先來(lái)看看運(yùn)行效果:

完整實(shí)例代碼:

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>www.dbjr.com.cn canvas時(shí)鐘</title>
  <style>
    canvas {
      border: 1px solid red;
    }
  </style>
</head>
<body>
<canvas width="800" height="600"></canvas>
</body>
<script>
  function Clock(opt) {
    for (var key in opt) {
      this[key] = opt[key];
    }
    this.init();
  }
  Clock.prototype = {
    init: function () {
      var self = this;
      var ctx = this.ctx;
      this.timer = setInterval(function(){
        ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
        self.drawDial();
        self.drawDegreeScale();
        self.drawNumber();
        self.drawPointers();
      },1000);
    },
    drawDial: function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.beginPath();
      ctx.lineWidth = this.clockDialW;
      ctx.strokeStyle = this.clockDialColor;
      ctx.arc(this.clockX, this.clockY, this.clockRadius, 0, 2 * Math.PI);
      ctx.stroke();
      ctx.restore();
    },
    drawDegreeScale: function () {
      var ctx = this.ctx;
      var clockRadius = this.clockRadius;
      var clockX = this.clockX;
      var clockY = this.clockY;
      var bigDegreeScaleL = this.bigDegreeScaleL;
      var smallDegreeScale = this.smallDegreeScale;
      var startX, startY, endX, endY, radian;
      ctx.save();
      for (var i = 0; i < 12; i++) {
        radian = i * Math.PI / 6;
        endX = clockX + clockRadius * Math.cos(radian);
        endY = clockY + clockRadius * Math.sin(radian);
        if (radian % (Math.PI / 2) == 0) {
          startX = clockX + (clockRadius - bigDegreeScaleL) * Math.cos(radian);
          startY = clockY + (clockRadius - bigDegreeScaleL) * Math.sin(radian);
          ctx.lineWidth = this.bigDCWidth;
        } else {
          startX = clockX + (clockRadius - smallDegreeScale) * Math.cos(radian);
          startY = clockY + (clockRadius - smallDegreeScale) * Math.sin(radian);
          ctx.lineWidth = this.smallDCWidth;
        }
        ctx.beginPath();
        ctx.moveTo(startX, startY);
        ctx.lineTo(endX, endY);
        ctx.stroke();
        ctx.restore();
      }
    },
    drawNumber: function () {
      var ctx = this.ctx;
      var textX, textY, textRadian;
      var clockX = this.clockX;
      var clockY = this.clockY;
      var clockRadius = this.clockRadius;
      ctx.font = '20px 微軟雅黑';
      ctx.fillStyle = 'red';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.save();
      for (var i = 0; i < 12; i++) {
        textRadian = i * Math.PI / 6 - Math.PI/3;
        textX = clockX + (clockRadius - 40) * Math.cos(textRadian);
        textY = clockY + (clockRadius - 40) * Math.sin(textRadian);
        ctx.beginPath();
        ctx.fillText(i + 1, textX, textY);
      }
      ctx.restore();
    },
    drawPointers: function () {
      var date = new Date();
      var h = date.getHours();
      var m = date.getMinutes();
      var s = date.getSeconds();
      h = h % 12;
      var hRadian = 2 * Math.PI / 12 * h - Math.PI / 2;
      this.drawPoint(hRadian,30,'red',8);
      var mRadian = 2 * Math.PI / 60 * m - Math.PI / 2;
      this.drawPoint(mRadian,50,'blue',6);
      var sRadian = 2 * Math.PI / 60 * s - Math.PI / 2;
      this.drawPoint(sRadian,70,'green',2);
    },
    drawPoint: function (radian, length,color,lineWidth) {
      var x = this.clockX + Math.cos(radian) * length;
      var y = this.clockY + Math.sin(radian) * length;
      var ctx = this.ctx;
      ctx.save();
      ctx.beginPath();
      ctx.strokeStyle = color;
      ctx.lineWidth = lineWidth;
      ctx.moveTo(this.clockX,this.clockY);
      ctx.lineTo(x,y);
      ctx.stroke();
      ctx.restore();
    }
  };
</script>
<script>
  var canvas = document.querySelector('canvas');
  var ctx = canvas.getContext('2d');
  var clock = new Clock({
    ctx: this.ctx,
    clockRadius: 150,
    clockX: 300,
    clockY: 300,
    clockDialW: 6,
    clockDialColor: 'blue',
    bigDegreeScaleL: 20,
    bigDCWidth: 6,
    smallDegreeScale: 10,
    smallDCWidth: 4
  });
</script>
</html>

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript+HTML5特效與技巧匯總》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • JS實(shí)現(xiàn)的簡(jiǎn)易拖放效果示例

    JS實(shí)現(xiàn)的簡(jiǎn)易拖放效果示例

    這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)易拖放效果的方法,涉及JS事件監(jiān)聽(tīng)、擴(kuò)展及頁(yè)面元素動(dòng)態(tài)操作的相關(guān)技巧,需要的朋友可以參考下
    2016-12-12
  • Markdown與Bootstrap相結(jié)合實(shí)現(xiàn)圖片自適應(yīng)屬性

    Markdown與Bootstrap相結(jié)合實(shí)現(xiàn)圖片自適應(yīng)屬性

    Markdown 是一種輕量級(jí)的標(biāo)記語(yǔ)言,它的優(yōu)點(diǎn)很多,目前也被越來(lái)越多的寫作愛(ài)好者,撰稿者廣泛使用。接下來(lái)通過(guò)本文給大家介紹Markdown與Bootstrap相結(jié)合實(shí)現(xiàn)圖片自適應(yīng)屬性,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • ES6如何用一句代碼實(shí)現(xiàn)函數(shù)的柯里化

    ES6如何用一句代碼實(shí)現(xiàn)函數(shù)的柯里化

    這篇文章主要介紹了ES6如何用一句代碼實(shí)現(xiàn)函數(shù)的柯里化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • JavaScript深入刨析this的指向以及如何修改指向

    JavaScript深入刨析this的指向以及如何修改指向

    JavaScript中this也是一件很神奇 事情,在面向?qū)ο螅ū热鏹ava)中表示一個(gè)當(dāng)前的對(duì)象引用,但是在JavaScript中this不是固定不變的,而是隨著運(yùn)行環(huán)境的改變而改變
    2021-11-11
  • JavaScript實(shí)現(xiàn)的內(nèi)存數(shù)據(jù)庫(kù)LokiJS介紹和入門實(shí)例

    JavaScript實(shí)現(xiàn)的內(nèi)存數(shù)據(jù)庫(kù)LokiJS介紹和入門實(shí)例

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)的內(nèi)存數(shù)據(jù)庫(kù)LokiJS介紹和入門實(shí)例,LokiJS是一個(gè)內(nèi)存數(shù)據(jù)庫(kù),將性能考慮放在第一位,使用JavaScript編寫,需要的朋友可以參考下
    2014-11-11
  • 5分鐘快速掌握J(rèn)S中var、let和const的異同

    5分鐘快速掌握J(rèn)S中var、let和const的異同

    在javascript中有三種聲明變量的方式:var、let、const,這個(gè)是對(duì)新手們來(lái)說(shuō)應(yīng)該掌握的知識(shí),所以這篇文章主要給大家介紹了關(guān)于如何通過(guò)5分鐘快速掌握J(rèn)S中var,let和const的異同,需要的朋友可以參考下
    2018-09-09
  • Javascript實(shí)現(xiàn)拖拽排序的代碼

    Javascript實(shí)現(xiàn)拖拽排序的代碼

    這篇文章主要介紹了Javascript實(shí)現(xiàn)拖拽排序的代碼,本文在vue運(yùn)行環(huán)境下給大家演示下效果圖,對(duì)js拖拽排序?qū)嵗a感興趣的朋友跟隨小編一起看看吧
    2022-09-09
  • js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL(支持大量數(shù)據(jù)導(dǎo)出)

    js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL(支持大量數(shù)據(jù)導(dǎo)出)

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL,支持大量數(shù)據(jù)導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • JS讀取cookies信息(記錄用戶名)

    JS讀取cookies信息(記錄用戶名)

    很多網(wǎng)站、博客把網(wǎng)頁(yè)生成html靜態(tài)頁(yè)面了,以利于搜索引擎的索引排名,減輕服務(wù)器負(fù)擔(dān)。靜態(tài)頁(yè)面由于其穩(wěn)定快速更快,給用戶及站長(zhǎng)帶來(lái)了方便。但評(píng)論后如何記住用戶的信息呢
    2012-01-01
  • 全面了解javascript中的錯(cuò)誤處理機(jī)制

    全面了解javascript中的錯(cuò)誤處理機(jī)制

    下面小編就為大家?guī)?lái)一篇全面了解javascript中的錯(cuò)誤處理機(jī)制。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07

最新評(píng)論