5
5
<template slot="hintInfo">
6
6
<p >基于VueDRR拖拽功能,在其上通过叠加图片、文字等,实现图片的叠加伪合成功能</p >
7
7
<p >VueDRR:基于vue-draggable-resizable的vue组件,可以实现拖动、拉伸和旋转功能</p >
8
- <p >github地址:访问 <el-link type =" success" href =" https://github.com/Alvin-Liu/vue-drr" target =" _blank" >VueDRR</el-link ></p >
9
8
</template >
10
9
</Hints >
11
10
<el-row :gutter =" 20" >
@@ -73,7 +72,7 @@ export default {
73
72
height: 0 ,
74
73
bgImageSrc: ' ../../../static/img/crop-image.jpg' // 底图路径
75
74
},
76
- elements: [],
75
+ elements: [], // 叠加组件数组
77
76
activeEle: {}, // 当前图片上聚焦的叠加组件
78
77
eleNum: 0
79
78
}
@@ -127,6 +126,7 @@ export default {
127
126
zIndex: zIndex
128
127
}
129
128
},
129
+ // 底图上传成功
130
130
handleSuccess (data ) {
131
131
this .container .bgImageSrc = data
132
132
const image = new Image ()
@@ -135,39 +135,46 @@ export default {
135
135
this .getDragContainerSize (image .width , image .height )
136
136
}
137
137
},
138
- // 计算图片展示区宽高
139
- getDragContainerSize (width , height ) {
140
- let [imageTrueW, imageTrueH, imageShowW, imageShowH] = [+ width, + height, 0 , 0 ]
141
- let [imageMaxW, imageMaxH] = [850 , 550 ]
142
-
138
+ // 计算图片宽高
139
+ getImageSize (imageTrueW , imageTrueH , showAreaW , showAreaH ) {
140
+ let [width, height] = [0 , 0 ]
143
141
// 图片真实宽大于真实高
144
142
if (imageTrueW > imageTrueH) {
145
- if (imageTrueW >= imageMaxW ) { // 真实宽大于或等于最大宽
146
- const imageRatioH = imageTrueH * (imageMaxW / imageTrueW)
147
- // 按最大宽与实际宽比率换算后 ,高度大于显示高度时
148
- if (imageRatioH >= imageMaxH ) {
149
- imageShowW = imageTrueW * (imageMaxH / imageTrueH)
150
- imageShowH = imageMaxH
143
+ if (imageTrueW >= showAreaW ) { // 真实宽大于或等于展示区最大宽
144
+ const imageRatioH = imageTrueH * (showAreaW / imageTrueW)
145
+ // 按展示区最大宽与实际宽比率换算后 ,高度大于显示高度时
146
+ if (imageRatioH >= showAreaW ) {
147
+ width = imageTrueW * (showAreaH / imageTrueH)
148
+ height = showAreaH
151
149
} else {
152
- imageShowW = imageMaxW
153
- imageShowH = imageRatioH
150
+ width = showAreaW
151
+ height = imageRatioH
154
152
}
155
153
} else {
156
- imageShowW = imageTrueW
157
- imageShowH = imageTrueH
154
+ width = imageTrueW
155
+ height = imageTrueH
158
156
}
159
- } else if (imageTrueW <= imageTrueH) { // 图片真实宽小于或等于真实高
160
- if (imageTrueH >= imageMaxH ) { // 真实高大于或等于最大高
161
- imageShowW = imageTrueW * (imageMaxH / imageTrueH)
162
- imageShowH = imageMaxH
157
+ } else { // 图片真实宽小于或等于真实高
158
+ if (imageTrueH >= showAreaH ) { // 真实高大于或等于展示区最大高
159
+ width = imageTrueW * (showAreaH / imageTrueH)
160
+ height = showAreaH
163
161
} else {
164
- imageShowW = imageTrueW
165
- imageShowH = imageTrueH
162
+ width = imageTrueW
163
+ height = imageTrueH
166
164
}
167
165
}
166
+ return {
167
+ width,
168
+ height
169
+ }
170
+ },
171
+ // 计算底图展示宽高
172
+ getDragContainerSize (imageW , imageH ) {
173
+ const [showAreaW , showAreaH ] = [850 , 550 ]
174
+ const sizeObj = this .getImageSize (imageW, imageH, showAreaW, showAreaH)
168
175
// 更新图片展示区宽高
169
- this .container .width = imageShowW
170
- this .container .height = imageShowH
176
+ this .container .width = sizeObj . width
177
+ this .container .height = sizeObj . height
171
178
},
172
179
// 添加文本
173
180
addText () {
@@ -229,52 +236,16 @@ export default {
229
236
const img = new Image ()
230
237
img .src = data
231
238
img .onload = () => {
232
- let imageSize = this .getImageSize (img .width , img .height , this .container .width , this .container .height )
239
+ let imageSize = this .getImageSize (img .width , img .height , parseInt ( this .container .width / 4 ), parseInt ( this .container .height / 4 ) )
233
240
this .addImage ({
234
241
active: true ,
235
242
src: data,
236
243
size: dataSize,
237
- width: imageSize .w ,
238
- height: imageSize .h
244
+ width: imageSize .width ,
245
+ height: imageSize .height
239
246
})
240
247
}
241
248
},
242
- // 计算叠加图片的宽和高
243
- getImageSize (width , height , showAreaW , showAreaH ) {
244
- let [w, h] = [0 , 0 ]
245
- // 添加的叠加图默认最大为底图的1/4
246
- let showW = parseInt (showAreaW / 4 )
247
- let showH = parseInt (showAreaH / 4 )
248
- // 图片真实宽大于真实高
249
- if (width > height) {
250
- if (width >= showW) { // 真实宽大于或等于最大宽
251
- const imageRatioH = height * (showW / width)
252
- // 按最大宽与实际宽比率换算后,高度大于显示高度时
253
- if (imageRatioH >= showH) {
254
- w = width * (showH / height)
255
- h = showH
256
- } else {
257
- w = showW
258
- h = imageRatioH
259
- }
260
- } else {
261
- w = width
262
- h = height
263
- }
264
- } else if (width <= height) { // 图片真实宽小于或等于真实高
265
- if (height >= showH) { // 真实高大于或等于最大高
266
- w = width * (showH / height)
267
- h = showH
268
- } else {
269
- w = width
270
- h = height
271
- }
272
- }
273
- return {
274
- w,
275
- h
276
- }
277
- },
278
249
// 更新当前选中的元素
279
250
updateActiveEle (element ) {
280
251
this .activeEle = element
0 commit comments