DrawingPreview.vue 6.8 KB

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