小程序demo(Canvas時(shí)鐘)

效果圖如下:


clock.gif

wxml文件

<view>
  <canvas canvas-id='clock' class='canvas'></canvas>
</view>

wxss文件

.canvas{
  width: 100%;
  height: 100%;
  position: fixed;
}

json文件(可以為空下面設(shè)置的是標(biāo)題)

{
  "navigationBarTitleText": "消息"
}

js文件

Page({
  data: {
    width:0,
    height:0
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.width = res.windowWidth;
        that.height = res.windowHeight;
      },
    })
  },
  onReady: function () {
    this.canvasClock();
    this.interval = setInterval(this.canvasClock,1000);
  },
  onUnload: function () {
    clearInterval(this.interval)
  },
  onShareAppMessage: function () {
    return {
      title:'轉(zhuǎn)發(fā)canvas時(shí)鐘'
    }
  },
  canvasClock: function(){
    const ctx = wx.createContext();
    const width = this.width;
    const height = this.height;
    const R = width/2-55;
    function reSet(){
      ctx.height = ctx.height;
      ctx.translate(width/2,height/2);
      ctx.save();
    }
    // 圓盤(pán)
    function circle(){
      ctx.setLineWidth(2);
      ctx.beginPath();
      ctx.setStrokeStyle('gray');
      ctx.arc(0,0,width/2-30,0,2 * Math.PI,true);
      ctx.closePath();
      ctx.stroke();
      ctx.beginPath();
      ctx.arc(0,0,8,0,2 * Math.PI,true);
      ctx.closePath();
      ctx.stroke();
    }
    // 數(shù)字
    function num(){
      ctx.setFontSize(20);
      ctx.setTextBaseline('middle');
      for(let i = 1; i < 13; i++){
        const x = (R - 10) * Math.cos(i * Math.PI / 6 - Math.PI / 2);
        const y = (R - 10) * Math.sin(i * Math.PI / 6 - Math.PI / 2);
        if(i === 11 || i === 12){
          ctx.fillText(i, x-12, y+9);
        }else{
          ctx.fillText(i, x-6, y+9);
        }
      }
    }
    // 刻度 start
    function smallGrid(){
      ctx.setLineWidth(1);
      ctx.rotate(-Math.PI / 2);
      for( let i = 0; i < 60; i++){
        ctx.beginPath();
        ctx.rotate(Math.PI / 30);
        ctx.moveTo(width / 2 - 30, 0);
        ctx.lineTo(width / 2 - 40, 0);
        ctx.stroke();
      }
    }
    function bigGrid() {
      ctx.setLineWidth(5);
      for (let i = 0; i < 12; i++) {
        ctx.beginPath();
        ctx.rotate(Math.PI / 6);
        ctx.moveTo(width / 2 - 30, 0);
        ctx.lineTo(width / 2 - 45, 0);
        ctx.setStrokeStyle('black');
        ctx.stroke();
      }
    }
    // 刻度 end
    // 指針移動(dòng)方法
    function move(){
      const date = new Date();
      let h = date.getHours();
      h = (h > 12) ? (h - 12) : h;
      const m = date.getMinutes();
      const s = date.getSeconds();
      ctx.save();
      // 時(shí)針
      ctx.setLineWidth(7);
      ctx.beginPath();
      ctx.setStrokeStyle('orange');
      ctx.rotate((Math.PI / 6) * (h + m / 60 + s / 3600));
      ctx.moveTo(-20, 0);
      ctx.lineTo(width / 4.5 - 20,0);
      ctx.stroke();
      ctx.restore();
      ctx.save();
      // 分針
      ctx.setLineWidth(5);
      ctx.beginPath();
      ctx.setStrokeStyle('blue');
      ctx.rotate((Math.PI / 30) * (m + s / 60));
      ctx.moveTo(-20, 0);
      ctx.lineTo(width / 3.5 - 20, 0);
      ctx.stroke();
      ctx.restore();
      ctx.save();
      // 秒針
      ctx.setLineWidth(2);
      ctx.beginPath();
      ctx.setStrokeStyle('red');
      ctx.rotate((Math.PI / 30) * s);
      ctx.moveTo(-20, 0);
      ctx.lineTo(width / 3 - 20, 0);
      ctx.stroke();
    }
    function drawClock(){
      reSet();
      circle();
      num();
      smallGrid();
      bigGrid();
      move();
    }
    drawClock();
    wx.drawCanvas({
      canvasId: 'clock',
      actions: ctx.getActions()
    })
  }
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 每天的學(xué)習(xí)記錄,可能有的地方寫(xiě)的不對(duì),因?yàn)閯倢W(xué),以后發(fā)現(xiàn)錯(cuò)的話會(huì)回來(lái)改掉整體流程 https://develope...
    有點(diǎn)健忘閱讀 5,045評(píng)論 0 7
  • 微信小程序由于適用性強(qiáng)、邏輯簡(jiǎn)要、開(kāi)發(fā)迅速的特性,疊加具有海量活躍用戶(hù)的騰訊公司背景,逐漸成為了輕量級(jí)單一功能應(yīng)用...
    純文筆記閱讀 4,202評(píng)論 1 9
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,659評(píng)論 19 139
  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飄閱讀 1,687評(píng)論 0 6
  • 又見(jiàn)面了!這里是第一期! 嘿大家好!這是一個(gè)系列連載的教(瞎)程(扯),主要會(huì)給“優(yōu)秀的大學(xué)生”講一講與蘋(píng)果電腦相...
    武希希閱讀 16,711評(píng)論 9 30

友情鏈接更多精彩內(nèi)容