SelectBackground.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <el-dialog
  3. custom-class="background"
  4. width="608px"
  5. :close-on-click-modal="false"
  6. :visible="visible"
  7. :before-close="handleClose"
  8. >
  9. <div class="select-background">
  10. <div class="select-background-top">
  11. <span class="tab">背景图</span>
  12. <SelectUpload type="image" @uploadSuccess="uploadSuccess" />
  13. </div>
  14. <div class="background-img">
  15. <div v-if="file_url" class="img-set" :style="{ top: `${imgData.top - 9}px`, left: `${imgData.left}px` }">
  16. <div class="dot top-left" @mousedown="dragStart($event, 'nwse-resize', 'top-left')"></div>
  17. <div class="horizontal-line" @mousedown="dragStart($event, 'ns-resize', 'top')"></div>
  18. <div class="dot top-right" @mousedown="dragStart($event, 'nesw-resize', 'top-right')"></div>
  19. <div class="vertical-line" @mousedown="dragStart($event, 'ew-resize', 'left')"></div>
  20. <img
  21. :src="file_url"
  22. draggable="false"
  23. alt="背景图"
  24. :style="{ width: `${imgData.width}px`, height: `${imgData.height}px` }"
  25. @mousedown="dragStart($event, 'move', 'move')"
  26. />
  27. <div class="vertical-line" @mousedown="dragStart($event, 'ew-resize', 'right')"></div>
  28. <div class="dot bottom-left" @mousedown="dragStart($event, 'nesw-resize', 'bottom-left')"></div>
  29. <div class="horizontal-line" @mousedown="dragStart($event, 'ns-resize', 'bottom')"></div>
  30. <div class="dot bottom-right" @mousedown="dragStart($event, 'nwse-resize', 'bottom-right')"></div>
  31. </div>
  32. </div>
  33. </div>
  34. <div slot="footer">
  35. <el-button @click="handleClose">取消</el-button>
  36. <el-button type="primary" @click="confirm">确定</el-button>
  37. </div>
  38. </el-dialog>
  39. </template>
  40. <script>
  41. import SelectUpload from './common/SelectUpload.vue';
  42. export default {
  43. name: 'SelectBackground',
  44. components: {
  45. SelectUpload,
  46. },
  47. props: {
  48. visible: {
  49. type: Boolean,
  50. required: true,
  51. },
  52. },
  53. data() {
  54. return {
  55. maxWidth: 576,
  56. maxHeight: 310,
  57. file_url: '',
  58. drag: {
  59. dragging: false,
  60. startX: 0,
  61. startY: 0,
  62. type: '',
  63. },
  64. imgData: {
  65. width: 0,
  66. height: 0,
  67. top: 0,
  68. left: 0,
  69. },
  70. };
  71. },
  72. mounted() {
  73. document.querySelector('.el-dialog__wrapper').addEventListener('mousemove', this.mouseMove);
  74. document.body.addEventListener('mouseup', this.mouseUp);
  75. },
  76. beforeDestroy() {
  77. document.querySelector('.el-dialog__wrapper')?.removeEventListener('mousemove', this.mouseMove);
  78. document.body.removeEventListener('mouseup', this.mouseUp);
  79. },
  80. methods: {
  81. handleClose() {
  82. this.$emit('update:visible', false);
  83. },
  84. confirm() {
  85. this.$emit('update:visible', false);
  86. const { width, height, top, left } = this.imgData;
  87. this.$emit('setBackgroundImage', this.file_url, {
  88. width: (width / this.maxWidth) * 100,
  89. height: (height / this.maxHeight) * 100,
  90. top: (top / this.maxHeight) * 100,
  91. left: (left / this.maxWidth) * 100,
  92. });
  93. },
  94. /**
  95. * 拖拽开始
  96. * @param {MouseEvent} event
  97. * @param {string} cursor
  98. * @param {string} type
  99. */
  100. dragStart(event, cursor, type) {
  101. const { clientX, clientY } = event;
  102. this.drag = {
  103. dragging: true,
  104. startX: clientX,
  105. startY: clientY,
  106. type,
  107. };
  108. document.querySelector('.el-dialog__wrapper').style.cursor = cursor;
  109. },
  110. /**
  111. * 鼠标移动
  112. * @param {MouseEvent} event
  113. */
  114. mouseMove(event) {
  115. if (!this.drag.dragging) return;
  116. const { clientX, clientY } = event;
  117. const { startX, startY, type } = this.drag;
  118. const widthDiff = clientX - startX;
  119. const heightDiff = clientY - startY;
  120. if (type === 'top-left') {
  121. this.imgData.width = Math.min(this.maxWidth, Math.max(0, this.imgData.width - widthDiff));
  122. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  123. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  124. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  125. } else if (type === 'top-right') {
  126. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  127. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  128. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  129. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  130. } else if (type === 'bottom-left') {
  131. this.imgData.width = Math.min(this.maxWidth, Math.max(0, this.imgData.width - widthDiff));
  132. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  133. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  134. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  135. } else if (type === 'bottom-right') {
  136. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  137. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  138. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  139. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  140. }
  141. if (type === 'top') {
  142. this.imgData.height = Math.min(this.maxHeight, Math.max(0, this.imgData.height - heightDiff));
  143. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  144. } else if (type === 'bottom') {
  145. this.imgData.height = Math.min(this.maxHeight, Math.max(this.imgData.height + heightDiff));
  146. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top));
  147. } else if (type === 'left') {
  148. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width - widthDiff));
  149. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  150. } else if (type === 'right') {
  151. this.imgData.width = Math.min(this.maxWidth, Math.max(this.imgData.width + widthDiff));
  152. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left));
  153. }
  154. if (type === 'move') {
  155. this.imgData.top = Math.min(this.maxHeight - this.imgData.height, Math.max(0, this.imgData.top + heightDiff));
  156. this.imgData.left = Math.min(this.maxWidth - this.imgData.width, Math.max(0, this.imgData.left + widthDiff));
  157. }
  158. this.drag.startX = clientX;
  159. this.drag.startY = clientY;
  160. },
  161. /**
  162. * 鼠标抬起
  163. */
  164. mouseUp() {
  165. this.drag.dragging = false;
  166. document.querySelector('.el-dialog__wrapper').style.cursor = 'auto';
  167. },
  168. /**
  169. * 上传成功
  170. * @param {array} fileList
  171. */
  172. uploadSuccess(fileList) {
  173. if (fileList.length > 0) {
  174. let file_url = fileList[0].file_url;
  175. const img = new Image();
  176. img.src = file_url;
  177. img.onload = () => {
  178. const { width, height } = img;
  179. if (width > this.maxWidth || height > this.maxHeight) {
  180. const wScale = width / this.maxWidth;
  181. const hScale = height / this.maxHeight;
  182. const scale = wScale > hScale ? this.maxWidth / 2 / width : this.maxHeight / 2 / height;
  183. this.imgData = {
  184. width: width * scale,
  185. height: height * scale,
  186. top: 0,
  187. left: 0,
  188. };
  189. } else {
  190. this.imgData = {
  191. width,
  192. height,
  193. top: 0,
  194. left: 0,
  195. };
  196. }
  197. this.file_url = fileList[0].file_url_open;
  198. };
  199. }
  200. },
  201. },
  202. };
  203. </script>
  204. <style lang="scss" scoped>
  205. .select-background {
  206. &-top {
  207. display: flex;
  208. align-items: center;
  209. margin-bottom: 16px;
  210. .tab {
  211. width: 80px;
  212. font-size: 14px;
  213. color: $font-light-color;
  214. }
  215. }
  216. .background-img {
  217. height: 310px;
  218. border: 1px dashed rgba(0, 0, 0, 8%);
  219. .img-set {
  220. position: relative;
  221. display: inline-grid;
  222. grid-template:
  223. ' . . . ' 2px
  224. ' . img . ' auto
  225. ' . . . ' 2px
  226. / 2px auto 2px;
  227. img {
  228. object-fit: cover;
  229. }
  230. .horizontal-line,
  231. .vertical-line {
  232. background-color: $main-color;
  233. }
  234. .horizontal-line {
  235. width: 100%;
  236. height: 2px;
  237. cursor: ns-resize;
  238. }
  239. .vertical-line {
  240. width: 2px;
  241. height: 100%;
  242. cursor: ew-resize;
  243. }
  244. .dot {
  245. z-index: 1;
  246. width: 6px;
  247. height: 6px;
  248. background-color: $main-color;
  249. &.top-left {
  250. top: -2px;
  251. left: -2px;
  252. }
  253. &.top-right {
  254. top: -2px;
  255. right: 2px;
  256. }
  257. &.bottom-left {
  258. bottom: 2px;
  259. left: -2px;
  260. }
  261. &.bottom-right {
  262. right: 2px;
  263. bottom: 2px;
  264. }
  265. &.top-left,
  266. &.bottom-right {
  267. position: relative;
  268. cursor: nwse-resize;
  269. }
  270. &.top-right,
  271. &.bottom-left {
  272. position: relative;
  273. cursor: nesw-resize;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. </style>
  280. <style lang="scss">
  281. .el-dialog.background {
  282. .el-dialog__header {
  283. display: none;
  284. }
  285. .el-dialog__body {
  286. padding: 8px 16px;
  287. }
  288. }
  289. </style>