微信小程序簡易的截圖工具

最近在做微信小程序,要做一個截圖的功能,所以就做了一個簡易的截圖功能。廢話不說,看功能實現(xiàn)吧(UI什么的沒有專業(yè)的切圖人員提供,自己隨意寫的,有點丑)。

js代碼

Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    showImage: {
      url: '',
      height: '',
      width: ''
    },
    imgMaxWidth: '',
    imgMaxHeight: '',
    showBtn: true,
    x: 40,
    y: 40,
    scale: 1,
    moveViewWidth: '',
    moveViewHeight: '',
    lastImage: ''
  },

  /**
   * 圖片加載獲取圖片參數(shù)
   */
  onImageLoad: function (e) {
    this.data.showImage.height = e.detail.height;
    this.data.showImage.width = e.detail.width;
    // let bili = e.detail.width / e.detail.height
    // if (bili > this.data.moveViewWidth / this.data.moveViewHeight) {
    //   wx.showToast({
    //     title: '請選擇正確比例的圖片',
    //   })
    //   this.setData({ showBtn: true })
    //   return;
    // }

    this.data.showImage.width = this.data.imgMaxWidth;
    this.data.showImage.height = e.detail.height * this.data.imgMaxWidth / e.detail.width

    this.setData({ showImage: this.data.showImage })
    var context = wx.createCanvasContext('canvasOne');
    context.drawImage(this.data.showImage.url, 0, 0, this.data.showImage.width, this.data.showImage.height)
    context.draw()
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let hh = res.windowHeight
        this.setData({
          imgMaxWidth: ww * 0.9
        });
        this.setData({ imgMaxHeight: hh })
        this.setData({ moveViewWidth: ww * 0.9 })
        this.setData({ moveViewHeight: this.data.moveViewWidth * 450 / 345 })
      }
    })
  },
  /**
   * 截圖完成將指定canvas區(qū)域生成圖片
   */
  jieDone: function () {
    var that = this
    wx.canvasToTempFilePath({
      x: that.data.x,
      y: that.data.y,
      width: that.data.moveViewWidth * that.data.scale,
      height: that.data.moveViewHeight * that.data.scale,
      destWidth: that.data.moveViewWidth * that.data.scale,
      destHeight: that.data.moveViewHeight * that.data.scale,
      canvasId: 'canvasOne',
      success: function (res) {
        that.data.showImage.url = res.tempFilePath
        that.data.showImage.height = that.data.moveViewWidth * that.data.scale
        that.data.showImage.width = that.data.moveViewHeight * that.data.scale
        that.setData({ showImage: that.data.showImage })
        that.setData({ showBtn: true })
        that.setData({ lastImage: res.tempFilePath })
        console.log(that.data.moveViewHeight, that.data.moveViewWidth)
        console.log(that.data.showImage.height, that.data.showImage.width)
      },
      fail(res) {
        wx.hideLoading()
        wx.showModal({
          title: '截取失敗',
          content: res.errMsg
        })
        console.log("fail res:")
        console.log(res)
      }
    })
  },
  cancelImg: function () {
    this.setData({ showBtn: true })
  },
  selectTap() {
    var that = this
    wx.chooseImage({
      count: 1, // 默認9
      sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
      sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
      success(res) {
        const tempFilePath = res.tempFilePaths[0]
        that.data.showImage.url = tempFilePath
        that.setData({ showImage: that.data.showImage })
        that.setData({ showBtn: false })
      }
    })
  },
  onChange: function (e) {
    console.log('')
    console.log(e.detail)
    this.setData({ x: e.detail.x })
    this.setData({ y: e.detail.y })
  },
  onScale: function (e) {
    this.setData({ scale: e.detail.scale })
  }
})

wxml代碼

<!--pages/screenshot/screenshot.wxml-->
<view wx:if="{{showBtn}}" class="btn-w">
  <view class="btn" bindtap='selectTap'>選擇照片</view>
  <image src="{{lastImage}}" onImageLoad="onImageLoad" style="width:{{moveViewWidth}}px;height:{{moveViewHeight}}px"></image>
</view>
<view class="wrapper" wx:if="{{!showBtn}}">
  <image src="{{showImage.url}}" bindload="onImageLoad" style="width:{{showImage.width}}px;height:{{showImage.height}}px"></image>
  <view class="canvas-wrapper">
    <canvas canvas-id="canvasOne" class="canvas-one" style="width:{{showImage.width}}px;height:{{showImage.height}}px"></canvas>
  </view>
  <movable-area class="move-area">
    <movable-view class='move-view' x="{{x}}" y="{{y}}" direction="all" bindchange="onChange" bindscale="onScale" style="width:{{moveViewWidth}}px;height:{{moveViewHeight}}px;"></movable-view>
  </movable-area>
  <view class="bottom-w">
    <view bindtap='cancelImg'>取消</view>
    <view bindtap='jieDone'>完成</view>
  </view>
</view>

wxss代碼

/* pages/screenshot/screenshot.wxss */
.btn-w{
  text-align: center;
}
.btn{
  width:100%;
  border-radius: 12rpx;
  height:100rpx;
  line-height: 100rpx;
  background: linear-gradient(to right,yellow,green);
  text-align: center;
  font-size:30rpx;
}
.wrapper{
  position:relative;
  width:100%;
  height:93vh;
  background-color:rgba(0, 0, 0, .7);
  overflow: hidden;
}
.wrapper image{
  position:absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
  z-index:5;
  margin-top:40rpx;
}
.canvas-one{
  position:absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
  width:90%;
  margin:0 auto;
  vertical-align: middle;
}
.canvas-wrapper{
  position:relative; 
  width:100%;
  margin-left:750rpx;
  height:93vh;
}
.move-area{
  width:90%;
  height:100vh;
  position:absolute;
  top:0;
  left:50%;
  transform: translateX(-50%);
  z-index: 6;
}
.move-view{
  position:absolute;
  border:3rpx solid #fff;
  z-index:7;
}
.bottom-w{
  position:fixed;
  width:100%;
  bottom:0;
  height:7vh;
  line-height: 7vh;
  text-align: center;
  color:#fff;
  background-color:#000;
  font-size:30rpx;
  z-index: 10;
}
.bottom-w view{
  float:left;
  width:50%;
  text-align: center;
}

就這樣完成,功能相當(dāng)簡易,有時間繼續(xù)優(yōu)化擴展,啦啦啦啦?。ㄒ?guī)則就是用來打破的)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容