小程序画带圆角的圆形进度条

打印 上一主题 下一主题

主题 960|帖子 960|积分 2880



老的API
  1. <canvas id="{{canvasId}}" canvas-id="{{canvasId}}" style="opacity: 0;" class="canvas"/>
复制代码
  1. startDraw() {
  2.       const { canvasId } = this.data
  3.       const query = this.createSelectorQuery()
  4.         query
  5.         .select(`#${canvasId}`)
  6.         .boundingClientRect(res => {
  7.           if (res?.width) {
  8.             const width = res.width
  9.             const height = res.height
  10.             this.data.canvasWidth = width
  11.             this.data.canvasHeight = height
  12.           }
  13.           this.data.canvasContext = wx.createCanvasContext(canvasId, this)
  14.           this.startProgress()
  15.         })
  16.         .exec()
  17.     },
  18.     startProgress() {
  19.       const context = this.data.canvasContext
  20.       const width = this.data.canvasWidth
  21.       const height = this.data.canvasHeight
  22.       // 计算百分比
  23.       let  percentage = 1
  24.       // 传入剩余和总数 或者传入进度 能算出百分比就行
  25.       const remain = 0
  26.       const total = 0
  27.       const progress = total - remain
  28.       if (remain && total) {
  29.         percentage = progress / total
  30.       }
  31.       // 原点
  32.       const centerX = width / 2
  33.       const cenetrY = height / 2
  34.       // 半径
  35.       const radius = width / 2 - 12
  36.       // 线条粗细
  37.       const lineWidth = 10
  38.       // 线条形状
  39.       const lineCap = 'round'
  40.       // 背景条颜色
  41.       let backgroundColor = 'rgba(80, 158, 46, 0.40)'
  42.       // 进度条颜色
  43.       let progressColor = '#509E2E'
  44.    
  45.       // 背景圆环
  46.       context.beginPath()
  47.       context.arc(
  48.         centerX,
  49.         cenetrY,
  50.         radius,
  51.         -0.75 * Math.PI,
  52.         1.5 * Math.PI,
  53.         false
  54.       )
  55.       context.lineWidth = lineWidth
  56.       context.lineCap = lineCap
  57.       context.strokeStyle = backgroundColor
  58.       context.stroke()
  59.       // 进度圆环
  60.       if (remain && total) {
  61.         context.beginPath()
  62.         context.arc(
  63.           centerX,
  64.           cenetrY,
  65.           radius,
  66.           -0.5 * Math.PI,
  67.           (-0.5 + 2 * percentage) * Math.PI,
  68.           false
  69.         )
  70.         context.lineWidth = lineWidth
  71.         context.lineCap = lineCap
  72.         context.strokeStyle = progressColor
  73.         context.stroke()
  74.       }
  75.       context.draw()
  76.     },
复制代码

2d
  1.       <canvas type="2d" canvas-id="{{canvasId}}" id="{{canvasId}}" class="canvas"/>
复制代码
  1. startDraw() {
  2.       const { canvasId, canvasWidth, canvasHeight } = this.data
  3.       const query = this.createSelectorQuery().in(this)
  4.   
  5.         query
  6.         .select(`#${canvasId}`)
  7.         .fields({ node: true, size: true })
  8.         .exec(res => {
  9.           const canvas = res[0].node
  10.           const ctx = canvas.getContext('2d')
  11.           canvas.width = canvasWidth
  12.           canvas.height = canvasHeight
  13.           this.data.canvasContext = ctx
  14.           this.startProgress()
  15.         })
  16.     },
  17.     startProgress() {
  18.       const context = this.data.canvasContext
  19.       const width = this.data.canvasWidth
  20.       const height = this.data.canvasHeight
  21.       context.clearRect(0, 0, width, height)
  22.       // 计算百分比
  23.       let  percentage = 1
  24.       // 设置剩余和总数
  25.       const remain = 50
  26.       const total = 100
  27.       const progress = total - remain
  28.       if (remain && total) {
  29.         percentage = progress / total
  30.       }
  31.       // 原点
  32.       const centerX = width / 2
  33.       const cenetrY = height / 2
  34.       // 半径
  35.       const radius = width / 2 - 12
  36.       // 线条粗细
  37.       const lineWidth = 14
  38.       // 线条形状
  39.       const lineCap = 'round'
  40.       // 背景条颜色
  41.       let backgroundColor = 'rgba(80, 158, 46, 0.40)'
  42.       // 进度条颜色
  43.       let progressColor = '#509E2E'
  44.       // 异常颜色
  45.       if (deviceStatus == 'OFFLINE') {
  46.         backgroundColor = 'rgba(208, 2, 27, 0.40)'
  47.         progressColor = '#D0021B'
  48.       }
  49.       // 背景圆环
  50.       context.beginPath()
  51.       context.arc(
  52.         centerX,
  53.         cenetrY,
  54.         radius,
  55.         -0.75 * Math.PI,
  56.         1.5 * Math.PI,
  57.         false
  58.       )
  59.       context.lineWidth = lineWidth
  60.       context.lineCap = lineCap
  61.       context.strokeStyle = backgroundColor
  62.       context.stroke()
  63.       // 进度圆环
  64.       if (remain && total) {
  65.         context.beginPath()
  66.         context.arc(
  67.           centerX,
  68.           cenetrY,
  69.           radius,
  70.           -0.5 * Math.PI,
  71.           (-0.5 + 2 * percentage) * Math.PI,
  72.           false
  73.         )
  74.         context.lineWidth = lineWidth
  75.         context.lineCap = lineCap
  76.         context.strokeStyle = progressColor
  77.         context.stroke()
  78.       }
  79.     },
复制代码
this.createSelectorQuery().in(this)要在ready之后调用哦
css
  1. .canvas {
  2.         height: 340rpx;
  3.         width: 340rpx;
  4.    
  5.       }
复制代码
老api js里宽高的设置的是170
2d里宽高设置的是340


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

雁过留声

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表