canvas 实现环形进度条

H5 PC

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas" height="100" width="100"></canvas>
    <script>
        //获取画布
        var canvas =document.querySelector('#canvas');
        canvas.style.border="1px solid black";
        //获取画笔
        var ctx=canvas.getContext('2d');

        //画出圆环
        //画出进度条
        function fillProcess(numDeg=10){
            //开启路径
            ctx.beginPath();
            ctx.lineWidth=8;
            ctx.arc(50,50,50,Math.PI*1.5,Math.PI*1.5+Math.PI*numDeg/100*2,false);
            ctx.lineCap="round";
            ctx.strokeStyle="#008000";
            ctx.stroke();
            //关闭路径
            ctx.closePath();

            //画出填充背景
            ctx.beginPath();
            ctx.arc(50,50,44,Math.PI*2,false)
            ctx.fillStyle="#ccc";
            ctx.fill()
            ctx.closePath();

            //填充文字
            ctx.beginPath();
            ctx.font="14px math";
            ctx.fillStyle="blue";
            ctx.fillText((numDeg+'%'),45,55,40)
            ctx.closePath();
        }
        
        fillProcess(80)
        </script>
</body>
</html>

微信小程序

wxml

<canvas type="2d" id="myCanvas" width="100%" height="100%"></canvas>

wx.js

// pages/version3/addStudent/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canvasDom: null,              // 画布dom对象
    canvas:null,                  // 画布的节点
    ctx: null,                    // 画布的上下文
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //表单提交
  submitForm:function(e){
    console.log(e.detail.value)
  },

  //查询节点信息,并准备绘制图像
  drawImage(){
    const query=wx.createSelectorQuery();
    query.select('#myCanvas')
    .fields({
      node:true,
      size:true
    }).exec((res)=>{
      const dom=res[0];
      const canvas=dom.node;
      const ctx=canvas.getContext('2d');
      const dpr=wx.getSystemInfoSync().pixelRatio;
      this.setData({
         canvasDom: dom,   // 把canvas的dom对象放到全局
          canvas: canvas,   // 把canvas的节点放到全局
          ctx: ctx,         // 把canvas 2d的上下文放到全局
      },function(){
        this.drawing()      // 开始绘图
      })
    })
  },
  drawing:function(deg=100){
      //绘制外圈画面
    console.log(this.data.ctx);
    this.data.ctx.beginPath();
    this.data.ctx.strokeStyle="#ccc";
    this.data.ctx.lineWidth=2.5;
    // this.data.ctx.arc(100,100,25,Math.PI*1.5,Math.PI*1.5+Math.PI*2*deg/100,false);
    this.data.ctx.arc(100,100,15.5,Math.PI*1.5,Math.PI*1.5+Math.PI*2,false);
    this.data.ctx.stroke();
    this.data.ctx.closePath();

    //绘制画面
    console.log(this.data.ctx);
    this.data.ctx.beginPath();
    this.data.ctx.strokeStyle="green";
    this.data.ctx.lineWidth=2.5;
    this.data.ctx.arc(100,100,15.5,Math.PI*1.5,Math.PI*1.5+Math.PI*2*deg/100,false);
    // this.data.ctx.arc(100,100,31,Math.PI*1.5,Math.PI*1.5+Math.PI*2,false);
    this.data.ctx.stroke();
    this.data.ctx.closePath();
    //绘制文本
    this.data.ctx.beginPath();
     this.data.ctx.font = "18rpx Math";   
    this.data.ctx.fillStyle="black";
    this.data.ctx.fillText((deg+'%'),85,105,60)
    this.data.ctx.closePath();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.drawImage()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

WX 小程序组件 环形进度条 

wxml

<view class="garage-status">
  <canvas type="2d" id="{{canvasId}}" class="canvas" style="width: {{customOptions.canvasSize.width}}rpx; height: {{customOptions.canvasSize.height}}rpx"></canvas>
  <view class="status-count">提交率</view>
</view>

 

 wxss

/* components/process/index.wxss */
.garage-status {
  position: relative;
  width: 90rpx;
  height: 120rpx;
  box-sizing: border-box;
  border: 1px solid orange;
  left: 100rpx;
  top: 100rpx;
  overflow:hidden;
}

.status-count {
  position: absolute;
  bottom:0;
  width: 90rpx;
  height: 42rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #999999;
  line-height: 42rpx;
}

.canvas {
  margin-left: 10rpx;
  margin-top: 10rpx;
}

wx.js

// components/process/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //进度条百分比
    deg: {
      type: Number,
      value: 15,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    canvasDom: null, // 画布dom对象
    canvas: null, // 画布的节点
    ctx: null, // 画布的上下文
    canvasId: `mp_progress_${new Date().getTime()}`,
    customOptions: {
      canvasSize: {
        width: 600,
        height: 300,
      },
      percent: 100,
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //查询节点信息,并准备绘制图像
    drawImage() {
       const query = wx.createSelectorQuery().in(this);
       let id='#'+this.data.canvasId;
      query
        .select(id)
        .fields({
          node: true,
          size: true,
        })
        .exec((res) => {
          const dom = res[0];
          const canvas = dom.node;
          const ctx = canvas.getContext("2d");
          const dpr = wx.getSystemInfoSync().pixelRatio;
          this.setData(
            {
              canvasDom: dom, // 把canvas的dom对象放到全局
              canvas: canvas, // 把canvas的节点放到全局
              ctx: ctx, // 把canvas 2d的上下文放到全局
            },
            function () {
              this.drawing(this.data.deg); // 开始绘图
            }
          );
        });
    },
    drawing: function (deg = 100) {
      //绘制外圈画面
      console.log(this.data.ctx);
      this.data.ctx.beginPath();
      this.data.ctx.strokeStyle = "#ccc";
      this.data.ctx.lineWidth = 2.5;
      // this.data.ctx.arc(100,100,25,Math.PI*1.5,Math.PI*1.5+Math.PI*2*deg/100,false);
      this.data.ctx.arc(
        16,
        16,
        15.5,
        Math.PI * 1.5,
        Math.PI * 1.5 + Math.PI * 2,
        false
      );
      this.data.ctx.stroke();
      this.data.ctx.closePath();

      //绘制画面
      this.data.ctx.beginPath();
      this.data.ctx.strokeStyle = "rgba(51, 147, 221, 1)";
      this.data.ctx.lineWidth = 2.5;
      this.data.ctx.arc(
        16,
        16,
        15.5,
        Math.PI * 1.5,
        Math.PI * 1.5 + (Math.PI * 2 * deg) / 100,
        false
      );
      // this.data.ctx.arc(100,100,31,Math.PI*1.5,Math.PI*1.5+Math.PI*2,false);
      this.data.ctx.stroke();
      this.data.ctx.closePath();
      //绘制文本
      this.data.ctx.beginPath();
      this.data.ctx.font = "18rpx Math";
      this.data.ctx.fillStyle = "black";
      this.data.ctx.fillText(deg + "%", 6.5, 19.5, 60);
      this.data.ctx.closePath();
    },

  },
   attached: function() {
    // 在组件实例进入页面节点树时执行
    this.drawImage()
  },
});

 

 

WX

wx.ml

<view class="circle_box" style="width:{{size}}px;height:{{size}}px;position:relative">
      <!-- <canvas class="circle_bg" canvas-id="{{draw}}bg" style="width:{{size}}px;height:{{size}}px;opacity:0;display:{{show}}"></canvas>  -->
      <canvas class="circle_draw" canvas-id="{{draw}}" style="width:{{size}}px;height:{{size}}px;opacity:0;display:{{show}}"></canvas> 
      <!-- <canvas class="circle_bg" canvas-id="{{draw}}bg" style="width:{{size}}px;height:{{size}}px;opaity:0"></canvas>  -->
      <!-- <image src="{{url}}" style="width:{{size}}px;height:{{size}}px;"></image> -->
      <image src="{{url1}}" style="width:{{size}}px;height:{{size}}px;"></image>
      <text class='circle_txt'> {{txt}}<text>%</text>  </text>
      <text class="true">提交率</text>
</view>
View Code

wx.ss

.circle_box,
.circle_draw {
  position: relative;
}

.circle_bg {
  position: absolute;
  left: 0;
  top: 0;
}

.circle_box {
  display: flex;
  /* flex-direction: row; */
  justify-content: center;
  flex-wrap: wrap;
  /* align-items: center; */
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPwAAAD8CAYAAABTq8lnAAAAAXNSR0IArs4c6QAAKMJJREFUeAHtnVtTE9nbxf8I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7GF7SAPGv6g2kVbTh8NDAyMATpusZVqcz+MZuzDokA6AAxo4e5zR58DFhRg85BHfgBLqSaEW7Ja+qAMKf3grehS9ODYg3u74TaVMYwwaapyW0Rh8217e3tF051VqoT4KuBDiOXnFPx0ofYbhokmwR8BY9LUT6iI51D2sHCCRfT6N7RiNlA6qdkeUUvepswZQ7zaWwF/D+bzzyCTjqLGjG3rXet/J/1Gi2QDheca5s+/A3K9HDShhy/gExKy1hvCD/vvkjEJrg3VXtdvXwU2UViuYV0CB+AEua9E8U4K+Hj6me7ma4ex/HIIo9gjyMjDKAwSN+oxRSRjjqDFD2ixgVmIdSxf3tRrvtN/QAI+fY1PhMC13BjxHsaA2BBs7gcxeNZyLwQ5kSjDCaT3EDbyW0juJmYWNrSngEG0hJ0I+IQFDesdajgO+uUx3TaIqbYB/KYteWZG/cOmp8Z9EbX4FqbwtjF9t4VBtwJ+a9CtRqRG/hTwjVTbGBag78Gafdqa96NPm8fsWB6g9GEQK5MtAcSRU367mMorII4FFF47WGtOuNUPNz7zRjkT8I1SOmY4bAlgEDAHqHIAibvyshXAVxvn+J3z5Wl1DdgUh9/7CJfGPfzb43eEu4fCqIhBNtbkqrkhTNY/Aj7rTyhE/AAgX8bRMzw83A3jlB5MDfagtmU50MU/jBeUvgPO0nOH+5IRDvrVZLr0Ha2KQ0yJ7cOoaH9jY+MAJqsEXavNQjwHOZUCUkAKZEKB/wO0up3BJUNloQAAAABJRU5ErkJggg==) no-repeat 0 0/ 100%;
  width: 90rpx;
  height: 120rpx;
}

.circle_box image {
  position: absolute;
  left: 0;
  top: 0;
}

.circle_txt {
  position: absolute;
  top: 20rpx;
  text-align: center;
  flex: 1;
  height: 25rpx;
  font-size: 18rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;
  line-height: 25rpx;
}

.circle_txt text {
  font-size: 20rpx;
}

.true {
  top: 75rpx;
  position: absolute;
  width: 90rpx !important;
  line-height: 25rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #999999;
  line-height: 42rpx;
}
View Code

wx,js

// component/pro/index.js
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true, // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    per: {
      //百分比 通过此值转换成step
      type: String,
      value: "0",
    },
    r: {
      //半径
      type: String,
      value: "50",
    },
  },

  data: {
    /*  私有数据,可用于模版渲染 */
    step: 1, //用来算圆的弧度0-2
    size: 0, //画板大小
    screenWidth: 750, //实际设备的宽度
    txt: 0,
    show: "block",
    url: "http://tmp/wx2df78d360c5a8eb8.o6zAJs9cZQ_86upd1kTYsqHHzeyM.aQs4376TV8Wpafb6b2f30c54e908fc2ee0b0218bbd04.png",
    url1: "",
    draw: `mp_progress_${new Date().getTime()}`,
  },
  methods: {
    /**
     * el:画圆的元素
     * r:圆的半径
     * w:圆的宽度
     * 功能:画背景
     */
    // drawCircleBg: function (el, r, w) {
    //   // debugger
    //   const ctx = wx.createCanvasContext(el,this);
    //   ctx.setLineWidth(w);// 设置圆环的宽度
    //   // debugger
    //   ctx.setStrokeStyle('rgba(216,216,216,.2)'); // 设置圆环的颜色
    //   ctx.setLineCap('round') // 设置圆环端点的形状
    //   ctx.beginPath();//开始一个新的路径
    //   ctx.arc(r, r, r - w, 0, 2 * Math.PI, true);
    //   //设置一个原点(110,110),半径为100的圆的路径到当前路径
    //   ctx.stroke();//对当前路径进行描边
    //   ctx.draw();

    // },
    /**
     * el:画圆的元素
     * r:圆的半径
     * w:圆的宽度
     * step:圆的弧度 (0-2)
     * 功能:彩色圆环
     */
    drawCircle: function (el, r, w, step, ni) {
      var context = wx.createCanvasContext(el, this);
      // 设置渐变
      var gradient = context.createLinearGradient(2 * r, r, 0);
      gradient.addColorStop("0", "#3393DD");
      gradient.addColorStop("0.5", "#3393DD");
      gradient.addColorStop("1.0", "#3393DD");
      context.setLineWidth(w);
      context.setStrokeStyle(gradient);
      context.setLineCap("round");
      // debugger
      context.beginPath(); //开始一个新的路径
      // step 从0到2为一周
      context.arc(
        r,
        r,
        r - w,
        -Math.PI / 2,
        step * Math.PI - Math.PI / 2,
        false
      );
      context.stroke(); //对当前路径进行描边
      context.draw();
    },
  },

  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function () {
      const _this = this;
      //获取屏幕宽度
      wx.getSystemInfo({
        success: function (res) {
          _this.setData({
            screenWidth: res.windowWidth,
          });
        },
      });

      //初始化
      const el = _this.data.draw; //画板元素
      const per = _this.data.per; //圆形进度
      const r = Number(_this.data.r); //圆形半径

      _this.setData({
        step: (2 * Number(_this.data.per)) / 100,
        txt: _this.data.per,
      });

      //获取屏幕宽度(并把真正的半径px转成rpx)
      let rpx = (_this.data.screenWidth / 750) * r;
      //计算出画板大小
      this.setData({
        size: rpx * 2,
      });
      const w = 2; //圆形的宽度

      //组件入口,调用下面即可绘制 背景圆环和彩色圆环。
      // _this.drawCircleBg(el + 'bg', rpx, w);//绘制 背景圆环
      // setTimeout(function(){

      //   wx.canvasToTempFilePath({
      //     x: 0,
      //     y: 0,
      //     width: _this.data.size * 2,
      //     height: _this.data.size * 2,
      //     destWidth: _this.data.size * 2,
      //     destHeight: _this.data.size * 2,
      //     canvasId: _this.data.draw + 'bg',
      //     success: function (res) {
      //       _this.setData({
      //         url: res.tempFilePath
      //       });
      //       console.log(res.tempFilePath)
      //     },
      //     fail: function (res) {
      //       console.log(res)
      //     }
      //   }, _this)
      // },500)
      _this.drawCircle(el, rpx, w, _this.data.step, true); //绘制 彩色圆环

      //  debugger
      setTimeout(function () {
        wx.canvasToTempFilePath(
          {
            x: 0,
            y: 0,
            width: _this.data.size * 2,
            height: _this.data.size * 2,
            destWidth: _this.data.size * 2,
            destHeight: _this.data.size * 2,
            canvasId: _this.data.draw,
            success: function (res) {
              _this.setData({
                url1: res.tempFilePath,
                show: "none",
              });
              console.log(res.tempFilePath);
            },
            fail: function (res) {
              console.log(res);
              _this.drawCircle(el, rpx, w, _this.data.step, true); //绘制 彩色圆环
              setTimeout(function () {
                wx.canvasToTempFilePath(
                  {
                    x: 0,
                    y: 0,
                    width: _this.data.size * 2,
                    height: _this.data.size * 2,
                    destWidth: _this.data.size * 2,
                    destHeight: _this.data.size * 2,
                    canvasId: _this.data.draw,
                    success: function (res) {
                      _this.setData({
                        url1: res.tempFilePath,
                        show: "none",
                      });
                      console.log(res.tempFilePath);
                    },
                    fail: function (res) {
                      console.log(res);
                    },
                  },
                  _this
                );
              }, 500);
            },
          },
          _this
        );
      }, 1000);
    },
  },
});
View Code

 

posted @ 2021-05-18 14:39  还有什么值得拥有  阅读(324)  评论(0编辑  收藏  举报