2021-03-29 22:48:13 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<PageWrapper title="图片裁剪示例" contentBackground>
|
2021-04-17 22:39:05 +08:00
|
|
|
|
<div class="container">
|
|
|
|
|
|
<div class="cropper-container">
|
|
|
|
|
|
<CropperImage
|
|
|
|
|
|
ref="refCropper"
|
|
|
|
|
|
src="https://fengyuanchen.github.io/cropperjs/images/picture.jpg"
|
|
|
|
|
|
@cropperedInfo="cropperedInfo"
|
|
|
|
|
|
style="width: 40vw"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2021-04-10 21:19:28 +08:00
|
|
|
|
<a-button type="primary" @click="onCropper"> 裁剪 </a-button>
|
|
|
|
|
|
<img :src="cropperImg" class="croppered" v-if="cropperImg" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p v-if="cropperImg">裁剪后图片信息:{{ info }}</p>
|
2021-03-29 22:48:13 +08:00
|
|
|
|
</PageWrapper>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts">
|
2021-04-17 22:39:05 +08:00
|
|
|
|
import { defineComponent, ref, unref } from 'vue';
|
2021-03-29 22:48:13 +08:00
|
|
|
|
import { PageWrapper } from '/@/components/Page';
|
|
|
|
|
|
import { CropperImage } from '/@/components/Cropper';
|
|
|
|
|
|
import img from '/@/assets/images/header.jpg';
|
2021-04-17 22:39:05 +08:00
|
|
|
|
import { templateRef } from '@vueuse/core';
|
|
|
|
|
|
|
2021-03-29 22:48:13 +08:00
|
|
|
|
export default defineComponent({
|
|
|
|
|
|
components: {
|
|
|
|
|
|
PageWrapper,
|
|
|
|
|
|
CropperImage,
|
|
|
|
|
|
},
|
|
|
|
|
|
setup() {
|
2021-04-12 23:02:48 +08:00
|
|
|
|
let info = ref('');
|
|
|
|
|
|
let cropperImg = ref('');
|
2021-04-17 22:39:05 +08:00
|
|
|
|
const refCropper = templateRef<HTMLElement | null>('refCropper', null);
|
2021-04-10 21:19:28 +08:00
|
|
|
|
|
|
|
|
|
|
const onCropper = (): void => {
|
2021-04-17 22:39:05 +08:00
|
|
|
|
unref(refCropper).croppered();
|
2021-04-12 23:02:48 +08:00
|
|
|
|
};
|
2021-04-10 21:19:28 +08:00
|
|
|
|
|
2021-04-12 23:02:48 +08:00
|
|
|
|
function cropperedInfo({ imgBase64, imgInfo }) {
|
|
|
|
|
|
info.value = imgInfo;
|
|
|
|
|
|
cropperImg.value = imgBase64;
|
|
|
|
|
|
}
|
2021-04-10 21:19:28 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
img,
|
|
|
|
|
|
info,
|
|
|
|
|
|
cropperImg,
|
|
|
|
|
|
onCropper,
|
2021-04-12 23:02:48 +08:00
|
|
|
|
cropperedInfo,
|
2021-04-10 21:19:28 +08:00
|
|
|
|
};
|
2021-03-29 22:48:13 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
</script>
|
2021-04-10 21:19:28 +08:00
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2021-04-17 22:39:05 +08:00
|
|
|
|
.container {
|
2021-04-12 23:02:48 +08:00
|
|
|
|
display: flex;
|
2021-04-17 22:39:05 +08:00
|
|
|
|
width: 100vw;
|
2021-04-12 23:02:48 +08:00
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-17 22:39:05 +08:00
|
|
|
|
.cropper-container {
|
|
|
|
|
|
width: 40vw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-12 23:02:48 +08:00
|
|
|
|
.croppered {
|
2021-04-17 22:39:05 +08:00
|
|
|
|
height: 360px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p {
|
|
|
|
|
|
margin: 10px;
|
2021-04-12 23:02:48 +08:00
|
|
|
|
}
|
2021-04-10 21:19:28 +08:00
|
|
|
|
</style>
|