MarkBody.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <div class="mark-body-container flex-auto" ref="container">
  3. <div v-if="!store.currentTask" class="text-center">暂无评卷任务</div>
  4. <div v-else :style="{ width: answerPaperScale }">
  5. <div
  6. v-for="(item, index) in sliceImagesWithTrackList"
  7. :key="index"
  8. class="single-image-container"
  9. >
  10. <img
  11. :src="item.url"
  12. @click="(event) => makeMark(event, item)"
  13. draggable="false"
  14. />
  15. <MarkDrawTrack
  16. :track-list="item.trackList"
  17. :original-image="item.originalImage"
  18. :slice-image="item.sliceImage"
  19. :dx="item.dx"
  20. :dy="item.dy"
  21. />
  22. <hr style="border: 2px solid grey" />
  23. </div>
  24. <!-- style="border: 1px solid black; background: black" -->
  25. </div>
  26. </div>
  27. </template>
  28. <script lang="ts">
  29. import { computed, defineComponent, reactive, ref, watchEffect } from "vue";
  30. import { findCurrentTaskMarkResult, store } from "./store";
  31. import filters from "@/filters";
  32. import MarkDrawTrack from "./MarkDrawTrack.vue";
  33. import { MarkResult, Track } from "@/types";
  34. interface SliceImage {
  35. url: string;
  36. indexInSliceUrls: number;
  37. trackList: Array<Track>;
  38. originalImage: HTMLImageElement;
  39. sliceImage: HTMLImageElement;
  40. dx: number;
  41. dy: number;
  42. accumTopHeight: number;
  43. effectiveWidth: number;
  44. }
  45. export default defineComponent({
  46. name: "MarkBody",
  47. components: { MarkDrawTrack },
  48. setup(props, context) {
  49. const container = ref(null);
  50. let sliceImagesWithTrackList: Array<SliceImage> = reactive([]);
  51. let maxSliceWidth = 0;
  52. let theFinalHeight = 0;
  53. const renderPaperAndMark = async () => {
  54. async function loadImage(url: string): Promise<HTMLImageElement> {
  55. return new Promise((resolve, reject) => {
  56. const image = new Image();
  57. image.setAttribute("crossorigin", "anonymous");
  58. image.src = url;
  59. image.onload = () => resolve(image);
  60. image.onerror = reject;
  61. });
  62. }
  63. if (!store.currentTask?.libraryId) {
  64. return;
  65. }
  66. // check if have MarkResult for currentTask
  67. let markResult = findCurrentTaskMarkResult();
  68. // console.log("watcheffect markResult 1", markResult, store.markResults);
  69. if (!markResult && store.currentTask) {
  70. const { libraryId, studentId } = store.currentTask;
  71. const statusValue = store.setting.statusValue;
  72. markResult = {} as MarkResult;
  73. markResult.libraryId = libraryId;
  74. markResult.studentId = studentId;
  75. markResult.statusValue = statusValue;
  76. markResult.spent = Date.now();
  77. markResult.trackList = store.currentTask.questionList.reduce(
  78. (all, c) => all.concat(c.trackList),
  79. [] as Array<Track>
  80. );
  81. store.markResults = [...store.markResults, markResult];
  82. // console.log("watcheffect markResult 2", markResult, store.markResults);
  83. }
  84. // store.markResults.splice(store.markResults.indexOf(markResult), 1);
  85. // store.markResults.push(markResult);
  86. // console.log("watcheffect markResult 3", markResult, store.markResults);
  87. // const allTrackList = findCurrentTaskMarkResult().trackList;
  88. // console.log(allTrackList);
  89. // console.log(store.markResults);
  90. // reset sliceImagesWithTrackList
  91. sliceImagesWithTrackList.splice(0);
  92. if (store.currentTask.sliceConfig?.length) {
  93. for (const url of store.currentTask.sliceUrls) {
  94. await loadImage(filters.toCompleteUrl(url));
  95. }
  96. // 必须要先加载一遍,把“选择整图”的宽高重置后,再算总高度
  97. for (const sliceConfig of store.currentTask.sliceConfig) {
  98. const url = filters.toCompleteUrl(
  99. store.currentTask.sliceUrls[sliceConfig.i - 1]
  100. );
  101. const image = await loadImage(url);
  102. if (sliceConfig.w === 0 && sliceConfig.h === 0) {
  103. // 选择整图时,w/h 为0
  104. sliceConfig.w = image.naturalWidth;
  105. sliceConfig.h = image.naturalHeight;
  106. }
  107. }
  108. theFinalHeight = store.currentTask.sliceConfig
  109. .map((v) => v.h)
  110. .reduce((acc, v) => (acc += v));
  111. let accumTopHeight = 0;
  112. let accumBottomHeight = 0;
  113. for (const sliceConfig of store.currentTask.sliceConfig) {
  114. accumBottomHeight += sliceConfig.h;
  115. const url = filters.toCompleteUrl(
  116. store.currentTask.sliceUrls[sliceConfig.i - 1]
  117. );
  118. const image = await loadImage(url);
  119. if (sliceConfig.w === 0 && sliceConfig.h === 0) {
  120. // 选择整图时,w/h 为0
  121. sliceConfig.w = image.naturalWidth;
  122. sliceConfig.h = image.naturalHeight;
  123. }
  124. const div = (container.value as unknown) as HTMLDivElement;
  125. maxSliceWidth = Math.max(
  126. ...store.currentTask.sliceConfig.map((v) => v.w)
  127. );
  128. const canvas = document.createElement("canvas");
  129. // canvas.width = sliceConfig.w;
  130. canvas.width = Math.max(sliceConfig.w, maxSliceWidth);
  131. canvas.height = sliceConfig.h;
  132. const ctx = canvas.getContext("2d");
  133. // drawImage 画图软件透明色
  134. ctx?.drawImage(
  135. image,
  136. sliceConfig.x,
  137. sliceConfig.y,
  138. sliceConfig.w,
  139. sliceConfig.h,
  140. 0,
  141. 0,
  142. sliceConfig.w,
  143. sliceConfig.h
  144. );
  145. // console.log(image, canvas.height, sliceConfig, ctx);
  146. // console.log(canvas.toDataURL());
  147. const thisImageTrackList = markResult.trackList.filter(
  148. (v) => v.offsetIndex === sliceConfig.i
  149. );
  150. const dataUrl = canvas.toDataURL();
  151. const sliceImage = new Image();
  152. sliceImage.src = dataUrl;
  153. // sliceConfig.x + sliceConfig.w
  154. sliceImagesWithTrackList.push({
  155. url: dataUrl,
  156. indexInSliceUrls: sliceConfig.i,
  157. // 通过positionY来定位是第几张slice的还原,并过滤出相应的track
  158. trackList: thisImageTrackList.filter(
  159. (t) =>
  160. t.positionY >= accumTopHeight / theFinalHeight &&
  161. t.positionY < accumBottomHeight / theFinalHeight
  162. ),
  163. originalImage: image,
  164. sliceImage,
  165. dx: sliceConfig.x,
  166. dy: sliceConfig.y,
  167. accumTopHeight,
  168. effectiveWidth: sliceConfig.w,
  169. });
  170. accumTopHeight = accumBottomHeight;
  171. }
  172. } else {
  173. const images = [];
  174. for (const url of store.currentTask.sliceUrls) {
  175. const image = await loadImage(filters.toCompleteUrl(url));
  176. images.push(image);
  177. }
  178. // TODO: add loading
  179. const newConfig = (store.setting.splitConfig
  180. .map((v, index, ary) =>
  181. index % 2 === 0 ? [v, ary[index + 1]] : false
  182. )
  183. .filter((v) => v) as unknown) as Array<[number, number]>;
  184. const maxSplitConfig = Math.max(...store.setting.splitConfig);
  185. maxSliceWidth =
  186. Math.max(...images.map((v) => v.naturalWidth)) * maxSplitConfig;
  187. theFinalHeight =
  188. newConfig.length *
  189. images.reduce((acc, v) => (acc += v.naturalHeight), 0);
  190. let accumTopHeight = 0;
  191. let accumBottomHeight = 0;
  192. for (const url of store.currentTask.sliceUrls) {
  193. const completeUrl = filters.toCompleteUrl(url);
  194. for (const config of newConfig) {
  195. const image = await loadImage(completeUrl);
  196. accumBottomHeight += image.naturalHeight;
  197. const div = (container.value as unknown) as HTMLDivElement;
  198. const width = image.naturalWidth * (config[1] - config[0]);
  199. const canvas = document.createElement("canvas");
  200. canvas.width = Math.max(width, maxSliceWidth);
  201. canvas.height = image.naturalHeight;
  202. const ctx = canvas.getContext("2d");
  203. // drawImage 画图软件透明色
  204. ctx?.drawImage(
  205. image,
  206. image.naturalWidth * config[0],
  207. 0,
  208. image.naturalWidth * config[1],
  209. image.naturalHeight,
  210. 0,
  211. 0,
  212. image.naturalWidth * config[1],
  213. image.naturalHeight
  214. );
  215. // console.log(image, canvas.height, sliceConfig, ctx);
  216. // console.log(canvas.toDataURL());
  217. const thisImageTrackList = markResult.trackList.filter(
  218. (t) =>
  219. t.offsetIndex === store.currentTask.sliceUrls.indexOf(url) + 1
  220. );
  221. const dataUrl = canvas.toDataURL();
  222. const sliceImage = new Image();
  223. sliceImage.src = dataUrl;
  224. sliceImagesWithTrackList.push({
  225. url: canvas.toDataURL(),
  226. indexInSliceUrls: store.currentTask.sliceUrls.indexOf(url) + 1,
  227. trackList: thisImageTrackList.filter(
  228. (t) =>
  229. t.positionY >= accumTopHeight / theFinalHeight &&
  230. t.positionY < accumBottomHeight / theFinalHeight
  231. ),
  232. originalImage: image,
  233. sliceImage,
  234. dx: image.naturalWidth * config[0],
  235. dy: 0,
  236. accumTopHeight,
  237. effectiveWidth: image.naturalWidth * config[1],
  238. });
  239. accumTopHeight = accumBottomHeight;
  240. }
  241. }
  242. }
  243. };
  244. watchEffect(renderPaperAndMark);
  245. // const reRenderPaperAndMark = () => {
  246. // // const markResult = findCurrentTaskMarkResult();
  247. // // const thisImageTrackList = markResult.trackList.filter(
  248. // // (v) => v.offsetIndex === sliceConfig.i
  249. // // );
  250. // sliceImagesWithTrackList;
  251. // };
  252. // watch(() => store.markResults, reRenderPaperAndMark, { deep: true });
  253. const answerPaperScale = computed(() => {
  254. // 放大、缩小不影响页面之前的滚动条定位
  255. let percentWidth = 0;
  256. let percentTop = 0;
  257. if (document.querySelector(".mark-body-container")) {
  258. const container = document.querySelector(
  259. ".mark-body-container"
  260. ) as HTMLDivElement;
  261. const scrollLeft = container.scrollLeft;
  262. const scrollTop = container.scrollTop;
  263. const scrollWidth = container.scrollWidth;
  264. const scrollHeight = container.scrollHeight;
  265. percentWidth = scrollLeft / scrollWidth;
  266. percentTop = scrollTop / scrollHeight;
  267. }
  268. setTimeout(() => {
  269. if (document.querySelector(".mark-body-container")) {
  270. const container = document.querySelector(
  271. ".mark-body-container"
  272. ) as HTMLDivElement;
  273. const scrollWidth = container.scrollWidth;
  274. const scrollHeight = container.scrollHeight;
  275. container.scrollTo({
  276. left: scrollWidth * percentWidth,
  277. top: scrollHeight * percentTop,
  278. });
  279. }
  280. }, 10);
  281. const scale = store.setting.uiSetting["answer.paper.scale"] || 1;
  282. return scale * 100 + "%";
  283. });
  284. const makeMark = (event: MouseEvent, item: SliceImage) => {
  285. // console.log(item);
  286. if (!store.currentQuestion || !store.currentScore) return;
  287. const target = event.target as HTMLImageElement;
  288. const track = {} as Track;
  289. // TODO: choose question first
  290. track.mainNumber = store.currentQuestion?.mainNumber;
  291. track.subNumber = store.currentQuestion?.subNumber;
  292. track.number = Math.round(Math.random() * 10000000);
  293. track.score = store.currentScore;
  294. track.offsetIndex = item.indexInSliceUrls;
  295. track.offsetX = Math.round(
  296. event.offsetX * (target.naturalWidth / target.width) + item.dx
  297. );
  298. track.offsetY = Math.round(
  299. event.offsetY * (target.naturalHeight / target.height) + item.dy
  300. );
  301. track.positionX = (track.offsetX - item.dx) / maxSliceWidth;
  302. track.positionY =
  303. (track.offsetY - item.dy + item.accumTopHeight) / theFinalHeight;
  304. // console.log(
  305. // track,
  306. // item.originalImage.naturalWidth,
  307. // item.originalImage.naturalHeight,
  308. // target.naturalWidth,
  309. // target.width,
  310. // track.offsetX,
  311. // item.effectiveWidth + item.dx
  312. // );
  313. if (track.offsetX > item.effectiveWidth + item.dx) {
  314. console.log("不在有效宽度内,轨迹不生效");
  315. return;
  316. }
  317. const markResult = findCurrentTaskMarkResult();
  318. // console.log("makemark markresult", markResult);
  319. if (markResult) {
  320. markResult.trackList = [...markResult.trackList, track];
  321. }
  322. // sliceImagesWithTrackList.find(s => s.indexInSliceUrls === item.indexInSliceUrls)
  323. item.trackList.push(track);
  324. };
  325. return {
  326. container,
  327. store,
  328. sliceImagesWithTrackList,
  329. answerPaperScale,
  330. makeMark,
  331. };
  332. },
  333. });
  334. </script>
  335. <style scoped>
  336. .mark-body-container {
  337. height: calc(100vh - 41px);
  338. overflow: scroll;
  339. background-size: 8px 8px;
  340. background-image: linear-gradient(to right, #e7e7e7 4px, transparent 4px),
  341. linear-gradient(to bottom, transparent 4px, #e7e7e7 4px);
  342. }
  343. .mark-body-container img {
  344. width: 100%;
  345. }
  346. .single-image-container {
  347. position: relative;
  348. }
  349. </style>