MarkBody.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. theFinalHeight = store.currentTask.sliceConfig
  97. .map((v) => v.h)
  98. .reduce((acc, v) => (acc += v));
  99. let accumTopHeight = 0;
  100. let accumBottomHeight = 0;
  101. for (const sliceConfig of store.currentTask.sliceConfig) {
  102. accumBottomHeight += sliceConfig.h;
  103. const url = filters.toCompleteUrl(
  104. store.currentTask.sliceUrls[sliceConfig.i - 1]
  105. );
  106. const image = await loadImage(url);
  107. const div = (container.value as unknown) as HTMLDivElement;
  108. maxSliceWidth = Math.max(
  109. ...store.currentTask.sliceConfig.map((v) => v.w)
  110. );
  111. const canvas = document.createElement("canvas");
  112. // canvas.width = sliceConfig.w;
  113. canvas.width = Math.max(sliceConfig.w, maxSliceWidth);
  114. canvas.height = sliceConfig.h;
  115. const ctx = canvas.getContext("2d");
  116. // drawImage 画图软件透明色
  117. ctx?.drawImage(
  118. image,
  119. sliceConfig.x,
  120. sliceConfig.y,
  121. sliceConfig.w,
  122. sliceConfig.h,
  123. 0,
  124. 0,
  125. sliceConfig.w,
  126. sliceConfig.h
  127. );
  128. // console.log(image, canvas.height, sliceConfig, ctx);
  129. // console.log(canvas.toDataURL());
  130. const thisImageTrackList = markResult.trackList.filter(
  131. (v) => v.offsetIndex === sliceConfig.i
  132. );
  133. const dataUrl = canvas.toDataURL();
  134. const sliceImage = new Image();
  135. sliceImage.src = dataUrl;
  136. // sliceConfig.x + sliceConfig.w
  137. sliceImagesWithTrackList.push({
  138. url: dataUrl,
  139. indexInSliceUrls: sliceConfig.i,
  140. // 通过positionY来定位是第几张slice的还原,并过滤出相应的track
  141. trackList: thisImageTrackList.filter(
  142. (t) =>
  143. t.positionY >= accumTopHeight / theFinalHeight &&
  144. t.positionY < accumBottomHeight / theFinalHeight
  145. ),
  146. originalImage: image,
  147. sliceImage,
  148. dx: sliceConfig.x,
  149. dy: sliceConfig.y,
  150. accumTopHeight,
  151. effectiveWidth: sliceConfig.w,
  152. });
  153. accumTopHeight = accumBottomHeight;
  154. }
  155. } else {
  156. const images = [];
  157. for (const url of store.currentTask.sliceUrls) {
  158. const image = await loadImage(filters.toCompleteUrl(url));
  159. images.push(image);
  160. }
  161. // TODO: add loading
  162. const newConfig = (store.setting.splitConfig
  163. .map((v, index, ary) =>
  164. index % 2 === 0 ? [v, ary[index + 1]] : false
  165. )
  166. .filter((v) => v) as unknown) as Array<[number, number]>;
  167. const maxSplitConfig = Math.max(...store.setting.splitConfig);
  168. maxSliceWidth =
  169. Math.max(...images.map((v) => v.naturalWidth)) * maxSplitConfig;
  170. theFinalHeight =
  171. newConfig.length *
  172. images.reduce((acc, v) => (acc += v.naturalHeight), 0);
  173. let accumTopHeight = 0;
  174. let accumBottomHeight = 0;
  175. for (const url of store.currentTask.sliceUrls) {
  176. const completeUrl = filters.toCompleteUrl(url);
  177. for (const config of newConfig) {
  178. const image = await loadImage(completeUrl);
  179. accumBottomHeight += image.naturalHeight;
  180. const div = (container.value as unknown) as HTMLDivElement;
  181. const width = image.naturalWidth * (config[1] - config[0]);
  182. const canvas = document.createElement("canvas");
  183. canvas.width = Math.max(width, maxSliceWidth);
  184. canvas.height = image.naturalHeight;
  185. const ctx = canvas.getContext("2d");
  186. // drawImage 画图软件透明色
  187. ctx?.drawImage(
  188. image,
  189. image.naturalWidth * config[0],
  190. 0,
  191. image.naturalWidth * config[1],
  192. image.naturalHeight,
  193. 0,
  194. 0,
  195. image.naturalWidth * config[1],
  196. image.naturalHeight
  197. );
  198. // console.log(image, canvas.height, sliceConfig, ctx);
  199. // console.log(canvas.toDataURL());
  200. const thisImageTrackList = markResult.trackList.filter(
  201. (t) =>
  202. t.offsetIndex === store.currentTask.sliceUrls.indexOf(url) + 1
  203. );
  204. const dataUrl = canvas.toDataURL();
  205. const sliceImage = new Image();
  206. sliceImage.src = dataUrl;
  207. sliceImagesWithTrackList.push({
  208. url: canvas.toDataURL(),
  209. indexInSliceUrls: store.currentTask.sliceUrls.indexOf(url) + 1,
  210. trackList: thisImageTrackList.filter(
  211. (t) =>
  212. t.positionY >= accumTopHeight / theFinalHeight &&
  213. t.positionY < accumBottomHeight / theFinalHeight
  214. ),
  215. originalImage: image,
  216. sliceImage,
  217. dx: image.naturalWidth * config[0],
  218. dy: 0,
  219. accumTopHeight,
  220. effectiveWidth: image.naturalWidth * config[1],
  221. });
  222. accumTopHeight = accumBottomHeight;
  223. }
  224. }
  225. }
  226. };
  227. watchEffect(renderPaperAndMark);
  228. // const reRenderPaperAndMark = () => {
  229. // // const markResult = findCurrentTaskMarkResult();
  230. // // const thisImageTrackList = markResult.trackList.filter(
  231. // // (v) => v.offsetIndex === sliceConfig.i
  232. // // );
  233. // sliceImagesWithTrackList;
  234. // };
  235. // watch(() => store.markResults, reRenderPaperAndMark, { deep: true });
  236. const answerPaperScale = computed(() => {
  237. // 放大、缩小不影响页面之前的滚动条定位
  238. let percentWidth = 0;
  239. let percentTop = 0;
  240. if (document.querySelector(".mark-body-container")) {
  241. const container = document.querySelector(
  242. ".mark-body-container"
  243. ) as HTMLDivElement;
  244. const scrollLeft = container.scrollLeft;
  245. const scrollTop = container.scrollTop;
  246. const scrollWidth = container.scrollWidth;
  247. const scrollHeight = container.scrollHeight;
  248. percentWidth = scrollLeft / scrollWidth;
  249. percentTop = scrollTop / scrollHeight;
  250. }
  251. setTimeout(() => {
  252. if (document.querySelector(".mark-body-container")) {
  253. const container = document.querySelector(
  254. ".mark-body-container"
  255. ) as HTMLDivElement;
  256. const scrollWidth = container.scrollWidth;
  257. const scrollHeight = container.scrollHeight;
  258. container.scrollTo({
  259. left: scrollWidth * percentWidth,
  260. top: scrollHeight * percentTop,
  261. });
  262. }
  263. }, 10);
  264. const scale = store.setting.uiSetting["answer.paper.scale"] || 1;
  265. return scale * 100 + "%";
  266. });
  267. const makeMark = (event: MouseEvent, item: SliceImage) => {
  268. // console.log(item);
  269. const target = event.target as HTMLImageElement;
  270. const track = {} as Track;
  271. // TODO: choose question first
  272. track.mainNumber = 4;
  273. track.subNumber = "1";
  274. track.number = 0;
  275. track.score = 2;
  276. track.offsetIndex = item.indexInSliceUrls;
  277. track.offsetX = Math.round(
  278. event.offsetX * (target.naturalWidth / target.width) + item.dx
  279. );
  280. track.offsetY = Math.round(
  281. event.offsetY * (target.naturalHeight / target.height) + item.dy
  282. );
  283. track.positionX = (track.offsetX - item.dx) / maxSliceWidth;
  284. track.positionY =
  285. (track.offsetY - item.dy + item.accumTopHeight) / theFinalHeight;
  286. // console.log(
  287. // track,
  288. // item.originalImage.naturalWidth,
  289. // item.originalImage.naturalHeight,
  290. // target.naturalWidth,
  291. // target.width,
  292. // track.offsetX,
  293. // item.effectiveWidth + item.dx
  294. // );
  295. if (track.offsetX > item.effectiveWidth + item.dx) {
  296. console.log("不在有效宽度内,轨迹不生效");
  297. return;
  298. }
  299. const markResult = findCurrentTaskMarkResult();
  300. // console.log("makemark markresult", markResult);
  301. if (markResult) {
  302. markResult.trackList = [...markResult.trackList, track];
  303. }
  304. // sliceImagesWithTrackList.find(s => s.indexInSliceUrls === item.indexInSliceUrls)
  305. item.trackList.push(track);
  306. };
  307. return {
  308. container,
  309. store,
  310. sliceImagesWithTrackList,
  311. answerPaperScale,
  312. makeMark,
  313. };
  314. },
  315. });
  316. </script>
  317. <style scoped>
  318. .mark-body-container {
  319. height: calc(100vh - 41px);
  320. overflow: scroll;
  321. background-size: 8px 8px;
  322. background-image: linear-gradient(to right, #e7e7e7 4px, transparent 4px),
  323. linear-gradient(to bottom, transparent 4px, #e7e7e7 4px);
  324. }
  325. .mark-body-container img {
  326. width: 100%;
  327. }
  328. .single-image-container {
  329. position: relative;
  330. }
  331. </style>