123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- <template>
- <view class="u-cropper">
- <view class="u-cropper__container"
- @touchstart.prevent="handleTouchStart"
- @touchmove.prevent="handleTouchMove"
- @touchend.prevent="handleTouchEnd">
- <!-- 遮罩层和裁剪框 -->
- <view class="u-cropper__mask">
- <!-- 裁剪框边框 -->
- <view class="u-cropper__cropbox" :class="[showGrid && 'u-cropper__cropbox_grid', `u-cropper__cropbox_${shape}`]" :style="[cropBoxStyle]">
- <view v-if="shape === 'square'" class="u-cropper__edge u-cropper__edge--1"></view>
- <view v-if="shape === 'square'" class="u-cropper__edge u-cropper__edge--2"></view>
- <view v-if="shape === 'square'" class="u-cropper__edge u-cropper__edge--3"></view>
- <view v-if="shape === 'square'" class="u-cropper__edge u-cropper__edge--4"></view>
- </view>
- </view>
-
- <!-- 图片层 -->
- <image
- v-if="viewData.imageLoaded && viewData.imageSrc"
- class="u-cropper__image"
- :src="viewData.imageSrc"
- :style="[imageStyle]"
- mode="aspectFit"
- @error="onImageError"
- />
-
- </view>
-
- <!-- 工具栏 -->
- <view class="u-cropper__tools">
- <view class="u-cropper__button__cancel" @click="close">
- <text>{{ cancelText }}</text>
- </view>
- <!-- #ifdef MP-WEIXIN -->
- <view class="cropper__button__choose">
- <button
- v-if="openType == 'chooseAvatar'"
- class="u-reset-button"
- hover-class="none"
- open-type="chooseAvatar"
- @click="chooseAvatar"
- @chooseavatar="chooseAvatar"
- >
- <u-icon name="photo" size="36" color="#fff"/>
- </button>
- <u-icon v-else name="photo" size="36" color="#fff" @click="chooseImage"/>
- </view>
- <!-- #endif -->
- <!-- #ifndef MP-WEIXIN -->
- <view class="cropper__button__choose" @click="chooseImage">
- <u-icon name="photo" size="36" color="#fff"/>
- </view>
- <!-- #endif -->
-
- <view class="cropper__button__rotate" @click="rotateImage">
- <u-icon name="rotate" size="36" color="#fff"/>
- </view>
- <view class="u-cropper__button__confirm" @click="confirmCrop(false)">
- <text>{{ confirmText }}</text>
- </view>
- </view>
- <!-- Canvas层 -->
- <canvas
- <!-- #ifdef MP-WEIXIN -->
- type="2d"
- <!-- #endif -->
- :canvas-id="canvasId"
- :id="canvasId"
- class="u-cropper__canvas" :style="[canvasStyle]"></canvas>
- </view>
- </template>
- <script>
- import props from './props.js';
- import mixin from '../../libs/mixin/mixin'
- import mpMixin from '../../libs/mixin/mpMixin';
- import ImageCropper from './cropper.js';
- /**
- * Cropper 图片裁剪组件
- * @description 封装的图片裁剪组件,支持图片选择、裁剪、旋转等功能,适用于头像上传、图片编辑等场景
- * @tutorial https://uview.d3u.cn/components/cropper.html
- *
- * @property {Boolean} autoChoose 每次显示时是否自动打开选择图片(默认 false )
- * @property {String | Number} rectWidth 裁剪框宽度,单位px(默认 200 )
- * @property {String | Number} rectHeight 裁剪框高度,单位px(默认 200 )
- * @property {String | Number} width 输出图片宽度,单位px(默认 200 )
- * @property {String | Number} height 输出图片高度,单位px(默认 200 )
- * @property {String} fileType 输出图片格式,支持jpg/png,建议使用jpg减小文件大小(默认 'jpg' )
- * @property {Boolean} showGrid 是否显示网格线(默认 true )
- * @property {String} shape 输入框形状,circle-圆形,square-方形 ( 默认 'square' )
- * @event {Function} change 选择图片时触发,返回原始图片路径
- * @event {Function} open 打开裁剪弹窗时触发
- * @event {Function} close 关闭裁剪弹窗时触发
- * @event {Function} confirm 确认裁剪时触发,返回裁剪后的图片临时路径
- * @example <u-cropper @confirm="onCropperConfirm"></u-cropper>
- */
- export default {
- name: "u-cropper",
- mixins: [mpMixin, mixin, props],
- data() {
- return {
- cropper: null,
- canvasId: 'cropper' + uni.$u.guid(),
- viewData: {
- imageLoaded: false,
- imageSrc: '',
- imageData: {},
- canvasWidth: 0,
- canvasHeight: 0,
- rectWidth: 0,
- rectHeight: 0
- }
- };
- },
- computed: {
-
- // 图片样式 - 从原始数据计算
- imageStyle() {
- const imageData = this.viewData.imageData;
- if (!this.viewData.imageLoaded || !imageData || !imageData.width) return {};
-
- let duration = '0s';
- let transform = `translate3d(${imageData.x}px, ${imageData.y}px,0)`;
- if (imageData.angle && imageData.angle !== 0) {
- duration = '0.3s';
- transform += ` rotate(${imageData.angle}deg)`;
- }
- return {
- width: uni.$u.addUnit(imageData.width),
- height: uni.$u.addUnit(imageData.height),
- transform: transform,
- transitionDuration: duration
- };
- },
-
- // 裁剪框样式 - 从原始数据计算
- cropBoxStyle() {
- if (!this.viewData.canvasWidth) return {};
-
- const rectX = (this.viewData.canvasWidth - this.viewData.rectWidth) / 2;
- const rectY = (this.viewData.canvasHeight - this.viewData.rectHeight) / 2;
-
- return {
- boxShadow: '0 0 0 '+ this.viewData.canvasWidth +'px rgba(0, 0, 0, 0.5)',
- left: uni.$u.addUnit(rectX),
- top: uni.$u.addUnit(rectY),
- width: uni.$u.addUnit(this.viewData.rectWidth),
- height: uni.$u.addUnit(this.viewData.rectHeight)
- };
- },
- canvasStyle() {
- return {
- width: uni.$u.addUnit(this.width),
- height: uni.$u.addUnit(this.height)
- }
- }
- },
- mounted() {
- this.$nextTick(async ()=>{
- await uni.$u.sleep(30)
- this.initCropper();
- })
- },
- watch: {
-
- },
- // #ifdef VUE3
- emits: ['change', 'open', 'close','confirm','error'],
- // #endif
- methods: {
- // 初始化裁剪器
- async initCropper() {
- const options = {
- canvasId: this.canvasId,
- width: uni.$u.getPx(this.width),
- height: uni.$u.getPx(this.height),
- rectWidth: uni.$u.getPx(this.rectWidth),
- rectHeight: uni.$u.getPx(this.rectHeight),
- watermark: this.watermark,
- fileType: this.fileType,
- quality: this.quality,
- onUpdate: (viewData) => {
- this.viewData = viewData;
- }
- };
- // #ifdef MP-WEIXIN
- options.type = '2d';
- // #endif
- this.cropper = new ImageCropper(this, options);
- this.$emit('open');
- if(this.autoChoose) {
- this.chooseImage();
- }
- },
-
- // 加载图片
- loadImage(src) {
- if (!this.cropper) return;
- this.cropper.setImage(src).catch(err => {
- this.$emit('error', uni.$u.$t('uCropper.loadImageError'));
- });
- },
- chooseAvatar(e) {
- const path = e.detail.avatarUrl;
- if (path) {
- this.$emit('change', path);
- this.loadImage(path);
- }
- },
- // 选择图片
- chooseImage() {
- uni.chooseImage({
- count: 1,
- sizeType: ['original', 'compressed'],
- sourceType: ['album', 'camera'],
- success: (res) => {
- const imagePath = res.tempFilePaths[0];
- this.$emit('change', imagePath);
- this.loadImage(imagePath);
- },
- fail: () => {
- this.$emit('error', uni.$u.$t('uCropper.chooseImageError'));
- }
- });
- },
-
- // 图片加载失败
- onImageError(e) {
- this.$emit('error', uni.$u.$t('uCropper.loadImageError'));
- },
-
- // 旋转图片
- rotateImage() {
- if (this.cropper) {
- this.cropper.rotate(90);
- }
- },
-
- // 重置图片
- resetImage() {
- if (this.cropper) {
- this.cropper.reset();
- }
- },
-
- // 确认裁剪
- async confirmCrop() {
- if (!this.cropper || !this.viewData.imageLoaded) {
- this.$emit('error', uni.$u.t('uCropper.chooseImage'));
- return;
- }
-
- uni.showLoading({
- title: uni.$u.$t('uCropper.cropping'),
- mask: true
- });
- this.cropper.getCropperImage().then((res)=>{
- this.$emit('confirm',res);
- this.close();
- }).catch(err => {
- this.$emit('error', uni.$u.$t('uCropper.cropError'));
- }).finally(()=>{
- uni.hideLoading();
- });
- },
-
- // 关闭裁剪器
- close() {
- if (this.cropper) {
- this.cropper.destroy();
- this.cropper = null;
- }
- this.viewData = {};
- this.$emit('close');
- },
-
- // 处理触摸开始 - 兼容各平台
- handleTouchStart(e) {
- if (!this.cropper) return;
-
- // 阻止默认行为和事件冒泡
- e.preventDefault && e.preventDefault();
- e.stopPropagation && e.stopPropagation();
-
- // 兼容不同平台的触摸事件
- let touch;
- // #ifdef H5
- touch = e.touches ? e.touches[0] : e;
- // #endif
- // #ifndef H5
- touch = e.touches && e.touches[0] ? e.touches[0] : e.changedTouches && e.changedTouches[0] ? e.changedTouches[0] : e;
- // #endif
-
- const event = {
- touches: [{
- clientX: touch.clientX || touch.x || touch.pageX,
- clientY: touch.clientY || touch.y || touch.pageY
- }]
- };
-
- this.cropper.touchStart(event);
- },
-
- // 处理触摸移动 - 兼容各平台
- handleTouchMove(e) {
- if (!this.cropper) return;
-
- // 阻止默认行为和事件冒泡
- e.preventDefault && e.preventDefault();
- e.stopPropagation && e.stopPropagation();
-
- // 兼容不同平台的触摸事件
- let touch;
- // #ifdef H5
- touch = e.touches ? e.touches[0] : e;
- // #endif
- // #ifndef H5
- touch = e.touches && e.touches[0] ? e.touches[0] : e.changedTouches && e.changedTouches[0] ? e.changedTouches[0] : e;
- // #endif
-
- const event = {
- touches: [{
- clientX: touch.clientX || touch.x || touch.pageX,
- clientY: touch.clientY || touch.y || touch.pageY
- }],
- preventdefault: () => ({})
- };
-
- this.cropper.touchMove(event);
- },
-
- // 处理触摸结束 - 兼容各平台
- handleTouchEnd(e) {
- if (!this.cropper) return;
-
- // 阻止默认行为和事件冒泡
- e.preventDefault && e.preventDefault();
- e.stopPropagation && e.stopPropagation();
-
- this.cropper.touchEnd(e);
- }
- },
-
- // #ifdef VUE2
- beforeDestroy() {
- if (this.cropper) {
- this.cropper.destroy();
- this.cropper = null;
- }
- }
- // #endif
-
- // #ifdef VUE3
- beforeUnmount() {
- if (this.cropper) {
- this.cropper.destroy();
- this.cropper = null;
- }
- }
- // #endif
- };
- </script>
- <style scoped lang="scss">
- .u-cropper {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- z-index: 999;
- background: #000;
-
- &__mask {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- z-index: 2;
- }
-
- &__cropbox {
- position: absolute;
- background: transparent;
- box-sizing: border-box;
- border: 1px solid rgba(255, 255, 255, .5);
- &_grid {
- &::before{
- content: '';
- position: absolute;
- top: 33.3333%;
- height: 33.3333%;
- left: 0;
- width: 100%;
- border: 1px dashed rgba(255, 255, 255, .5);
- }
- &::after{
- content: '';
- position: absolute;
- top: 0;
- left: 33.3333%;
- width: 33.3333%;
- height: 100%;
- border: 1px dashed rgba(255, 255, 255, .5);
- }
- }
- &_circle{
- border-radius: 100%;
- overflow: hidden;
- }
- &_square{
- }
- }
-
- &__edge {
- position: absolute;
- border:3px solid #ffffff;
- width: 15px;
- height: 15px;
- &--1 {
- top: -3px;
- left: -3px;
- border-right: 0;
- border-bottom: 0;
- }
-
- &--2 {
- top: -3px;
- right: -3px;
- border-left: 0;
- border-bottom: 0;
- }
-
- &--3 {
- bottom: -3px;
- left: -3px;
- border-right: 0;
- border-top: 0;
- }
-
- &--4 {
- bottom: -3px;
- right: -3px;
- border-left: 0;
- border-top: 0;
- }
- }
-
- &__image {
- position: absolute;
- left: 0;
- top: 0;
- z-index: 1;
- display: block;
- transform-origin: center center;
- }
-
- // &__grid {
- // position: absolute;
- // top: 0;
- // left: 0;
- // right: 0;
- // bottom: 0;
-
- // &-line {
- // position: absolute;
- // background: rgba(255, 255, 255, 0.3);
-
- // &--v {
- // top: 0;
- // bottom: 0;
- // width: 1px;
-
- // &1 {
- // left: 33.33%;
- // }
-
- // &2 {
- // left: 66.67%;
- // }
- // }
-
- // &--h {
- // left: 0;
- // right: 0;
- // height: 1px;
-
- // &1 {
- // top: 33.33%;
- // }
-
- // &2 {
- // top: 66.67%;
- // }
- // }
- // }
- // }
-
- &__tools {
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- z-index: 100;
- padding: 20px;
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: space-between;
- }
- &__button{
- &__cancel{
- color:#fff;
- font-weight: 700;
- width: 70px;
- height: 40px;
- line-height: 40px;
- text-align: center;
- }
-
- &__confirm {
- color:#fff;
- font-weight: 700;
- width: 70px;
- height: 40px;
- line-height: 40px;
- background-color: $u-primary;
- border-radius: 4px;
- text-align: center;
-
- }
- }
-
- &__canvas {
- position: absolute;
- left: -9999px;
- top: -9999px;
- opacity: 0;
- }
- }
- </style>
|