首页 > 其他分享 >移动端,cavans和svg绘制进度图

移动端,cavans和svg绘制进度图

时间:2023-04-10 21:13:51浏览次数:33  
标签:return svg isDark ctx stroke cavans document 绘制 Math

先看效果:

起初是用cavans绘制的 结果会模糊,倍数绘制再缩小也是模糊,最后换成了svg绘制:

cavans:

global.progressChart = {
    template: ` 
      <div class="process-chart-box">
        <canvas id="progressChart">当前浏览器不支持canvas</canvas>
        <img src="xxx/progress_icon.png" class="progress-icon" v-if="per>0" />
      </div>
      `,
    data() {
      return {
        isDark: false
      }
    },
    props: ['per'],
    methods: {
      handleCanvas() {
        const isDark = this.isDark
        const percent = Math.round(this.per) / 100
        let rem =
          (document.body.offsetWidth / 750) * 100 < 60
            ? (document.body.offsetWidth / 750) * 100
            : 60
        let labelW = Math.round(0.32 * rem)

        const box = document.querySelector('.process-chart-box')
        if (!box) return
        const w = box.offsetWidth
        const ctx = document.getElementById('progressChart').getContext('2d')
        ctx.canvas.width = w + 1
        ctx.canvas.height = w + 1

        //外圆环
        ctx.beginPath()
        ctx.arc(w / 2, w / 2, w / 2, 0, 2 * Math.PI)
        //strokeStyle边框  fillStyle填充
        ctx.strokeStyle = isDark ? '#262626' : '#fff'
        ctx.fillStyle = isDark ? '#283851' : '#d6e6ff'
        ctx.fill()
        ctx.stroke()
        //内圆环
        ctx.beginPath()
        ctx.arc(w / 2, w / 2, w / 2 - labelW, 0, 2 * Math.PI)
        ctx.strokeStyle = isDark ? '#262626' : '#fff'
        ctx.fillStyle = isDark ? '#262626' : '#fff'
        ctx.fill()
        ctx.stroke()
        //环形图的进度条
        if (percent > 0) {
          ctx.beginPath()
          ctx.arc(
            w / 2,
            w / 2,
            (w - labelW) / 2,
            -Math.PI / 2,
            -Math.PI / 2 + percent * (Math.PI * 2),
            false
          )
          ctx.lineWidth = labelW
          ctx.lineCap = 'round'
          ctx.strokeStyle = isDark ? '#2f71de' : '#3480ff'
          ctx.stroke()
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.getDisplayMode()
          .then(res => {
            this.isDark = !!res
            this.handleCanvas()
          })
          .catch(() => {
            this.handleCanvas()
          })
        window.addEventListener(
          'resize',
          () => {
            this.handleCanvas()
          },
          false
        )
      })
    },
    watch: {
      per() {
        this.handleCanvas()
      }
    }
  }

svg:

  global.progressChart = {
    template: ` 
      <div class="process-chart-box">
        <svg :width="w" :height="w" version="1.1" xmlns="http://www.w3.org/2000/svg">
          <circle  class="progress-bg" :cx="w/2" :cy="w/2" :r="r" :stroke="isDark ? '#283851' : '#d6e6ff'" :stroke-width="progressW" fill="none"/>
          <circle :style="+per>0?'':'display:none'" class="progress" :transform="rotate" :cx="w/2" :cy="w/2" :r="r" :stroke="isDark ? '#2f71de' : '#3480ff'" :stroke-width="progressW" fill="none" stroke-linecap="round" />
        </svg>
        <img src="xxx/progress_icon.png" class="progress-icon" v-if="per>0" />
      </div>
      `,
    data() {
      return {
        isDark: false
      }
    },
    props: ['per'],
    computed: {
      remToPx() {
        let rem =
          (document.body.offsetWidth / 750) * 100 < 60 ?
          (document.body.offsetWidth / 750) * 100 :
          60
        console.log('remToPx', rem);
        return rem
      },

      // 容器宽度
      w() {
        return 2.2 * this.remToPx
      },
      // 进度宽度
      progressW() {
        return 0.28 * this.remToPx
      },
      // 绘制半径
      r() {
        return (this.w - this.progressW) / 2
      },
      // 进度旋转
      rotate() {
        return `rotate(-90,${this.w/2},${this.w/2})`
      }
    },
    methods: {
      handleSvg() {
        //换成svg绘制
        var progressDom = document.querySelector(".progress");
        if (!progressDom) {
          return
        }
        let persent = this.per
        if (persent > 0) {
          var circleLength = Math.floor(2 * Math.PI * parseFloat(progressDom.getAttribute("r")));
          var value = persent * circleLength / 100;
          progressDom.setAttribute("stroke-dasharray", value + ",10000");
        }

      },
    },
    mounted() {
      this.$nextTick(() => {
        this.getDisplayMode()
          .then(res => {
            this.isDark = !!res
            this.handleSvg()
          })
          .catch(() => {
            this.handleSvg()
          })
      })
    },
    watch: {
      per() {
        this.handleSvg()
      }
    }
  }

标签:return,svg,isDark,ctx,stroke,cavans,document,绘制,Math
From: https://www.cnblogs.com/dxy9527/p/17304279.html

相关文章

  • MFC-FrameRect绘制矩形边框
     HDChdc=::GetDC(m_hWnd);RECTrect={10,10,50,100};HBRUSHhbr;hbr=CreateSolidBrush(RGB(255,0,0));intf=FrameRect(hdc,&rect,hbr);//绘制矩形边框/*参数1:HDChdc将要画边框的设备环境句柄参数2:CONSTRECT......
  • MFC-FillRect绘制并填充矩形
     HDChdc=::GetDC(m_hWnd);RECTrect={10,10,50,100};HBRUSHhbr;hbr=CreateSolidBrush(RGB(255,0,0));SelectObject(hdc,hbr);intf=FillRect(hdc,&rect,hbr);//绘制并填充矩形/*用指定的画刷填充矩形,此函数包......
  • MFC-Rectangle绘制矩形
     HDChdc=::GetDC(m_hWnd);::MoveToEx(hdc,0,0,NULL);HPENhpen=CreatePen(PS_SOLID,3,RGB(0,0,255));SelectObject(hdc,hpen);BOOLb=Rectangle(hdc,0,0,50,100);//绘制矩形/*该矩形用当前画笔绘制轮廓,用当前画刷填充......
  • 让Window可以预览查看Svg图标的解决方法
    让Window可以预览查看Svg图标的解决方法下载插件包下载地址:https://github.com/maphew/svg-explorer-extension/releases也可以直接下载安装包https://github.com/tibold/svg-explorer-extension/releases/download/v1.1.0/svg_see_x64.exe解压并安装下载后解压svg-e......
  • 百度高德地图行政区域边界GeoJSON数据获取并绘制行政区域
    highcharts是提供地图数据包的:https://www.highcharts.com/docs/maps/map-collectionechart矢量地图或者地图绘制矢量图层,GeoJSON哪里提供呢?dataV提供数据下载,http://datav.aliyun.com/tools/atlas/#&lat=30.332329214580188&lng=106.75386074913891&zoom=4.5这些数据也是从高......
  • C++,OpenCV图形绘制与文字输出(7)
    绘线voidline(InputOutputArrayimg,Pointpt1,Pointpt2,constScalar&color,intthickness=1,intlineType=LINE_8,intshift=0);//线的样式enumLineTypes{FILLED=-1,LINE_4=4,//!<4-connectedlineLINE_8=8,//!<8-connec......
  • 关于绘制UML
       任何建模语言都以静态建模机制为基础,UNL也不例外。UML的静态建模机制包括用例图、类图、对象图、包图等。用例图从用户的角度描述系统的功能,由用例(usecase)、参与者(actor)以及他们的关系连线组成。用例从用户角度描述系统的行为,他将系统的一个功能描述成一系列的事件......
  • 图形软件绘制UML用例图
    用例图是参与者(角色)可以感受到的系统服务或功能单元。它定义了系统是如何被参与者使用的,描述了参与者为使用系统所提供的某一完整功能而与系统之间发生的一段对话。用例最大的优点就是站在用户的角度上(从系统的外部)来描述系统的功能。它把系统当作一个黑箱子,并不关心系统内部是如......
  • matlab连续潮流程序绘制PV曲线 静态电压稳定 该程序为连续潮流IEEE14节点和33节点的程
    matlab连续潮流程序绘制PV曲线静态电压稳定该程序为连续潮流IEEE14节点和33节点的程序运行出来有分岔点和鼻点可移植性强,注释详细YID:53120676973227785......
  • winCE 页面绘制 例子一则
     效果如下:   usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Reflection;usingSystem.Drawing.Imaging;n......