自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS 自定義組件之可拖拽圓形進(jìn)度條

系統(tǒng) OpenHarmony
在項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)用到自定義組件,此處分享一下HarmonyOS中JS如何利用canvas實(shí)現(xiàn)自定義組件之可拖拽圓形進(jìn)度條。

[[443107]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

簡(jiǎn)介

在項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)用到自定義組件,此處分享一下HarmonyOS中JS如何利用canvas實(shí)現(xiàn)自定義組件之可拖拽圓形進(jìn)度條。

效果演示

#星光計(jì)劃2.0# HarmonyOS 自定義組件之可拖拽圓形進(jìn)度條-鴻蒙HarmonyOS技術(shù)社區(qū)

實(shí)現(xiàn)思路

官方文檔:JS API參考-HarmonyOS應(yīng)用開(kāi)發(fā)

思路參考: 可拖拽圓形進(jìn)度條組件(支持移動(dòng)端))

這里并未采用官方文檔說(shuō)通過(guò)element引入到宿主頁(yè)面的方式;

#星光計(jì)劃2.0# HarmonyOS 自定義組件之可拖拽圓形進(jìn)度條-鴻蒙HarmonyOS技術(shù)社區(qū)

采用上述過(guò)程發(fā)了bug:canvas首次渲染繪制的不顯示;

解決方案: 在頁(yè)面生命周期onShow的時(shí)候,通過(guò)js讓canvas繪制一次;

  1. onShow() { 
  2.    // Todo 繪制 

1.項(xiàng)目結(jié)構(gòu)

#星光計(jì)劃2.0# HarmonyOS 自定義組件之可拖拽圓形進(jìn)度條-鴻蒙HarmonyOS技術(shù)社區(qū)

2.DragAcr初始化

定義構(gòu)造函數(shù),聲明繪制圓的參數(shù);

  1. export default class DragAcr { 
  2.     constructor(param) { 
  3.         this.initParam(param) 
  4.         this.draw(this.value) 
  5.     } 
  6.  
  7.     initParam(param) { 
  8.         const { 
  9.             el, 
  10.             startAngle = 0, 
  11.             endAngle = 2, 
  12.             width = 252, 
  13.             innerColor = "red"
  14.             outColor = "#08000000"
  15.             innerLineWidth = 1, 
  16.             outLineWidth = 24, 
  17.             counterclockwise = false
  18.             slider = 8, 
  19.             color = ["#ffffff""#0F75F3""#54C8A5""#FEDB00""red"], 
  20.             sliderColor = "#ffffff"
  21.             sliderBorderColor = "blue"
  22.             value = 0, 
  23.             change = (v) => { 
  24.                 console.log(v) 
  25.             }, 
  26.             textShow = true
  27.         } = param; 
  28.          
  29.            this.el = el; 
  30.         this.width = width; 
  31.         this.height = width; 
  32.         this.center = this.width / 2 
  33.         this.outLineWidth = outLineWidth; 
  34.         this.radius = this.width / 2 - this.outLineWidth / 2; 
  35.         // this.ctx = el.getContext("2d"); 
  36.         this.ctx = this.el.getContext('2d', { 
  37.             antialias: true 
  38.         }); 
  39.  
  40.         this.startAngle = startAngle; 
  41.         this.endAngle = endAngle; 
  42.         this.innerColor = innerColor; 
  43.         this.outColor = outColor; 
  44.         this.innerLineWidth = innerLineWidth; 
  45.         this.counterclockwise = counterclockwise; 
  46.         this.slider = slider; 
  47.         this.color = color; 
  48.         this.sliderColor = sliderColor; 
  49.         this.sliderBorderColor = sliderBorderColor; 
  50.         this.value = value; 
  51.         this.textShow = textShow; 
  52.         this.change = change; 
  53.         this.isDown = false

3.DragAcr繪制

canvas的API參考:canvas組件-畫布組件

根據(jù)當(dāng)前進(jìn)度 分段式的繪制需要的各個(gè)小控件;

  1. // 繪圖 
  2.  draw(value) { 
  3.         console.log(TAG + ';draw  value:' + value); 
  4.         if (value == undefined) { 
  5.             value = this.value; 
  6.         } else { 
  7.             this.value = value; 
  8.         } 
  9.  
  10.         this.ctx.clearRect(0, 0, this.width, this.width); 
  11.         this.ctx.save(); 
  12.  
  13.         let startDeg = this.counterclockwise ? Math.PI * (2 - this.startAngle) : Math.PI * this.startAngle 
  14.         let endDeg = this.counterclockwise ? Math.PI * (2 - this.endAngle) : Math.PI * this.endAngle 
  15.  
  16.  
  17.         // 繪制背景圓 
  18.         this.ctx.beginPath(); 
  19.         this.ctx.arc(this.center, this.center, this.radius, startDeg, 
  20.             endDeg, this.counterclockwise); 
  21.         this.ctx.strokeStyle = this.outColor; 
  22.         this.ctx.lineCap = "round"
  23.         this.ctx.lineWidth = this.outLineWidth; 
  24.         this.ctx.stroke(); 
  25.  
  26.         let Deg = this.valToDeg(value) 
  27.         this.drawOne(startDeg, value); 
  28.  
  29.         if (value > 25) { 
  30.             // 繪制可變圓弧 
  31.             this.drawTwo(value); 
  32.         } 
  33.  
  34.         if (value > 50) { 
  35.             // 繪制可變圓弧 
  36.             this.drawThree(value); 
  37.         } 
  38.  
  39.         if (value > 75) { 
  40.             this.drawFour(value) 
  41.         } 
  42.  
  43.         // 繪制滑塊bar 
  44.         this.P = this.DegToXY(Deg) 
  45.         this.ctx.beginPath(); 
  46.         this.ctx.moveTo(this.center, this.center,); 
  47.         this.ctx.arc(this.P.x, this.P.y, this.outLineWidth / 2, 0, Math.PI * 2, false); // 繪制滑塊內(nèi)側(cè) 
  48.         this.ctx.fillStyle = this.sliderBorderColor; 
  49.         this.ctx.fill(); 
  50.         this.ctx.beginPath(); 
  51.         this.ctx.moveTo(this.center, this.center); 
  52.         this.ctx.arc(this.P.x, this.P.y, this.slider, 0, Math.PI * 2, false); // 繪制滑塊 
  53.         this.ctx.fillStyle = this.sliderColor; 
  54.         this.ctx.fill(); 
  55.  
  56.         // 文字 
  57.         if (this.textShow) { 
  58.             this.ctx.font = '60px HarmonyHeiTi, HarmonyHeiTi-Medium'
  59.             this.ctx.fillStyle = "#000000"
  60.             this.ctx.textAlign = "center" 
  61.             this.ctx.textBaseline = "middle"
  62.             this.ctx.fillText(this.value + "", this.center, this.center); 
  63.         } 
  64.  
  65.         //  this.drawLine(); 
  66.     } 

4.DragAcr手勢(shì)監(jiān)聽(tīng)

手勢(shì)按下,手勢(shì)移動(dòng),手勢(shì)抬起的事件處理

  1. OnMouseMove(evt) { 
  2.     if (!this.isDown) return
  3.     let evpoint = {}; 
  4.     evpoint.x = this.getx(evt); 
  5.     evpoint.y = this.gety(evt); 
  6.     let point = this.spotchangeXY(evpoint); 
  7.     let deg = this.XYToDeg(point.x, point.y); 
  8.     //   console.log(TAG + '; OnMouseMove deg XYToDeg ...' + deg); 
  9.     deg = this.counterclockwise ? deg : Math.PI * 2 - deg; 
  10.     //  console.log(TAG + '; OnMouseMove deg...' + deg); 
  11.  
  12.     let val = (deg / Math.PI - this.startAngle) / (this.endAngle - this.startAngle) * 100 
  13.     val = Math.round(val) 
  14.     //  console.log(TAG + '; OnMouseMove val:' + val); 
  15.     if (val < 0) val = 100 + val; 
  16.     if (val <= 0) val = 0; 
  17.     if (val > 100) { 
  18.         if (this.value > 75) { 
  19.             val = 100; 
  20.         } else { 
  21.             val = val - 100; 
  22.         } 
  23.     } 
  24.     if (val > 75) { 
  25.         if (this.value < 25) { 
  26.             val = 0; 
  27.         } 
  28.     } 
  29.  
  30.     //    console.log(TAG + '; OnMouseMove val2:' + val); 
  31.     if (Math.abs(val - this.value) > 10) return
  32.     //   console.log(TAG + '; OnMouseMove val:' + val); 
  33.     this.animate = requestAnimationFrame(this.draw.bind(this, val)); 
  34.     if (this.value != Math.round(val)) { 
  35.         this.value = Math.round(val); 
  36.         this.change(this.value) 
  37.     } 
  38.     //   console.log(TAG + '; OnMouseMove end...'); 
  39.  
  40. OnMouseDown(evt) { 
  41.     let range = 10; 
  42.     let X = this.getx(evt); 
  43.     let Y = this.gety(evt); 
  44.     let P = this.P 
  45.  
  46.     let minX = P.x - this.slider - range; 
  47.     let maxX = P.x + this.slider + range; 
  48.     let minY = P.y - this.slider - range; 
  49.     let maxY = P.y + this.slider + range; 
  50.  
  51.     if (minX < X && X < maxX && minY < Y && Y < maxY) { //判斷鼠標(biāo)是否在滑塊上 
  52.         this.isDown = true
  53.     } else { 
  54.         this.isDown = false
  55.     } 
  56.     console.log(TAG + 'OnMouseDown end...'); 
  57.  
  58. OnMouseUp() { //鼠標(biāo)釋放 
  59.     const _this = this 
  60.     cancelAnimationFrame(_this.animate); 
  61.     this.isDown = false 
  62.     console.log(TAG + 'OnMouseUp end...'); 

5.使用方法

  1. index.hml文件 
  2. <div  class="container"
  3.     
  4.     <canvas ref="canvas2" 
  5.             style="width : 252px; height : 252px;" 
  6.             @touchstart="canvasTouchStart" 
  7.             on:touchmove="canvasTouchMove" 
  8.             on:touchend="canvasTouchEnd"></canvas> 
  9. </div> 
  10.  
  11. index.js文件 
  12.  
  13. import DragAcr2 from './dragAcr2.js' 
  14.  
  15. export default { 
  16.     dragAcr2: undefined 
  17.     data: { 
  18.      // 出事化值 
  19.      reverb2: 30, 
  20.     } 
  21.      onShow() { 
  22.           // 首次繪制 
  23.         this.initDragAcr2(); 
  24.     }, 
  25.     // 觸摸事件 
  26.     canvasTouchStart(msg) { 
  27.         //console.log('dragAcr  canvasTouchStart msg:' + msg); 
  28.         this.dragAcr2.OnMouseDown(msg); 
  29.     }, 
  30.     canvasTouchMove(msg) { 
  31.         //console.log('dragAcr  OnMouseMove msg:' + msg); 
  32.         this.dragAcr2.OnMouseMove(msg); 
  33.     }, 
  34.     canvasTouchEnd(msg) { 
  35.         // console.log('dragAcr  canvasTouchEnd msg:' + msg); 
  36.         this.dragAcr2.OnMouseUp(msg); 
  37.     }, 
  38.  
  39.     initDragAcr2() { 
  40.         const el = this.$refs.canvas2; 
  41.         if (this.dragAcr2 == undefined) { 
  42.             this.dragAcr2 = new DragAcr2({ 
  43.                 el: el, 
  44.                 value: this.reverb2, 
  45.                 change: (v) => { 
  46.                     console.log(`value:${v}`) 
  47.                 } 
  48.             }) 
  49.         } 
  50.     } 

總結(jié)

1,目前在API6及一下手機(jī),canvas繪制時(shí)會(huì)是屏幕閃爍(API7遠(yuǎn)程模式無(wú)此現(xiàn)象);

2,無(wú)論什么語(yǔ)言,思路都是大體相同,繪制,手勢(shì),事件分發(fā)等。

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2017-03-14 15:09:18

AndroidView圓形進(jìn)度條

2021-09-06 14:58:23

鴻蒙HarmonyOS應(yīng)用

2022-09-09 14:47:50

CircleArkUI

2021-12-07 18:23:50

自定義進(jìn)度條分段式

2021-01-11 11:36:23

鴻蒙HarmonyOSApp開(kāi)發(fā)

2021-11-01 10:21:36

鴻蒙HarmonyOS應(yīng)用

2022-04-24 14:56:53

容器組件StackTS

2021-09-27 10:43:18

鴻蒙HarmonyOS應(yīng)用

2022-06-30 14:02:07

鴻蒙開(kāi)發(fā)消息彈窗組件

2022-07-15 16:45:35

slider滑塊組件鴻蒙

2022-06-20 15:43:45

switch開(kāi)關(guān)鴻蒙

2023-07-18 15:49:22

HTMLCSS

2021-12-24 15:46:23

鴻蒙HarmonyOS應(yīng)用

2022-07-12 16:56:48

自定義組件鴻蒙

2015-07-31 11:19:43

數(shù)字進(jìn)度條源碼

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2022-02-17 14:51:39

HarmonyOSJSPAI開(kāi)發(fā)canvas畫布

2022-05-20 14:34:20

list組件鴻蒙操作系統(tǒng)

2023-02-20 15:20:43

啟動(dòng)頁(yè)組件鴻蒙

2021-11-24 10:02:53

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)