微信小程序 關於【canvas.drawImage 】完全顯示圖片問題

問題描述

問題產生

對於微信小程序,canvas處理過程中,dramImage默認圖片引用是有殘缺的

導入初始項目

打開鏈接(原官網例子),瀏覽器喚醒微信開發這工具,打開連接之前需要下載好微信開發者工具,如已安裝則直接喚起,沒有則會提示下載

目的

通過對canvas繪圖過程的修改,或者其樣式的修改,達到完全顯示,並自適應不同機型的目的

解決方案

準備工作

  Page({
   data:{
     imgSrc: '', // 需要處理圖片地址
     imgW: '', // canvas 寬度
     imgH: '', // canvas 高度
     byclear: 1 // 比例,這裏將iphon6- 375像素設置爲1標準,以便在自適應上的轉換
   },
   onReady() {
    var that = this
    // 根據屏幕的寬度計算標準比例值。這裏講375作爲標準值
    wx.getSystemInfo({
      success: function(res) {
        let byclear = res.screenWidth / 375
        that.setData({
          byclear
        })
      },
    })
   },
   openAndDraw() { // 選擇圖片
    var that = this
    wx.chooseImage({
      success: (res) => {
        that.setData({
          imgSrc: res.tempFilePaths[0],
          res
        })
      }
    })
   },
   checkwh(e) {
    // 處理邏輯
   }
  })

獲取選擇目標圖片的寬高度~

默認canvas 是無法獲取圖片的高度的,再者小程序裏面沒有 new Image()這個方法,只能通過標籤組件image間接獲取,所以我們需要在wxml中插入一個隱藏的標籤image,隱藏方法我們設置display:none 或者hidden就可以了,注意不要wx:if, wx:if 不會觸發bindload事件。

  <image src="{{imgSrc}}" bindload='checkwh' mode='widthFix' hidden/>
  <canvas canvas-id="canvasIn" class="canvas"></canvas>

在方法checkwh裏面即可獲取到圖片寬高

  checkwh(e){
     // 實際寬度 e.detail.width 高度 e.detail.height
     let whsrc = e.detail.height / e.detail.width
     // 計算高寬,需要處理圖片寬度小於屏幕寬度的時候 對應的canvas比例
     
  }

canvas.scale 方案

dramImage 繪圖方法,我們可以通過對畫布的放大縮小scale來完整繪製,繼續在checkwh中進行處理.scale縮放比例很簡單,我們只要計算出屏幕與圖片的實際比例,對應縮小就可。即:375 * byclear / e.detail.width 這裏要帶上自適應比例,當然對於圖片寬度小於屏幕的我們不做縮放處理

  checkwh(e){
     // 實際寬度 e.detail.width 高度 e.detail.height
    let whsrc = e.detail.height / e.detail.width
     // 計算高寬,需要處理圖片寬度大於屏幕寬度的時候 對應的canvas比例
    let res = this.data.res 
    let byclear = this.data.byclear
    const ctx = wx.createCanvasContext('canvasIn', this);
    // 對畫布進行縮放,注意scale兩個參數保持一致,即縮放比例都是一樣的。保證寬高比一致
    if (e.detail.width > 375 * byclear) ctx.scale(375 * byclear / e.detail.width, 375 * byclear / e.detail.width);
    ctx.drawImage(res.tempFilePaths[0], 0, 0, e.detail.width, e.detail.height)
    ctx.draw()
    // 後續操作
  }

上面我們已經完整的將圖片繪製到canvas中了,還不夠,下面我們將設置設置canvas寬高大小,已達到完全展示

  <canvas canvas-id="canvasIn" class="canvas" style="width:{{imgW}}rpx;height:{{imgH}}rpx;margin:0 auto;">
  </canvas>

微信自適應單位是rpx,對於iphone 6 ,375px = 750rpx => 1px = 2rpx; 其他型號計算是帶上比例byclear即可,然後圖片小於屏幕寬度,不做處理,checkwh後續代碼
因此:

  checkwh(e){
    // 前面代碼...
      this.setData({
        imgW: e.detail.width > 375 ? 750 : e.detail.width * 2 / byclear,
        imgH: e.detail.width > 375 ? 750 * whsrc : e.detail.height * 2 / byclear
      })
  }

canvas 縮放 zoom 方案

zoom方案對比scale方案,比較好的地方在於,不用計算canvas的大小,也不用縮放比例,直接將原圖的寬高設置成canvas的寬高,然後,通過zoom對canvas進行縮放,直接放代碼額,這裏的縮放比例,即爲 圖片寬度 / 750注意這裏不需要比例計算,css樣式會自動進行樣式比率計算
關鍵wxml代碼

   <canvas canvas-id="canvasIn" class="canvas" style="width:{{imgW}}rpx;height:{{imgH}}rpx;margin:0 auto;zoom:{{imgW > 750 ? 750 / imgW : 1}}"></canvas>

關鍵js代碼

  checkwh(e){
    var vhsrc = e.detail.height / e.detail.width
    let res = this.data.res
    let byclear = this.data.byclear
    const ctx = wx.createCanvasContext('canvasIn', this);
    ctx.drawImage(res.tempFilePaths[0], 0, 0, e.detail.width, e.detail.height)
    ctx.draw()
    this.setData({
      imgW: e.detail.width * 2 / byclear,
      imgH: e.detail.height * 2 / byclear
    })
  },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章