微信小程序实现裁剪图片大小

 更新时间:2022年5月23日 16:58  点击:657

本文实例为大家分享了微信小程序实现裁剪图片大小的具体代码,供大家参考,具体内容如下

效果图

.wxml

<button bindtap="uploadtap">上传图片</button>
<image style="width: 100%;" mode="widthFix" src="{{canfile_image}}"></image>
<canvas canvas-id="myCanvas" id="myCanvas" style="width:100%;height:70vh;"></canvas>
<view class="canvas_model" wx:if="{{canvas_arr.show}}" catchtouchmove="setTouchMove">
  <view class="canvas_view" style="width: {{canvas_arr.width}}px;height: {{canvas_arr.height}}px;"
  bindtouchstart="handletouchstart" bindtouchmove="handletouchmove">
    <image style="width:100%;height:100%;" src="{{canvas_arr.src}}"></image>
    <view style="{{color_status?'background: rgba(0, 0, 0, 0.6);':''}}">
      <view class="canvas_horn" style="height:{{cutting.height}}px;top:{{canvas_y}}px;">
        <view></view>
        <view></view>
        <view></view>
        <view></view>
        <view></view>
        <view></view>
        <image style="width: {{canvas_arr.width}}px;height: {{canvas_arr.height}}px;top:-{{canvas_y}}px;" src="{{canvas_arr.src}}"></image>
      </view>
    </view>
  </view>
  <view class="canvas_bar" wx:if="{{cutting.show}}">
    <view bindtap="color_tap">×</view>
    <view class="bar_tab {{color_status?'tab_cation':''}}" bindtap="color_tap" data-type="1">
      <view>浅色</view>
      <view>深色</view>
      <view></view>
    </view>
    <view bindtap="ationimg">√</view>
  </view>
</view>

.wxss

 .canvas_model{width: 100%;height: 100vh;position: fixed;left: 0;top: 0;background: rgba(0, 0, 0, 0.6);z-index: 10;
display:flex;justify-content: center;padding-top: 10vh;animation:model 0.3s;}
@keyframes model{
  from { opacity: 0;transform: scale(0.5); }
  to { opacity:1;transform: scale(1); }
}
.canvas_view{float: left;position: relative;}
.canvas_view>view{width: 100%;height: 100%;position: absolute;left: 0;top: 0;background: rgba(255, 255, 255, 0.4);transition: all 0.3s;}
.canvas_horn{position: absolute;left: 0;width: 100%;overflow: hidden;}
.canvas_horn>view{width: 40rpx;height: 40rpx;position: absolute;z-index: 1;}
.canvas_horn>image{position: absolute;left: 0;top: 0;}
.canvas_horn>view:nth-child(1){border-left: 2px solid #fff;border-top: 2px solid #fff;left: 0;top: 0;}
.canvas_horn>view:nth-child(2){border-right: 2px solid #fff;border-top: 2px solid #fff;right: 0;top: 0;}
.canvas_horn>view:nth-child(3){border-left: 2px solid #fff;border-bottom:2px solid #fff;left: 0;bottom: 0;}
.canvas_horn>view:nth-child(4){border-right: 2px solid #fff;border-bottom: 2px solid #fff;right: 0;bottom: 0;}
.canvas_horn>view:nth-child(5){width: 60rpx;height: 2px;background: #fff;top: 0;left: 0;right: 0;margin: 0 auto;}
.canvas_horn>view:nth-child(6){width: 60rpx;height: 2px;background: #fff;bottom: 0;left: 0;right: 0;margin: 0 auto;}
.canvas_bar{width: 100%;height: 14vh;display: flex;align-items: center;justify-content: space-between;background: #fff;
position: absolute;left: 0;bottom: 0;animation:bartion 0.5s;}
@keyframes bartion{
  from { bottom: -14vh; }
  to { bottom: 0; }
}
.canvas_bar>view:nth-child(1),.canvas_bar>view:nth-child(3){width: 160rpx;height: 100%;display: flex;align-items: center;
justify-content: center;font-size: 44rpx;font-weight: 700;}
.bar_tab{width: 300rpx;display: flex;height: 60rpx;border-radius: 10rpx;border: 1px solid #f4f4f4;line-height: 60rpx;
position: relative;font-size: 24rpx;color: #333;}
.bar_tab>view{width: 50%;height: 100%;text-align: center;position: relative;z-index: 1;transition: all 0.3s;}
.bar_tab>view:nth-child(1){color: #fff;}
.bar_tab>view:nth-child(3){position: absolute;left: 0;top: 0;background: #0081ff;border-radius: 10rpx;z-index: 0;}
.tab_cation>view:nth-child(1){color: #000;}
.tab_cation>view:nth-child(2){color: #fff;}
.tab_cation>view:nth-child(3){left: 50%;}
#myCanvas{position: absolute;left: 0;top: -71vh;z-index: -1;opacity: 0;}

.js

Page({
  data: {
    canvas_arr:{
      src:'',
      width:parseInt(wx.getSystemInfoSync().windowWidth * 0.9),
      height:parseInt(wx.getSystemInfoSync().windowHeight * 0.7),
      show:false
    },
    cutting:{
      height:0,
      max_y:0,
      show:false
    },
    canvas_y:0,
    color_status:false,
    canfile_image:'',
  },
  color_tap(e){
    var type = e?e.currentTarget.dataset.type:0
    if(type == 1){
      this.setData({
        color_status:!this.data.color_status
      })
    }else{
      this.data.canvas_arr.show = false
      this.data.canvas_arr.height = parseInt(wx.getSystemInfoSync().windowHeight * 0.7)
      this.data.cutting.show = false
      this.data.cutting.src = ''
      this.setData({
        canvas_arr:this.data.canvas_arr,
        cutting:this.data.cutting,
        canvas_y:0,
      })
    }
  },
  setTouchMove(e){return;},
  uploadtap(e){
    var that = this
    wx.chooseImage({
      count:1,
      success (res) {
        const tempFilePaths = res.tempFilePaths[0]
        that.data.canvas_arr.src = tempFilePaths
        wx.getImageInfo({
          src: tempFilePaths,
          success (res) {
            that.data.canvas_arr.show = true
            that.data.cutting.show = true
            that.data.cutting.height = that.data.canvas_arr.width / 2
            var height = parseInt(res.height / (res.width / that.data.canvas_arr.width))
            that.data.canvas_arr.height = height > that.data.canvas_arr.height ? that.data.canvas_arr.height : height
            that.data.cutting.max_y = that.data.canvas_arr.height - that.data.cutting.height
            that.setData({
              canvas_arr:that.data.canvas_arr
            })
            setTimeout(function () {
              that.setData({
                cutting:that.data.cutting
              })
            },500)
          }
        })
      }
    })
  },
  handletouchstart: function (e) {
    this.data.startY = e.touches[0].clientY
  },
  handletouchmove (e) {
    let currentY = e.touches[0].clientY - this.data.startY
    if(currentY > 0){
      this.setData({
        canvas_y:currentY > this.data.cutting.max_y?this.data.cutting.max_y:currentY,
      })
    }else{
      this.setData({
        canvas_y:0
      })
    }
  },
  ationimg(e){
    var that = this
    var canvas_img = wx.createCanvasContext('myCanvas')
    canvas_img.width = that.data.canvas_arr.width
    canvas_img.height = that.data.canvas_arr.height
    canvas_img.drawImage(that.data.canvas_arr.src,0,0,canvas_img.width,canvas_img.height)
    canvas_img.draw(true,(()=>{
      wx.canvasToTempFilePath({
        x: 0,
        y: that.data.canvas_y,
        width:that.data.canvas_arr.width,
        height:that.data.cutting.height,
        canvasId: 'myCanvas',
        success: function (res) {
          that.setData({
            canfile_image:res.tempFilePath
          })
          that.color_tap()
          wx.showToast({
            title: '裁剪成功~',
            icon: 'none',
            duration: 3000
          })
        }
      });
    }))
  },
})

功能主要针对轮播图片尺寸,不合适可自行修改比例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/qq_43764578/article/details/121212662

[!--infotagslink--]

相关文章

  • iOS如何将图片裁剪成圆形

    这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
  • 微信小程序实现canvas分享朋友圈海报

    这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
  • 微信小程序 页面跳转传递值几种方法详解

    这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16
  • 微信小程序手势操作之单触摸点与多触摸点

    这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
  • uniapp,微信小程序中使用 MQTT的问题

    这篇文章主要介绍了uniapp,微信小程序中使用 MQTT的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • 微信小程序自定义底部弹出框动画

    这篇文章主要介绍了微信小程序自定义底部弹出框动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-16
  • 微信小程序实现选择地址省市区三级联动

    这篇文章主要为大家详细介绍了微信小程序实现选择地址省市区三级联动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
  • 微信小程序实现点击导航条切换页面

    这篇文章主要为大家详细介绍了微信小程序实现点击导航条切换页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-19
  • 微信小程序入门之绘制时钟

    这篇文章主要为大家详细介绍了微信小程序入门之绘制时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-10-23
  • 微信小程序 富文本转文本实例详解

    这篇文章主要介绍了微信小程序 富文本转文本实例详解的相关资料,需要的朋友可以参考下...2016-10-25
  • 详解微信小程序 通过控制CSS实现view隐藏与显示

    这篇文章主要介绍了微信小程序 通过控制CSS实现view隐藏与显示的相关资料,需要的朋友可以参考下...2017-05-27
  • 微信小程序 PHP后端form表单提交实例详解

    这篇文章主要介绍了微信小程序 PHP后端form表单提交实例详解的相关资料,需要的朋友可以参考下...2017-01-16
  • 微信小程序开发之map地图实现教程

    相信大家现在都知道微信小程序吧,下面这篇文章主要给大家介绍了微信小程序开发之map地图的相关资料,分享出来供大家参考学习,文中通过示例代码介绍的非常详细,需要的朋友们下面跟着小编来一起学习学习吧。...2017-06-15
  • 微信小程序 教程之注册页面

    这篇文章主要介绍了微信小程序 注册页面的相关资料,需要的朋友可以参考下...2016-10-20