MarkDrawTrack.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <transition-group name="track-score" tag="div">
  3. <template v-for="track in trackList">
  4. <div
  5. v-if="store.shouldShowTrack && (doubleTrack || !track.isByMultMark)"
  6. :key="`key-${track.mainNumber}-${track.subNumber}-${track.offsetY}-${track.offsetX}`"
  7. class="score-container no-event"
  8. :class="[focusedTrack(track) && 'score-animation']"
  9. :style="computeTopAndLeft(track)"
  10. >
  11. <span
  12. :id="`a-${track.mainNumber}-${track.subNumber}-${track.offsetY}-${track.offsetX}`"
  13. class="tw-m-auto"
  14. >
  15. {{ track.unanswered ? "空" : track.score }}
  16. </span>
  17. </div>
  18. </template>
  19. </transition-group>
  20. <template
  21. v-for="tag in specialTagList"
  22. :key="`${tag.offsetX}_${tag.offsetY}`"
  23. >
  24. <img
  25. v-if="tag.tagType === 'TEXT'"
  26. class="special-text"
  27. :style="computeSpecialTextStyle(tag)"
  28. :src="getSpecialTextImg(tag)"
  29. />
  30. <div
  31. v-else-if="tag.tagType === 'LINE'"
  32. class="special-line"
  33. :style="computeSpecialLineStyle(tag)"
  34. ></div>
  35. <div
  36. v-else-if="tag.tagType === 'CIRCLE'"
  37. class="special-circle"
  38. :style="computeSpecialCircleStyle(tag)"
  39. @click="circleTagClickHandle"
  40. ></div>
  41. <div
  42. v-else-if="tag.tagType === 'RIGHT'"
  43. :class="['score-container', 'no-event']"
  44. :style="computeTopAndLeft(tag)"
  45. >
  46. <span class="tw-m-auto">
  47. <CheckOutlined />
  48. </span>
  49. </div>
  50. <div
  51. v-else
  52. :class="['score-container', 'no-event']"
  53. :style="computeTopAndLeft(tag)"
  54. >
  55. <span class="tw-m-auto">
  56. {{ tag.tagName }}
  57. </span>
  58. </div>
  59. </template>
  60. </template>
  61. <script setup lang="ts">
  62. import type { SpecialTag, Track } from "@/types";
  63. import { toRefs, watch, nextTick, computed } from "vue";
  64. import { store } from "@/store/app";
  65. import { message } from "ant-design-vue";
  66. import { CheckOutlined } from "@ant-design/icons-vue";
  67. import { useRoute } from "vue-router";
  68. const route = useRoute();
  69. const doubleTrack = computed(() => {
  70. return !!store.setting?.doubleTrack;
  71. });
  72. const props = defineProps<{
  73. trackList: Array<Track>;
  74. specialTagList: Array<SpecialTag>;
  75. sliceImageWidth: number;
  76. sliceImageHeight: number;
  77. dx: number;
  78. dy: number;
  79. }>();
  80. const emit = defineEmits(["click-specialtag"]);
  81. const { trackList } = toRefs(props);
  82. const computeSpecialLineStyle = (track: SpecialTag) => {
  83. // {"tagName":"{\"len\":241.9842519685039}","tagType":"LINE","offsetIndex":2,"offsetX":324.8193228048039,"offsetY":759.8560783391572,"positionX":0.06189180773871382,"positionY":-0.01054037309709878}
  84. const tagProp = JSON.parse(track.tagName);
  85. const topInsideSlice = track.offsetY - props.dy;
  86. const leftInsideSlice = track.offsetX - props.dx;
  87. const topInsideSliceRatio = topInsideSlice / props.sliceImageHeight;
  88. const leftInsideSliceRatio = leftInsideSlice / props.sliceImageWidth;
  89. return {
  90. top: topInsideSliceRatio * 100 + "%",
  91. left: leftInsideSliceRatio * 100 + "%",
  92. width: (100 * tagProp.len) / props.sliceImageWidth + "%",
  93. position: "absolute",
  94. borderTop: `1px solid ${track.color || "red"}`,
  95. zIndex: 9,
  96. };
  97. };
  98. const computeSpecialCircleStyle = (track: SpecialTag) => {
  99. // {"tagName":"{\"len\":241.9842519685039}","tagType":"LINE","offsetIndex":2,"offsetX":324.8193228048039,"offsetY":759.8560783391572,"positionX":0.06189180773871382,"positionY":-0.01054037309709878}
  100. const tagProp = JSON.parse(track.tagName);
  101. const topInsideSlice = track.offsetY - props.dy;
  102. const leftInsideSlice = track.offsetX - props.dx;
  103. const topInsideSliceRatio = topInsideSlice / props.sliceImageHeight;
  104. const leftInsideSliceRatio = leftInsideSlice / props.sliceImageWidth;
  105. return {
  106. top: topInsideSliceRatio * 100 + "%",
  107. left: leftInsideSliceRatio * 100 + "%",
  108. width: (100 * tagProp.width) / props.sliceImageWidth + "%",
  109. height: (100 * tagProp.height) / props.sliceImageHeight + "%",
  110. position: "absolute",
  111. border: `1px solid ${track.color || "red"}`,
  112. borderRadius: "50%",
  113. zIndex: 9,
  114. };
  115. };
  116. const computeSpecialTextStyle = (track: SpecialTag) => {
  117. // {"tagName":"{\"len\":241.9842519685039}","tagType":"LINE","offsetIndex":2,"offsetX":324.8193228048039,"offsetY":759.8560783391572,"positionX":0.06189180773871382,"positionY":-0.01054037309709878}
  118. const tagProp = JSON.parse(track.tagName);
  119. const topInsideSlice = track.offsetY - props.dy;
  120. const leftInsideSlice = track.offsetX - props.dx;
  121. const topInsideSliceRatio = topInsideSlice / props.sliceImageHeight;
  122. const leftInsideSliceRatio = leftInsideSlice / props.sliceImageWidth;
  123. return {
  124. top: topInsideSliceRatio * 100 + "%",
  125. left: leftInsideSliceRatio * 100 + "%",
  126. width: (100 * tagProp.width) / props.sliceImageWidth + "%",
  127. height: (100 * tagProp.height) / props.sliceImageHeight + "%",
  128. position: "absolute",
  129. marginTop: "-10px",
  130. zIndex: 9,
  131. };
  132. };
  133. const getSpecialTextImg = (track: SpecialTag) => {
  134. const tagProp = JSON.parse(track.tagName);
  135. const canvas = document.createElement("canvas");
  136. canvas.width = tagProp.width * 2;
  137. canvas.height = tagProp.height * 2;
  138. const ctx = canvas.getContext("2d");
  139. ctx.fillStyle = track.color || "#ff0000";
  140. ctx.font = "normal 40px 黑体";
  141. const contents = tagProp.content.split("\n");
  142. const lineHeight = 48;
  143. let y = 42;
  144. const x = 0;
  145. // 每次回车换行时会多一个换行符,需要剔除
  146. let conts = [];
  147. let lastContIsEmpty = false;
  148. contents.forEach((cont) => {
  149. if (cont.trim()) {
  150. conts.push(cont);
  151. lastContIsEmpty = false;
  152. } else {
  153. if (lastContIsEmpty) return;
  154. conts.push(cont);
  155. lastContIsEmpty = true;
  156. }
  157. });
  158. conts.forEach((cont, index) => {
  159. if (!cont) {
  160. y += lineHeight;
  161. return;
  162. }
  163. let arrText = cont.split("");
  164. let line = "";
  165. for (let n = 0; n < arrText.length; n++) {
  166. let textLine = line + arrText[n];
  167. const metrics = ctx.measureText(textLine);
  168. if (metrics.width > canvas.width && n > 0) {
  169. ctx.fillText(line, x, y);
  170. line = arrText[n];
  171. y += lineHeight;
  172. } else {
  173. line = textLine;
  174. }
  175. }
  176. ctx.fillText(line, x, y);
  177. y += lineHeight;
  178. });
  179. return canvas.toDataURL();
  180. };
  181. const computeTopAndLeft = (track: Track | SpecialTag) => {
  182. const topInsideSlice = track.offsetY - props.dy;
  183. const leftInsideSlice = track.offsetX - props.dx;
  184. const topInsideSliceRatio = topInsideSlice / props.sliceImageHeight;
  185. const leftInsideSliceRatio = leftInsideSlice / props.sliceImageWidth;
  186. if (
  187. topInsideSliceRatio < 0 ||
  188. topInsideSliceRatio > 1 ||
  189. leftInsideSliceRatio < 0 ||
  190. leftInsideSliceRatio > 1
  191. ) {
  192. /** 解决message提示死循环的问题 */
  193. void nextTick(() => {
  194. void message.error("轨迹坐标有误,可能是图片被修改过,请联系管理员!");
  195. });
  196. }
  197. return {
  198. color: route.path === "/arbitrate" ? "green" : track.color || "red",
  199. top: topInsideSliceRatio * 100 + "%",
  200. left: leftInsideSliceRatio * 100 + "%",
  201. "font-size":
  202. (store.setting.uiSetting["score.fontSize.scale"] || 1) *
  203. store.setting.uiSetting["answer.paper.scale"] *
  204. 2.2 +
  205. "em",
  206. };
  207. };
  208. const hasMember = (track: any) => {
  209. return (
  210. // (store.getMarkStatus === "正评" || store.getMarkStatus === "试评") &&
  211. store.focusTracks.find((item: any) => {
  212. return (
  213. item.mainNumber == track.mainNumber && item.subNumber == track.subNumber
  214. );
  215. })
  216. );
  217. };
  218. const focusedTrack = (track: Track) => {
  219. return store.focusTracks.includes(track) || hasMember(track);
  220. };
  221. const circleTagClickHandle = (event: MouseEvent) => {
  222. emit("click-specialtag", event);
  223. };
  224. watch(
  225. () => store.focusTracks,
  226. () => {
  227. if (store.focusTracks.length === 0) return;
  228. const minImageIndex = Math.min(
  229. ...store.focusTracks.map((t) => t.offsetIndex)
  230. );
  231. const minImageOffsetY = Math.min(
  232. ...store.focusTracks
  233. .filter((t) => t.offsetIndex === minImageIndex)
  234. .map((t) => t.offsetY)
  235. );
  236. const topTrack = store.focusTracks.find(
  237. (t) => t.offsetIndex === minImageIndex && t.offsetY === minImageOffsetY
  238. );
  239. if (topTrack) {
  240. let allHeaderTracks = store.currentTask.questionList
  241. .map((item: any) => item.headerTrack || [])
  242. .flat();
  243. console.log("allHeaderTracks", allHeaderTracks);
  244. let find = allHeaderTracks.find(
  245. (item: any) =>
  246. item.mainNumber == topTrack.mainNumber &&
  247. item.subNumber == topTrack.subNumber
  248. );
  249. document
  250. .getElementById(
  251. `a-${topTrack.mainNumber}-${topTrack.subNumber}-${
  252. find?.offsetY || topTrack.offsetY
  253. }-${find?.offsetX || topTrack.offsetX}`
  254. )
  255. ?.scrollIntoView({ behavior: "smooth" });
  256. }
  257. },
  258. {
  259. deep: true,
  260. }
  261. );
  262. </script>
  263. <style scoped>
  264. .score-container {
  265. position: absolute;
  266. z-index: 9;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. place-content: center;
  271. /* color: red; */
  272. /* to center score */
  273. width: 200px;
  274. height: 200px;
  275. margin-top: -100px;
  276. margin-left: -100px;
  277. }
  278. .score-container.special-container {
  279. width: auto;
  280. height: auto;
  281. margin-top: 0;
  282. margin-left: 0;
  283. max-width: 200px;
  284. max-height: 200px;
  285. transform: translate(-50%, -50%);
  286. }
  287. .score-container.no-event {
  288. /* to click through div */
  289. pointer-events: none;
  290. }
  291. .score-animation {
  292. animation: 0.5s ease-in-out 0s infinite alternate change_size;
  293. }
  294. @keyframes change_size {
  295. from {
  296. transform: scale(1, 1);
  297. }
  298. to {
  299. transform: scale(2, 2);
  300. }
  301. }
  302. .track-score-enter-active {
  303. transition: opacity 0.3s ease;
  304. }
  305. .track-score-leave-active {
  306. transition: opacity 0.6s ease;
  307. }
  308. .track-score-enter-from,
  309. .track-score-leave-to {
  310. opacity: 0;
  311. }
  312. </style>