微信小程序图片选择区域裁剪实现方法
本篇文章主要介绍了微信小程序图片选择区域屏裁剪实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文介绍了微信小程序图片选择区域屏裁剪实现方法,分享给大家。具体如下:
效果图
HTML代码
<view class="index_all_box"> <view class="imgCut_header"> <view class="imgCut_header_l" bindtap='okCutImg'>开始裁剪</view> <view class="imgCut_header_m" bindtap='clickUpImg'>点击上传图片</view> <view class="imgCut_header_r" bindtap='okBtn'>点击确认</view> </view> <!-- 选择裁剪模式 --> <view class="selectCutMode" wx:if='{{alreay}}'> <view class="selectCutMode_in {{cutType?'selectCutMode_in_act':''}}" bindtap='etcType'> 等屏裁剪 </view> <view class="selectCutMode_in {{!cutType?'selectCutMode_in_act':''}}" bindtap='areaType'> 区域裁剪 </view> </view> <view class="areaSelct_box" wx:if='{{!cutType && alreay}}'> <slider bindchange="areaChange" min="50" max="100" show-value value='{{propor}}'/> </view> <view class="cutImg_box" wx:if='{{!prienFlag}}'> <view class="cutImg_box_t"> <image src="{{cutImgUrl}}" mode='widthFix'></image> </view> <view class="clickCutImg_txt" bindtap='againBtn'>重新裁剪</view> </view> <view class="allCavans" wx:if='{{prienFlag}}' style='width: {{canvasW}}px;height: {{canvasH}}px' > <canvas class='canvasSty' style='width: {{canvasW}}px;height: {{canvasH}}px' canvas-id='cutImg' disable-scroll='true' bindtouchmove='canvasMove'></canvas> <view class="allCavans_inbg" style='width: {{canvasW}}px;height:{{canvasH}}px; background: url({{img}});background-size: 100% 100%'></view> </view> </view>
CSS代码
.imgCut_header{ padding: 30rpx; display: flex; justify-content: space-between; align-items: center; background: #000; color: #fff; font-size: 24rpx; } .allCavans{ margin: 20rpx auto; position: relative; } .canvasSty{ position: absolute; } .cutImg_box{ width: 100%; border-bottom: 2rpx #f98700 solid; padding-bottom: 20rpx; } .cutImg_box .cutImg_box_t{ width: 90%; margin: 20rpx auto; } .cutImg_box image{ width: 100%; } .cutImg_box .cutImg_box_b{ margin-top: 20rpx; width: 80%; height: 80rpx; line-height: 80rpx; background: #f98700; color: #fff; border-radius: 10rpx; text-align: center; margin:0rpx auto; } .selectCutMode{ background: #fff; display: flex; justify-content: space-between; align-items: center; } .selectCutMode .selectCutMode_in{ width: 100%; text-align: center; background: #fff; color: #f98700; font-size: 24rpx; padding: 20rpx; } .selectCutMode .selectCutMode_in_act{ background: #f98700; color: #fff; padding: 20rpx; } .areaSelct_box{ width: 100%; display: flex; align-items: center; height: 50rpx; justify-content: center; margin-top: 20rpx; } .areaSelct_box slider{ width: 80%; } .cutImg_box .clickCutImg_txt{ width: 100%; text-align: center; height: 50rpx; font-size: 24rpx; line-height: 50rpx; color: #999; }
JS代码部分
初始加载带入上一个页面带过来的参数路径
onLoad: function (options) { var that = this; const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) var aa = 'https://pintuanqu.oss-cn-hangzhou.aliyuncs.com/Uploads/Picture/goodsShow/20171201/5a2125fc86566.png'<br /> //获取当前屏幕宽度 var phoneW = Number(util.nowPhoneWH()[0]*90)/100; var cutH = 150; wx.getImageInfo({ src: aa, success: function (res) { var w = phoneW; var h = (phoneW/Number(res.width))*Number(res.height) ctx.save() ctx.drawImage(aa, 0, 0, w, h) ctx.restore() ctx.setFillStyle('red') ctx.fillRect(0, 0, phoneW, cutH) ctx.draw() that.setData({ canvasW:w, canvasH:h, img:aa, cutH:cutH }) } }) },
确定选择区域开始裁剪
// 点击确认裁剪图片 okCutImg:function(){ var that = this; var canvasW = that.data.canvasW; var canvasH = that.data.canvasH; var nowCutW = that.data.cutType?canvasW:that.data.nowCutW; var nowCutH = that.data.cutType?that.data.cutH:that.data.nowCutH; var cutX = that.data.cutX; var cutY = that.data.cutY; const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(1) ctx.drawImage(that.data.img, 0, 0, canvasW, canvasH) ctx.draw() wx.canvasToTempFilePath({ x: cutX, y: cutY, width: nowCutW, height: nowCutH, destWidth: nowCutW, destHeight: nowCutH, canvasId: 'cutImg', success: function(res) { var aa = res.tempFilePath that.setData({ cutImgUrl:aa, prienFlag:false, alreay:false }) } }) },
红框根据手指移动方法
// 点击红框开始移动 canvasMove:function(e){ var that = this; var canvasW = that.data.canvasW; var canvasH = that.data.canvasH; var nowCutW = that.data.cutType?canvasW:that.data.nowCutW; var nowCutH = that.data.cutType?that.data.cutH:that.data.nowCutH var touches = e.touches[0]; var x = touches.x; var y = touches.y-(Number(nowCutH)/2); that.data.cutType?x=0:x=x-(Number(nowCutW)/2); that.setData({ cutX:x, cutY:y }) const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) ctx.drawImage(that.data.img, 0, 0, canvasW, canvasH) ctx.setFillStyle('red') ctx.fillRect(x, y, nowCutW, nowCutH) ctx.draw() },
上方两个选择裁剪方式的按钮
等屏裁剪
//等屏裁剪 etcType:function(){ var that = this; var propor = 100; var canvasW = that.data.canvasW; var canvasH = that.data.canvasH; var cutH = that.data.cutH; var nowCutW = (Number(canvasW)*propor)/100 var nowCutH = (Number(cutH)*propor)/100 const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) ctx.drawImage(that.data.img, 0, 0, canvasW, canvasH) ctx.setFillStyle('red') ctx.fillRect(0, 0, nowCutW, nowCutH) ctx.draw() that.setData({ nowCutW:nowCutW, nowCutH:nowCutH, cutType:true }) },
局域裁剪
areaType:function(){ var that = this; var propor = that.data.propor; var canvasW = that.data.canvasW; var canvasH = that.data.canvasH; var cutH = that.data.cutH; var nowCutW = (Number(canvasW)*propor)/100 var nowCutH = (Number(cutH)*propor)/100 const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) ctx.drawImage(that.data.img, 0, 0, canvasW, canvasH) ctx.setFillStyle('red') ctx.fillRect(0,0, nowCutW, nowCutH) ctx.draw() that.setData({ nowCutW:nowCutW, nowCutH:nowCutH, cutType:false }) },
局域裁剪上方的滑动选择红框根据宽度等比例缩放
areaChange:function(e){ var that = this; var propor = e.detail.value; var canvasW = that.data.canvasW; var canvasH = that.data.canvasH; var cutH = that.data.cutH; var nowCutW = (Number(canvasW)*propor)/100 var nowCutH = (Number(cutH)*propor)/100 const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) ctx.drawImage(that.data.img, 0, 0, canvasW, canvasH) ctx.setFillStyle('red') ctx.fillRect(that.data.cutX||0, that.data.cutY||0,nowCutW, nowCutH) ctx.draw() that.setData({ nowCutW:nowCutW, nowCutH:nowCutH, propor:propor }) },
重新裁剪回到初始选择图片的页面
// 重新裁剪 againBtn:function(){ var that = this; var data = that.data this.setData({ prienFlag:true, alreay:true }) const ctx = wx.createCanvasContext('cutImg'); ctx.setGlobalAlpha(0.4) ctx.drawImage(data.img, 0, 0, data.canvasW, data.canvasH) ctx.setFillStyle('red') ctx.fillRect(that.data.cutX||0, that.data.cutY||0, data.nowCutW||data.canvasW, data.nowCutH||data.cutH) ctx.draw() },
现在IOS还有个坑就是裁剪不了,官方正在修复不知道什么时候好
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是微信小程序图片选择区域裁剪实现方法的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

闲鱼官方微信小程序悄然上线,在小程序中可以发布闲置与买家/卖家私信交流、查看个人资料及订单、搜索物品等,有用好奇闲鱼微信小程序叫什么,现在快来看一下。闲鱼微信小程序叫什么答案:闲鱼,闲置交易二手买卖估价回收。1、在小程序中可以发布闲置、与买家/卖家私信交流、查看个人资料及订单、搜索指定物品等功能;2、在小程序的页面中有首页、附近、发闲置、消息、我的5项功能;3、想要使用的话必要要开通微信支付才可以购买;

有些用户发现图片上的某些东西是歪的,没办法直接框选裁剪,想知道有没有什么方法可以把图片上的东西摆正呢?其实这个操作对于ps高手来说非常简单,这里小编给ps小白用户讲讲photoshop把歪的图片裁剪成正的,这个方法很容易操作,希望能帮到大家。 ps裁剪倾斜照片教程 1、打开Photoshop,鼠标移至左侧的裁剪工具,然后点鼠标右键,选择“透视剪切工具”。 2、选择需要摆正的图片,确定好四个点。 3、然后按下回车键,即可成功地摆正。 4、这样一来照片上的东西就变正了,而

微信小程序实现图片上传功能随着移动互联网的发展,微信小程序已经成为了人们生活中不可或缺的一部分。微信小程序不仅提供了丰富的应用场景,还支持开发者自定义功能,其中包括图片上传功能。本文将介绍如何在微信小程序中实现图片上传功能,并提供具体的代码示例。一、前期准备工作在开始编写代码之前,我们需要先下载并安装微信开发者工具,并注册成为微信开发者。同时,还需要了解微信

实现微信小程序中的下拉菜单效果,需要具体代码示例随着移动互联网的普及,微信小程序成为了互联网开发的重要一环,越来越多的人开始关注和使用微信小程序。微信小程序的开发相比传统的APP开发更加简便快捷,但也需要掌握一定的开发技巧。在微信小程序的开发中,下拉菜单是一个常见的UI组件,实现了更好的用户操作体验。本文将详细介绍如何在微信小程序中实现下拉菜单效果,并提供具

实现微信小程序中的图片滤镜效果随着社交媒体应用的流行,人们越来越喜欢在照片中应用滤镜效果,以增强照片的艺术效果和吸引力。在微信小程序中也可以实现图片滤镜效果,为用户提供更多有趣和创造性的照片编辑功能。本文将介绍如何在微信小程序中实现图片滤镜效果,并提供具体的代码示例。首先,我们需要在微信小程序中使用canvas组件来加载和编辑图片。canvas组件可以在页面

使用微信小程序实现轮播图切换效果微信小程序是一种轻量级的应用程序,具有简单、高效的开发和使用特点。在微信小程序中,实现轮播图切换效果是常见的需求。本文将介绍如何使用微信小程序实现轮播图切换效果,并给出具体的代码示例。首先,在微信小程序的页面文件中,添加一个轮播图组件。例如,可以使用<swiper>标签来实现轮播图的切换效果。在该组件中,可以通过b

闲鱼官方微信小程序已经悄然上线,它为用户提供了一个便捷的平台,让你可以轻松地发布和交易闲置物品。在小程序中,你可以与买家或卖家进行私信交流,查看个人资料和订单,以及搜索你想要的物品。那么闲鱼在微信小程序中究竟叫什么呢,这篇教程攻略将为您详细介绍,想要了解的用户们快来跟着本文继续阅读吧!闲鱼微信小程序叫什么答案:闲鱼,闲置交易二手买卖估价回收。1、在小程序中可以发布闲置、与买家/卖家私信交流、查看个人资料及订单、搜索指定物品等功能;2、在小程序的页面中有首页、附近、发闲置、消息、我的5项功能;3、

如何使用PHP开发微信小程序的二手交易功能?微信小程序作为一种热门的移动应用开发平台,被越来越多的开发者所使用。在微信小程序中,二手交易是一种常见的功能需求。本文将介绍如何使用PHP开发微信小程序的二手交易功能,并提供具体的代码示例。一、准备工作在开始开发前,需要先确保已具备以下条件:微信小程序的开发环境已搭建完成,包括注册小程序的AppID,并在小程序后台
