index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <script setup lang="ts">
  2. import { ref, nextTick, getCurrentInstance } from "vue";
  3. import Cropper from "/@/components/ReCropper";
  4. import img from "./picture.jpeg";
  5. const instance = getCurrentInstance();
  6. let info = ref<object>(null);
  7. let cropperImg = ref<string>("");
  8. const onCropper = (): void => {
  9. nextTick(() => {
  10. instance.refs.refCropper.cropper.getCroppedCanvas().toBlob(blob => {
  11. let fileReader: FileReader = new FileReader();
  12. fileReader.onloadend = (e: ProgressEvent) => {
  13. // @ts-ignore
  14. cropperImg.value = e.target.result;
  15. info.value = instance.refs.refCropper.cropper.getData();
  16. };
  17. fileReader.readAsDataURL(blob);
  18. }, "image/jpeg");
  19. });
  20. };
  21. </script>
  22. <template>
  23. <div style="margin: 10px">
  24. <div class="cropper-container">
  25. <Cropper ref="refCropper" :width="'40vw'" :src="img" />
  26. <img :src="cropperImg" class="croppered" v-if="cropperImg" />
  27. </div>
  28. <el-button type="primary" @click="onCropper">裁剪</el-button>
  29. <p v-if="cropperImg">裁剪后图片信息:{{ info }}</p>
  30. </div>
  31. </template>
  32. <style scoped>
  33. .cropper-container {
  34. display: flex;
  35. justify-content: space-between;
  36. align-items: center;
  37. }
  38. .el-button {
  39. margin-top: 10px;
  40. }
  41. .croppered {
  42. display: block;
  43. width: 45%;
  44. height: 360px;
  45. }
  46. </style>