SheetViewModal.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <teleport to="body">
  3. <div v-if="store.sheetViewModal" class="dialog-container">
  4. <header ref="mouseHandler" class="tw-flex tw-place-content-between">
  5. <div class="tw-text-2xl tw-cursor-move">原图</div>
  6. <div class="tw-mx-8 tw-flex-grow">
  7. <span
  8. v-for="(u, index) in dataUrls"
  9. :key="index"
  10. @click="checkedIndex = index"
  11. class="image-index hover:tw-bg-gray-300"
  12. :class="checkedIndex === index && 'tw-bg-gray-300'"
  13. >{{ index + 1 }}</span
  14. >
  15. </div>
  16. <a-button shape="circle" @click="store.sheetViewModal = false">
  17. <template #icon><CloseOutlined /></template>
  18. </a-button>
  19. </header>
  20. <div>
  21. <div
  22. v-for="(url, index) in dataUrls"
  23. :key="index"
  24. style="display: none"
  25. :class="index === checkedIndex && 'show-image'"
  26. >
  27. <img :src="url" />
  28. </div>
  29. </div>
  30. </div>
  31. </teleport>
  32. </template>
  33. <script lang="ts">
  34. import { defineComponent, reactive, ref, watchEffect } from "vue";
  35. import { CloseOutlined } from "@ant-design/icons-vue";
  36. import { store } from "@/features/mark/store";
  37. import { loadImage } from "@/utils/utils";
  38. import { PictureSlice } from "@/types";
  39. export default defineComponent({
  40. name: "SheetViewModal",
  41. components: { CloseOutlined },
  42. props: {},
  43. emits: ["close"],
  44. setup() {
  45. const dataUrls: Array<string> = reactive([]);
  46. watchEffect(async () => {
  47. const urls =
  48. store.currentTask?.sheetUrls.map((s) => store.setting.fileServer + s) ??
  49. [];
  50. const images = [];
  51. for (const url of urls) {
  52. images.push(await loadImage(url));
  53. }
  54. const sheetConfig = store.setting.sheetConfig;
  55. for (let i = 0; i < images.length; i++) {
  56. if (sheetConfig.length === 0) {
  57. dataUrls.push(
  58. getDataUrlForSheetConfig(
  59. images[i],
  60. i % 2 === 0
  61. ? [
  62. // 通过-1来标记该用默认遮盖规则
  63. { i: -1, x: 0, y: 0, w: 0, h: 0 },
  64. ]
  65. : []
  66. )
  67. );
  68. } else {
  69. const scs = sheetConfig.filter((s) => s.i - 1 === i);
  70. dataUrls.push(getDataUrlForSheetConfig(images[i], scs));
  71. }
  72. }
  73. });
  74. const checkedIndex = ref(0);
  75. return { store, dataUrls, checkedIndex };
  76. },
  77. });
  78. export function getDataUrlForSheetConfig(
  79. image: HTMLImageElement,
  80. sliceConfigs: Array<PictureSlice>
  81. ) {
  82. const canvas = document.createElement("canvas");
  83. canvas.width = image.naturalWidth;
  84. canvas.height = image.naturalWidth;
  85. const ctx = canvas.getContext("2d");
  86. if (!ctx) {
  87. console.log('canvas.getContext("2d") error');
  88. return "null";
  89. }
  90. // drawImage 画图软件透明色
  91. ctx?.drawImage(image, 0, 0);
  92. ctx.fillStyle = "grey";
  93. for (const sc of sliceConfigs) {
  94. if (sc.i === -1) {
  95. ctx.fillRect(0, 0, image.naturalWidth / 2, image.naturalHeight / 3);
  96. } else if (sc.w === 0 && sc.h === 0) {
  97. ctx.fillRect(0, 0, image.naturalWidth, image.naturalHeight);
  98. } else {
  99. ctx.fillRect(sc.x, sc.y, sc.w, sc.h);
  100. }
  101. }
  102. const dataurl = canvas.toDataURL();
  103. return dataurl;
  104. }
  105. </script>
  106. <style scoped>
  107. .dialog-container {
  108. /* always top */
  109. z-index: 99999;
  110. position: absolute;
  111. background-color: white;
  112. top: 0;
  113. left: 0;
  114. width: 100vw;
  115. height: 100vh;
  116. }
  117. header {
  118. background-color: #eff3f6;
  119. }
  120. .image-index {
  121. display: inline-block;
  122. border: 1px solid grey;
  123. width: 25px;
  124. margin-right: 10px;
  125. margin-top: 5px;
  126. text-align: center;
  127. border-radius: 5px;
  128. cursor: pointer;
  129. }
  130. .show-image {
  131. display: block !important;
  132. }
  133. </style>