ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

小程序裁剪

2021-08-02 20:03:29  阅读:166  来源: 互联网

标签:src const 裁剪 程序 height width cropper


使用上一篇文件的cropper插件引入后

wxml

<import src="../we/we-cropper.wxml"/>
<view class="cropper-wrapper">
<template is="we-cropper" data="{{...cropperOpt}}"/>

</view>
<view class="cropper-buttons">
<button class="upload"
bindtap="uploadTap">
重新上传
</button>
<button
class="getCropperImage"
bindtap="mygetCropperImage">
确定
</button>
</view>

wxss

/* pages/cut/index.wxss */

.cropper-wrapper{
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #e5e5e5;
}

.cropper-buttons{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 150px;
  padding: 0 20rpx;
  box-sizing: border-box;
  line-height: 150px;
}

.cropper-buttons .upload, .cropper-buttons .getCropperImage{
  text-align: center;
  z-index: 999999;
  margin: 10rpx;
  background-color: #36ccf9;
}

js

const Util = require('../../../../utils/util.js');
const app = getApp()
const {
  regeneratorRuntime
} = global

import WeCropper from '../we/we-cropper.js';
const device = wx.getSystemInfoSync() // 获取设备信息
const width = device.windowWidth // 示例为一个与屏幕等宽的正方形裁剪框
const height = device.windowHeight -150


Page({
    data: {
        cropperOpt: {
            id: 'cropper', // 用于手势操作的canvas组件标识符
            targetId: 'targetCropper', // 用于用于生成截图的canvas组件标识符
            pixelRatio: device.pixelRatio, // 传入设备像素比
            width, // 画布宽度
            height, // 画布高度
            src: '',
            scale: 2.5, // 最大缩放倍数
            zoom: 8, // 缩放系数
            cut: {
                x: (width - 320) / 2, // 裁剪框x轴起点
                y: (width - 320) / 2, // 裁剪框y轴起点
                width: 320, // 裁剪框宽度
                height: 320 // tore/"裁剪框高度
            }
        }
    },
    // 页面onLoad函数中实例化WeCropper
    onl oad: function(options) {

        const {
          cropperOpt
        } = this.data;
        cropperOpt.src = options.src;
        if (cropperOpt.src) {
          this.cropper = new WeCropper(cropperOpt)
            .on('ready', (ctx) => {
              console.log(`wecropper is ready for work!`)
            })
            .on('beforeImageLoad', (ctx) => {
               wx.showToast({
                 title: '上传中',
                 icon: 'loading',
                duration: 3000
               })
             })
            .on('imageLoad', (ctx) => {
               wx.hideToast()
             })
        }
    },
    // 插件通过touchStart、touchMove、touchEnd方法来接收事件对象。
    touchStart(e) {
        this.cropper.touchStart(e)
    },
    touchMove(e) {
        this.cropper.touchMove(e)
    },
    touchEnd(e) {
        this.cropper.touchEnd(e)
    },
    // 自定义裁剪页面的布局中,可以重新选择图片
    uploadTap() {
        const self = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
            success(res) {
                const src = res.tempFilePaths[0]
                self.cropper.pushOrign(src)
              
            }
        })
    },

    // 生成图片
    mygetCropperImage(){
      var that = this;
        this.cropper.getCropperImage((tempFilePath) => {
            // tempFilePath 为裁剪后的图片临时路径
            if (tempFilePath) {
                //TODO 处理逻辑
            }else{
                console.log('获取图片地址失败,请稍后重试')
            }
        })
      }
})

 

json

{
  "navigationBarTitleText": "裁剪",
  "usingComponents": {}
}

 

标签:src,const,裁剪,程序,height,width,cropper
来源: https://www.cnblogs.com/shuihanxiao/p/15091417.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有