原生小程序使用canvas画布放置图片并下载图片

 

 

wxml代码

<view class="download_certificate">

  <!-- <view class="certificate_img"> -->

    <!-- <image src="cloud://produce-6gykelggdea632cb.7072-produce-6gykelggdea632cb-1317432646/download.png"/> -->

    <view style="width: 700rpx;height: 500rpx;">

    <canvas type="2d" id="myCanvas" style="width: 700rpx;height: 500rpx;" />

  </view>

  <!-- </view> -->

  <van-button type="danger" size="large" custom-style="margin-top:20rpx;border-radius:20rpx;" color="#DF3535" bindtap="savaImage">下载</van-button>

</view>

js代码

 onReady() {

    this.drawCanvas()

  },

 drawCanvas() {

    //返回一个对象实例,通过实例可以获取canvas

    const query = wx.createSelectorQuery()

    query.select('#myCanvas')

      .fields({ node: true, size: true })

      .exec((res) => {

        const canvas = res[0].node

        const ctx = canvas.getContext('2d')

        this.setData({

          canvasRef: canvas

        })

        //使canvas适应各种屏幕不至于大小不同

        const dpr = wx.getSystemInfoSync().pixelRatio

        canvas.width = res[0].width * dpr // 获取宽

        canvas.height = res[0].height * dpr // 获取高

        ctx.scale(dpr, dpr)

        // 到这里就可以直接绘制

        let image = canvas.createImage(); //创建iamge实例

        image.src = this.data.src; // 引入本地图片

        image.onload = function (imginfo) {

          // 获取http图片宽高

          // const {width,height} = imginfo.path[0];

          // const aspect = width / height;

          // console.log(width,height)

          // 先不用图片宽高了

          // dpr是设备像素比

          ctx.drawImage(image, 0, 0, canvas.width / dpr, canvas.height / dpr); // 背景图

        }

      })

  },

 saveImg() {

    let that=this;

    wx.showLoading({

      title: '生成中...',

    })

    wx.canvasToTempFilePath({

      canvas: that.data.canvasRef,

      x: 0,

      y: 0,

      width: that.canvasWidth * 2,

      height: that.canvasHeight * 2,

      destWidth: that.canvasWidth * 2,

      destHeight: that.canvasHeight * 2,

      success: res => {

        wx.saveImageToPhotosAlbum({

          filePath: res.tempFilePath,

          success: res => {

            // console.log(this.saveTempFilePath)

            console.log(11111)

            wx.showModal({

              title: "保存成功!",

              content: "图片已保存到本地相册",

              showCancel: false,

              success(res) {

                if (res.confirm) {

                  wx.reLaunch({

                    url: '/pages/joinU/joinU'

                  })

                }

              }

            })

          },

          fail: err => {

            console.log(err)

          }

        })

      },

      fail: err => {

        console.log(err)

      }

    })

    setTimeout(() => {

      wx.hideLoading()

    }, 1000)

  },

当用户点击了下载图片的按钮后, 触发了以下的函数:

  savaImage() {

    let that = this;

    //获取用户保存图片权限

    wx.getSetting({

     success(res){

      if (!res.authSetting['scope.writePhotosAlbum']) {

            wx.authorize({

              scope: 'scope.writePhotosAlbum',

              success(res){

                that.saveImg()

              },

              fail(){

                wx.showModal({

                  content: '请允许相册权限,拒绝将无法正常使用小程序',

                  showCancel: false,

                  success() {

                    wx.openSetting({

                      success(settingdata) {

                        if (settingdata.authSetting["scope.writePhotosAlbum"]) {

                        } else {

                          console.log("获取权限失败")

                        }

  

                      }

                    })

                  }

                })

              }

            })

        }else{

          that.saveImg()

        }

     }

    })

  },

保存图片:

 saveImg() {

    let that=this;

    wx.showLoading({

      title: '生成中...',

    })

    wx.canvasToTempFilePath({

      canvas: that.data.canvasRef,

      x: 0,

      y: 0,

      width: that.canvasWidth * 2,

      height: that.canvasHeight * 2,

      destWidth: that.canvasWidth * 2,

      destHeight: that.canvasHeight * 2,

      success: res => {

        wx.saveImageToPhotosAlbum({

          filePath: res.tempFilePath,

          success: res => {

            // console.log(this.saveTempFilePath)

            wx.showModal({

              title: "保存成功!",

              content: "图片已保存到本地相册",

              showCancel: false,

              success(res) {

                if (res.confirm) {

                  wx.reLaunch({

                    url: '/pages/joinU/joinU'

                  })

                }

              }

            })

          },

          fail: err => {

            console.log(err)

          }

        })

      },

      fail: err => {

        console.log(err)

      }

    })

    setTimeout(() => {

      wx.hideLoading()

    }, 1000)

  },

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值