index.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <PageWrapper title="图片裁剪示例" contentBackground>
  3. <div class="cropper-container">
  4. <CropperImage
  5. ref="refCropper"
  6. src="https://fengyuanchen.github.io/cropperjs/images/picture.jpg"
  7. @cropperedInfo="cropperedInfo"
  8. style="width: 40%"
  9. />
  10. <a-button type="primary" @click="onCropper"> 裁剪 </a-button>
  11. <img :src="cropperImg" class="croppered" v-if="cropperImg" />
  12. </div>
  13. <p v-if="cropperImg">裁剪后图片信息:{{ info }}</p>
  14. </PageWrapper>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref, onBeforeMount, getCurrentInstance } from 'vue';
  18. import { PageWrapper } from '/@/components/Page';
  19. import { CropperImage } from '/@/components/Cropper';
  20. import img from '/@/assets/images/header.jpg';
  21. export default defineComponent({
  22. components: {
  23. PageWrapper,
  24. CropperImage,
  25. },
  26. setup() {
  27. let vm: any;
  28. let info = ref('');
  29. let cropperImg = ref('');
  30. const onCropper = (): void => {
  31. vm.refs.refCropper.croppered();
  32. };
  33. onBeforeMount(() => {
  34. vm = getCurrentInstance();
  35. });
  36. function cropperedInfo({ imgBase64, imgInfo }) {
  37. info.value = imgInfo;
  38. cropperImg.value = imgBase64;
  39. }
  40. return {
  41. img,
  42. info,
  43. cropperImg,
  44. onCropper,
  45. cropperedInfo,
  46. };
  47. },
  48. });
  49. </script>
  50. <style scoped>
  51. .cropper-container {
  52. display: flex;
  53. justify-content: space-around;
  54. align-items: center;
  55. }
  56. .croppered {
  57. width: 50%;
  58. height: 100%;
  59. }
  60. </style>