微信小程序canvas實(shí)現(xiàn)儀表盤(pán)動(dòng)畫(huà)

1571137725714.jpg

效果圖如上圖所示,上篇文章分享了用css clip 實(shí)現(xiàn)圓盤(pán)loading。
但是由于動(dòng)畫(huà)效果有虛線和漸變,所以綜合考慮,選擇用canvas實(shí)現(xiàn)。

涉及知識(shí)點(diǎn):

繪制圓?。?/h4>

CanvasContext.arc(number x, number y, number r, number sAngle, number eAngle, boolean counterclockwise)

下載.png

針對(duì) arc(100, 75, 50, 0, 1.5 * Math.PI)的三個(gè)關(guān)鍵坐標(biāo)如下:

綠色: 圓心 (100, 75)
紅色: 起始弧度 (0)
藍(lán)色: 終止弧度 (1.5 * Math.PI)

繪制虛線:

CanvasContext.setLineDash(Array.<number> pattern, number offset)

Array.<number> pattern
一組描述交替繪制線段和間距(坐標(biāo)空間單位)長(zhǎng)度的數(shù)字(虛線長(zhǎng)度與虛線間距)

number offset
虛線偏移量

繪制底部灰色背景儀表盤(pán)代碼:

drawBack:function(){
    ctx.beginPath();
    ctx.arc(120, 120, 110, (5 / 6) * Math.PI, (13 / 6) * Math.PI)
    ctx.strokeStyle = '#4e6a68';
    ctx.lineWidth = 8;
    ctx.setLineDash([0]);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(120, 120, 119, (5 / 6) * Math.PI, (13 / 6) * Math.PI)
    ctx.strokeStyle = '#4e6a68';
    ctx.lineWidth = 2;
    ctx.setLineDash([2, 12]);
    ctx.stroke();
    ctx.draw();
  },

繪制填充顏色代碼:

drawRight:function(start,end){
    now = start;
    let that = this;
    canvasInterval = setInterval(function () {
      if (now > end) {
        clearInterval(canvasInterval);
      } else {
        that.draw(now);
        now += (end-start)/(time/5);
      }
    }, 5);
  },
  drawLeft: function (start, end) {
    now = start;
    let that = this;
    canvasInterval = setInterval(function () {
      if (now < end) {
        clearInterval(canvasInterval);
      } else {
        that.draw(now);
        now -= (start - end) / (time / 5);
      }
    }, 5);
  },
  draw: function (now){
    //繪制背景底盤(pán)
    ctx.beginPath();
    ctx.arc(120, 120, 110, (5 / 6) * Math.PI, (13 / 6) * Math.PI)
    ctx.strokeStyle = '#4e6a68';
    ctx.lineWidth = 8;
    ctx.setLineDash([0]);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(120, 120, 119, (5 / 6) * Math.PI, (13 / 6) * Math.PI);
    ctx.strokeStyle = '#4e6a68';
    ctx.lineWidth = 2;
    ctx.setLineDash([2, 12]);
    ctx.stroke();
    
    //繪制填充顏色部分
    ctx.beginPath();
    ctx.strokeStyle = '#18c9b2';
    ctx.arc(120, 120, 110, (5 / 6) * Math.PI, now * Math.PI);
    ctx.lineWidth = 8;
    ctx.setLineDash([0]);
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(120, 120, 119, (5 / 6) * Math.PI, now * Math.PI);
    ctx.lineWidth = 2;
    ctx.setLineDash([2, 12]);
    ctx.stroke();

    ctx.draw();
  },

time是為了和指針旋轉(zhuǎn)動(dòng)畫(huà)時(shí)間一致引入的參數(shù)。
指針動(dòng)畫(huà)直接用css即可實(shí)現(xiàn)。transform:rotate({{angle}}deg)

微信小程序下載文件有最大10M的限制,如果要進(jìn)行大文件下載需要平臺(tái)側(cè)進(jìn)行拆分
?著作權(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)容

  • 18- UIBezierPath官方API中文翻譯(待校對(duì)) ----------------- 華麗的分割線 -...
    醉臥欄桿聽(tīng)雨聲閱讀 1,171評(píng)論 1 1
  • 今天項(xiàng)目有個(gè)需求,發(fā)過(guò)來(lái)瞅了一眼,尼瑪哦,這尼瑪是畫(huà)的吧,畫(huà)的吧?。。。▓D1) 趕緊百度一波 ,沒(méi)有。。。沒(méi)有。。...
    驀然暖心閱讀 1,059評(píng)論 0 0
  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開(kāi)發(fā)出絢麗的界面效果,一方面得益于成功系統(tǒng)的設(shè)計(jì),另一方面得益...
    韓七夏閱讀 2,981評(píng)論 2 10
  • 一、canvas簡(jiǎn)介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    J_L_L閱讀 1,697評(píng)論 0 4
  • 親愛(ài)的寶貝兒,今天下午不和諧的聲音悄悄的彌漫在我們周圍,讓我們彼此之間都給了對(duì)方極其傷害性的眼神,讓我們彼此...
    茹珊閱讀 255評(píng)論 0 1

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