首页 > 编程语言 >原生小程序使用we-cropper实现照片裁剪

原生小程序使用we-cropper实现照片裁剪

时间:2023-02-17 09:11:24浏览次数:55  
标签:原生 200 const 裁剪 width cropper wx


      <import src="../we-cropper/we-cropper.wxml" />  <-- 引入 -->
        
      <template is="we-cropper" data="{{...cropperOpt}}" />
      <view class="cropperbg">
        <template is="we-cropper" data="{{...cropperOpt}}" />
        <view class="flex-wrap rowcenter mgt-15">
          <view class="operation_icon">
            <image src="/package/image/icon_cancel.png" bindtap="cancel_sel" />
            <image src="/package/image/icon_camera.png" bindtap="getCropperImage" />
          </view>

          <!-- <van-button
            type="info"
            bindtap="chooseimg"
            size="large"
            custom-style="width:90vw;border-radius:20rpx;height:80rpx"
          >选择图片
          </van-button>

          <van-button
            type="info"
            bindtap="getCropperImage"
            size="large"
            custom-style="width:90vw;border-radius:20rpx;height:80rpx"
          >保存
          </van-button> -->
        </view>
      </view>

import WeCropper from '../we-cropper/we-cropper'
const device = wx.getSystemInfoSync() // 获取设备信息
const width = device.windowWidth // 示例为一个与屏幕等宽的正方形裁剪框
const height = width
onLoad(option) {
  const { cropperOpt } = this.data
  this.cropper = new WeCropper(cropperOpt)
    .on('ready', (ctx) => {
    console.log(`wecropper is ready for work!`)
  })
    .on('beforeImageLoad', (ctx) => {
    wx.showToast({
      title: '上传中',
      icon: 'loading',
      duration: 20000,
    })
  })
    .on('imageLoad', (ctx) => {
    wx.hideToast()
  })
},
  
touchStart(e) {
  this.cropper.touchStart(e)
},
touchMove(e) {
  this.cropper.touchMove(e)
},
touchEnd(e) {
  this.cropper.touchEnd(e)
},
// 选择
selImg() {
  let that = this
  wx.chooseImage({
    count: 1,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success(res) {
      const tempFilePaths = res.tempFilePaths[0]
      that.cropper.pushOrign(tempFilePaths)
    },
  })
},
data:{
    cropperOpt: {
      id: 'cropper', // 用于手势操作的canvas组件标识符
      targetId: 'targetCropper', // 用于用于生成截图的canvas组件标识符
      pixelRatio: device.pixelRatio, // 传入设备像素比
      width, // 画布宽度
      height, // 画布高度
      scale: 2.5, // 最大缩放倍数
      zoom: 8, // 缩放系数
      cut: {
        x: (width - 200) / 2, // 裁剪框x轴起点
        y: (width - 200) / 2, // 裁剪框y轴期起点
        width: 200, // 裁剪框宽度
        height: 200, // 裁剪框高度
      },
    },
  }

官方文档
https://we-plugin.github.io/we-cropper/#/

标签:原生,200,const,裁剪,width,cropper,wx
From: https://www.cnblogs.com/newBugs/p/17128921.html

相关文章

  • 原生 js 渐变图例
    https://codepen.io/hihust-knighterrant/pen/YzOPLgB渐变色图例<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"c......
  • 技研智联云原生容器化平台实践
    作者简介:郑建林,现任深圳市技研智联科技有限公司架构师,技术负责人。多年物联网及金融行业经验,对云计算、区块链、大数据等领域有较深入研究及应用。现主要从事PaaS平台建......
  • 2023云原生开发者真实现状:K8s居容器榜首,Docker冲顶技术热词,微服务热度不减
    在数字化转型浪潮下,企业上云已成为企业和政府的普遍共识,云原生开发者迎来了最好的时代。从云原生概念提出后的爆炸式增长,到近年来进入稳定期,开发者也逐渐在各个技术领域中......
  • 云原生-k8s-云原生开源K8s系统,面向大规模生产集群
    11月4日,在腾讯数字生态大会上,腾讯宣布了云原生领域一项重磅开源进展——K8s多集群管理项目Clusternet正式开源。Clusternet由腾讯联合多点生活、QQ音乐、富途证券、......
  • 分布式云原生平台Kurator v0.2.0正式发布!一键构建分布式云原生平台
    摘要:北京时间2023年2月9日,Kurator正式发布v0.2.0版本。本文分享自华为云社区《分布式云原生平台Kuratorv0.2.0正式发布!一键构建分布式云原生平台》,作者:Kurator团队。......
  • 原生的js分页
    functionpagePagings(option){lethtmlStr='';console.log(option.current,option.total,option.btnNums);//当前页letcurrent=option.current|......
  • 云原生
      IaaS(Infrastructureasaservice–基础设施即服务):用户可以在云服务提供商提供的基础设施上部署和运行任何软件,包括操作系统和应用软件。用户没有权限管理和访问......
  • 原生js贼强轮播图
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><styletype="text/css">*{padding:0;margin:0;}......
  • 云原生周刊 | 开源领导者应该如何应对碎片化挑战?
    LinuxFundation发布了一份关于开源开发中的碎片化问题的报告《实现全球协作:开源领导者如何应对碎片化挑战》,该报告由华为在美国的研发部门Futurewei赞助。报告指出,虽然......
  • 云原生时代顶流消息中间件Apache Pulsar部署实操-上
    @目录安装运行时Java版本推荐LocallyStandalone集群启动验证部署分布式集群部署说明初始化集群元数据部署BookKeeper部署BrokerAdmin客户端和验证TieredStorage(层级存储......