RecognizeImage.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div ref="arbitrateImgRef" class="arbitrate-img" @scroll="onImgScroll">
  3. <img src="@/assets/imgs/paper.jpg" alt="扫描结果" @load="onImgLoad" />
  4. <!-- <img :src="imgSrc" alt="扫描结果" @load="onImgLoad" /> -->
  5. </div>
  6. <div ref="imgThumbRef" class="arbitrate-img-thumb">
  7. <img src="@/assets/imgs/paper.jpg" alt="扫描结果" />
  8. <!-- <img :src="imgSrc" alt="扫描结果" /> -->
  9. <div
  10. class="arbitrate-img-area"
  11. v-ele-move-directive.prevent.stop="{
  12. moveStart: moveAreaStart,
  13. moveElement: moveArea,
  14. moveStop: moveAreaStop,
  15. }"
  16. :style="areaStyle"
  17. ></div>
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { computed, ref, reactive } from "vue";
  22. import { vEleMoveDirective } from "@/directives/eleMove";
  23. import { RecognizeArbitrateTaskDetail } from "@/ap/types/recognizeCheck";
  24. import { onMounted } from "vue";
  25. defineOptions({
  26. name: "RecognizeImage",
  27. });
  28. const props = defineProps<{
  29. imgSrc: string;
  30. recogData: RecognizeArbitrateTaskDetail[];
  31. }>();
  32. const arbitrateImgRef = ref();
  33. // img
  34. function updateImgAreaSize() {
  35. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  36. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  37. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  38. const areaRate = Math.min(imgBoxDom.clientHeight / imgDom.clientHeight, 1);
  39. const areaHeight = clientHeight * areaRate;
  40. areaSize.height = areaHeight;
  41. }
  42. function onImgLoad() {
  43. updateImgAreaSize();
  44. }
  45. let areaIsMoving = false;
  46. function onImgScroll(e: Event) {
  47. if (areaIsMoving) {
  48. e.preventDefault();
  49. return;
  50. }
  51. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  52. const scrollTop = imgBoxDom.scrollTop;
  53. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  54. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  55. areaSize.top = (clientHeight * scrollTop) / imgDom.clientHeight;
  56. }
  57. // img-thumb area
  58. const imgThumbRef = ref();
  59. const areaSize = reactive({
  60. height: 40,
  61. top: 0,
  62. });
  63. const areaOriginTop = ref(0);
  64. const areaStyle = computed(() => {
  65. return { height: `${areaSize.height}px`, top: `${areaSize.top}px` };
  66. });
  67. interface MovePos {
  68. left: number;
  69. top: number;
  70. }
  71. function moveAreaStart() {
  72. areaOriginTop.value = areaSize.top;
  73. areaIsMoving = false;
  74. }
  75. function moveArea(pos: MovePos) {
  76. areaIsMoving = true;
  77. const areaTop = pos.top + areaOriginTop.value;
  78. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  79. areaSize.top = Math.min(areaTop, clientHeight - areaSize.height);
  80. areaSize.top = Math.max(0, areaSize.top);
  81. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  82. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  83. imgBoxDom.scrollTop = (imgDom.clientHeight * areaSize.top) / clientHeight;
  84. }
  85. function moveAreaStop(pos: MovePos) {
  86. moveArea(pos);
  87. areaIsMoving = false;
  88. }
  89. </script>