DrawingPreview.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="drawimg-preview" :style="[getAreaStyle(), getComponentStyle()]">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div
  6. class="img-box"
  7. :style="{
  8. background: image_url ? '' : '#DCDFE6',
  9. width: isMobile ? '100%' : data.image_width + 'px',
  10. height: isMobile ? mobileHeight + 'px' : data.image_height + 'px',
  11. border: '1px dotted #DCDFE6',
  12. }"
  13. >
  14. <div
  15. v-if="image_url"
  16. class="img-set"
  17. :style="{ top: `${data.imgData.top - 9}px`, left: `${data.imgData.left}px` }"
  18. >
  19. <img
  20. :src="image_url"
  21. draggable="false"
  22. :alt="convertText('背景图')"
  23. :style="{
  24. width: isMobile ? '100%' : `${data.imgData.width}px`,
  25. height: isMobile ? mobileHeight + 'px' : `${data.imgData.height}px`,
  26. }"
  27. />
  28. </div>
  29. <!-- 如果是查看答案模式 v-if 下面画画的vue-esign不显示 -->
  30. <!-- <img :src="answer.answer_list[0].answer" alt="" /> -->
  31. <!-- <vue-esign
  32. ref="esign-drawing"
  33. class="esign-canvas"
  34. :width="data.image_width"
  35. :height="data.image_height"
  36. :is-crop="isCrop"
  37. :line-width="lineWidth"
  38. :line-color="lineColor"
  39. /> -->
  40. <VueSignaturePad
  41. ref="signaturePad"
  42. class="esign-canvas"
  43. :width="isMobile ? '100%' : data.image_width + 'px'"
  44. :height="isMobile ? mobileHeight + 'px' : data.image_height + 'px'"
  45. :options="options"
  46. />
  47. </div>
  48. <div class="footer">
  49. <div class="pen">
  50. <!-- 选择颜色 -->
  51. <el-color-picker v-model="lineColor" @change="changeColor" />
  52. <!-- 清除一笔 -->
  53. <img class="clean-btn" src="@/assets/drawing-back.png" @click="undo" />
  54. <!-- 清空画板 -->
  55. <img class="clean-btn" src="@/assets/icon-clean.png" @click="clear" />
  56. <img :src="penIndex == 0 ? thinpenActive : thinpen" class="pen-img" @click="selSize(0)" />
  57. <img :src="penIndex == 1 ? thickpenActive : thickpen" class="pen-img" @click="selSize(1)" />
  58. <img :src="penIndex == 2 ? thicskpenActive : thicskpen" class="pen-img" @click="selSize(2)" />
  59. <div class="save" @click="save">Save</div>
  60. </div>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import PreviewMixin from '../common/PreviewMixin';
  66. import { getDrawingData } from '@/views/book/courseware/data/drawing';
  67. export default {
  68. name: 'DrawingPreview',
  69. mixins: [PreviewMixin],
  70. props: {
  71. isMobile: {
  72. type: Boolean,
  73. default: false,
  74. },
  75. },
  76. data() {
  77. return {
  78. data: getDrawingData(),
  79. penIndex: 0,
  80. image_url: '',
  81. lineWidth: 2,
  82. lineColor: '#000000',
  83. bgColor: '#f7f8fa',
  84. isCrop: false,
  85. weightList: [1, 2, 3],
  86. thinpen: require('@/assets/thin-pen.png'), // 细笔
  87. thinpenActive: require('@/assets/thin-pen-active.png'),
  88. thickpen: require('@/assets/thick-pen.png'),
  89. thickpenActive: require('@/assets/thick-pen-active.png'),
  90. thicskpen: require('@/assets/thicks-pen.png'),
  91. thicskpenActive: require('@/assets/thicks-pen-active.png'),
  92. resultImg: null,
  93. panelTitle: '',
  94. options: {
  95. penColor: this.lineColor,
  96. minWidth: 1, // 控制画笔最小宽度
  97. maxWidth: 1, // 控制画笔最大宽度
  98. },
  99. isActive1: true,
  100. isActive2: false,
  101. isActive3: false,
  102. imgSrc: '',
  103. signData: null,
  104. mobileHeight: 0,
  105. };
  106. },
  107. created() {
  108. this.initData();
  109. },
  110. mounted() {},
  111. methods: {
  112. // 撤销
  113. undo() {
  114. this.$refs.signaturePad.undoSignature();
  115. },
  116. // 清除
  117. clear() {
  118. this.$refs.signaturePad.clearSignature();
  119. },
  120. // 保存
  121. save() {
  122. this.signData = this.$refs.signaturePad.toData();
  123. const { data } = this.$refs.signaturePad.saveSignature();
  124. this.imgSrc = data;
  125. this.answer.answer_list[0].answer = data;
  126. this.$message.success(this.convertText('保存成功'));
  127. },
  128. replay() {
  129. const signaturePad = this.$refs.signaturePad;
  130. const data = signaturePad.toData(); // 获取签名数据
  131. signaturePad.clearSignature(); // 清空画布以开始新动画
  132. data.forEach((item) => {
  133. item.points.forEach((point, index) => {
  134. setTimeout(() => {
  135. signaturePad.lineTo(point.x, point.y); // 画线到指定点
  136. if (index === data.points.length - 1) {
  137. // 如果是最后一个点
  138. }
  139. }, index * 10); // 控制播放速度,可以根据需要调整时间间隔
  140. });
  141. });
  142. },
  143. // 调节颜色
  144. changeColor(val) {
  145. this.options = {
  146. penColor: val,
  147. minWidth: this.weightList[this.penIndex],
  148. maxWidth: this.weightList[this.penIndex],
  149. };
  150. },
  151. // 调节画笔粗细大小
  152. selSize(index) {
  153. this.penIndex = index;
  154. this.options = {
  155. penColor: this.lineColor,
  156. minWidth: this.weightList[this.penIndex],
  157. maxWidth: this.weightList[this.penIndex],
  158. };
  159. },
  160. initData() {
  161. if (!this.isJudgingRightWrong) {
  162. this.answer.answer_list = [];
  163. let obj = {
  164. answer: '',
  165. };
  166. this.answer.answer_list.push(obj);
  167. }
  168. this.data.image_list.forEach((item) => {
  169. this.image_url = item.file_url;
  170. // GetFileURLMap({ file_id_list: [item.file_id] }).then(({ url_map }) => {
  171. // this.image_url = url_map[item.file_id];
  172. // });
  173. });
  174. if (this.isMobile) {
  175. setTimeout(() => {
  176. let totalWidth = document.querySelector('.drawimg-preview').offsetWidth;
  177. this.mobileHeight = (totalWidth / this.data.image_width) * this.data.image_height;
  178. }, 50);
  179. }
  180. },
  181. // 保存图片
  182. handleGenerate() {
  183. this.$refs['esign-drawing']
  184. .generate()
  185. .then((res) => {
  186. this.resultImg = res;
  187. this.answer.answer_list[0].answer = res;
  188. this.$message.success('保存成功');
  189. })
  190. .catch((err) => {});
  191. },
  192. changePen(index) {
  193. this.penIndex = index;
  194. this.lineWidth = this.weightList[this.penIndex];
  195. },
  196. handleReset() {
  197. this.$refs['esign-drawing'].reset(false);
  198. },
  199. // 删除最后一笔
  200. removeOne() {
  201. // this.$refs['esign-drawing'].removelastOne();
  202. },
  203. },
  204. };
  205. </script>
  206. <style lang="scss" scoped>
  207. @use '@/styles/mixin.scss' as *;
  208. .img-box {
  209. position: relative;
  210. margin: 20px auto;
  211. }
  212. .img-set {
  213. position: relative;
  214. z-index: 0;
  215. display: inline-grid;
  216. grid-template:
  217. ' . . . ' 2px
  218. ' . img . ' auto
  219. ' . . . ' 2px
  220. / 2px auto 2px;
  221. img {
  222. object-fit: cover;
  223. }
  224. }
  225. .esign-canvas {
  226. position: absolute;
  227. top: 0;
  228. left: 0;
  229. z-index: 1;
  230. }
  231. .pen {
  232. display: flex;
  233. align-items: center;
  234. justify-content: flex-end;
  235. .save {
  236. box-sizing: border-box;
  237. width: 65px;
  238. height: 32px;
  239. margin-left: 16px;
  240. font-size: 16px;
  241. font-weight: 400;
  242. line-height: 32px;
  243. color: #000;
  244. text-align: center;
  245. cursor: pointer;
  246. background: rgba(0, 0, 0, 5%);
  247. border: 1px solid rgba(0, 0, 0, 10%);
  248. border-radius: 6px;
  249. }
  250. .save:hover {
  251. background: rgba(0, 0, 0, 25%);
  252. }
  253. > img {
  254. width: 24px;
  255. height: 24px;
  256. margin: 0 8px;
  257. cursor: pointer;
  258. }
  259. }
  260. </style>