sadais-piui 1.2.7-beta69 → 1.2.7-beta70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/pi-img-cropper/index.vue +110 -110
- package/package.json +1 -1
|
@@ -18,20 +18,20 @@
|
|
|
18
18
|
v-if="canvasId"
|
|
19
19
|
:id="canvasId"
|
|
20
20
|
:canvas-id="canvasId"
|
|
21
|
-
style="position: absolute; left: -500000px; top: -500000px
|
|
21
|
+
style="position: absolute; left: -500000px; top: -500000px"
|
|
22
22
|
:style="canvasStyle"
|
|
23
23
|
/>
|
|
24
24
|
</view>
|
|
25
25
|
</template>
|
|
26
26
|
<script>
|
|
27
|
-
import Touch from
|
|
28
|
-
import Vector from
|
|
29
|
-
import Canvas from
|
|
30
|
-
import { getConfig } from
|
|
27
|
+
import Touch from "./touch";
|
|
28
|
+
import Vector from "./vector";
|
|
29
|
+
import Canvas from "./canvas";
|
|
30
|
+
import { getConfig } from "../../config";
|
|
31
31
|
|
|
32
|
-
const { imgCropper } = getConfig()
|
|
32
|
+
const { imgCropper } = getConfig();
|
|
33
33
|
export default {
|
|
34
|
-
name:
|
|
34
|
+
name: "PiImgCropper",
|
|
35
35
|
mixins: [Touch, Vector, Canvas],
|
|
36
36
|
props: {
|
|
37
37
|
// 自定义样式
|
|
@@ -39,97 +39,97 @@ export default {
|
|
|
39
39
|
type: Object,
|
|
40
40
|
default() {
|
|
41
41
|
// {}
|
|
42
|
-
return imgCropper.customStyle
|
|
43
|
-
}
|
|
42
|
+
return imgCropper.customStyle;
|
|
43
|
+
},
|
|
44
44
|
},
|
|
45
45
|
// 自定义样式类
|
|
46
46
|
customClass: {
|
|
47
47
|
type: String,
|
|
48
48
|
default() {
|
|
49
49
|
// ''
|
|
50
|
-
return imgCropper.customClass
|
|
51
|
-
}
|
|
50
|
+
return imgCropper.customClass;
|
|
51
|
+
},
|
|
52
52
|
},
|
|
53
53
|
// 图片src
|
|
54
54
|
src: {
|
|
55
55
|
type: String,
|
|
56
56
|
// `''`
|
|
57
|
-
default: imgCropper.src
|
|
57
|
+
default: imgCropper.src,
|
|
58
58
|
},
|
|
59
59
|
// 图片格式
|
|
60
60
|
fileType: {
|
|
61
61
|
type: String,
|
|
62
62
|
// `png`
|
|
63
|
-
deault: imgCropper.fileType
|
|
63
|
+
deault: imgCropper.fileType,
|
|
64
64
|
},
|
|
65
65
|
// 图片质量(0,1]
|
|
66
66
|
quality: {
|
|
67
67
|
type: Number,
|
|
68
68
|
// `1`
|
|
69
|
-
default: imgCropper.quality
|
|
69
|
+
default: imgCropper.quality,
|
|
70
70
|
},
|
|
71
71
|
// 裁剪窗口大小
|
|
72
72
|
cropSize: {
|
|
73
73
|
// {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
|
|
74
74
|
type: Object,
|
|
75
75
|
// { width: `'50%'`, height: `'50%'`}
|
|
76
|
-
default: () => imgCropper.cropSize
|
|
76
|
+
default: () => imgCropper.cropSize,
|
|
77
77
|
},
|
|
78
78
|
// 最小的裁剪窗口
|
|
79
79
|
minCropSize: {
|
|
80
80
|
// {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
|
|
81
81
|
type: Object,
|
|
82
82
|
// { width: `'50px'`, height: `'50px'`}
|
|
83
|
-
default: () => imgCropper.minCropSize
|
|
83
|
+
default: () => imgCropper.minCropSize,
|
|
84
84
|
},
|
|
85
85
|
// true: 开启图片边界检测 保证移动图片过程,当图片的边与裁剪框的边重叠时 就无法继续移动
|
|
86
86
|
boundaryDetect: {
|
|
87
87
|
type: Boolean,
|
|
88
88
|
// `true`
|
|
89
|
-
default: imgCropper.boundaryDetect
|
|
89
|
+
default: imgCropper.boundaryDetect,
|
|
90
90
|
},
|
|
91
91
|
// 是否保持裁剪框的高宽比不变
|
|
92
92
|
keepCropRatio: {
|
|
93
93
|
type: Boolean,
|
|
94
94
|
// `false`
|
|
95
|
-
default: imgCropper.keepCropRatio
|
|
95
|
+
default: imgCropper.keepCropRatio,
|
|
96
96
|
},
|
|
97
97
|
// 是否禁止旋转
|
|
98
98
|
disableRotate: {
|
|
99
99
|
type: Boolean,
|
|
100
100
|
// `false`
|
|
101
|
-
default: imgCropper.disableRotate
|
|
101
|
+
default: imgCropper.disableRotate,
|
|
102
102
|
},
|
|
103
103
|
// 放大裁剪窗口的倍数
|
|
104
104
|
canvasZoom: {
|
|
105
105
|
type: Number,
|
|
106
106
|
// `1`
|
|
107
|
-
default: imgCropper.canvasZoom
|
|
107
|
+
default: imgCropper.canvasZoom,
|
|
108
108
|
},
|
|
109
109
|
// 遮罩层背景颜色
|
|
110
110
|
maskBackground: {
|
|
111
111
|
type: String,
|
|
112
112
|
// 'rgba(0, 0, 0, .5)'
|
|
113
|
-
default: imgCropper.maskBackground
|
|
114
|
-
}
|
|
113
|
+
default: imgCropper.maskBackground,
|
|
114
|
+
},
|
|
115
115
|
},
|
|
116
116
|
data() {
|
|
117
117
|
return {
|
|
118
118
|
ratio: 1,
|
|
119
119
|
root: {
|
|
120
120
|
width: 0,
|
|
121
|
-
height: 0
|
|
121
|
+
height: 0,
|
|
122
122
|
},
|
|
123
123
|
img: {
|
|
124
|
-
path:
|
|
124
|
+
path: "",
|
|
125
125
|
transform: {
|
|
126
126
|
x: 0,
|
|
127
127
|
y: 0,
|
|
128
128
|
rotate: 0,
|
|
129
|
-
scale: 1
|
|
129
|
+
scale: 1,
|
|
130
130
|
},
|
|
131
131
|
width: 0,
|
|
132
|
-
height: 0
|
|
132
|
+
height: 0,
|
|
133
133
|
},
|
|
134
134
|
crop: {
|
|
135
135
|
top: 0,
|
|
@@ -138,106 +138,106 @@ export default {
|
|
|
138
138
|
left: 0,
|
|
139
139
|
ratio: 0,
|
|
140
140
|
width: 0,
|
|
141
|
-
height: 0
|
|
141
|
+
height: 0,
|
|
142
142
|
},
|
|
143
|
-
focus: true
|
|
144
|
-
}
|
|
143
|
+
focus: true,
|
|
144
|
+
};
|
|
145
145
|
},
|
|
146
146
|
computed: {
|
|
147
147
|
imgStyle() {
|
|
148
148
|
const {
|
|
149
149
|
width,
|
|
150
150
|
height,
|
|
151
|
-
transform: { x, y, rotate, scale }
|
|
152
|
-
} = this.img
|
|
151
|
+
transform: { x, y, rotate, scale },
|
|
152
|
+
} = this.img;
|
|
153
153
|
// 指定图片的大小、位置、旋转角度、缩放系数
|
|
154
154
|
const style = {
|
|
155
155
|
width: `${width}px`,
|
|
156
156
|
height: `${height}px`,
|
|
157
|
-
transform: `translate3d(${x}px, ${y}px, 0) rotate(${rotate}deg) scale(${scale})
|
|
158
|
-
}
|
|
159
|
-
return style
|
|
157
|
+
transform: `translate3d(${x}px, ${y}px, 0) rotate(${rotate}deg) scale(${scale})`,
|
|
158
|
+
};
|
|
159
|
+
return style;
|
|
160
160
|
},
|
|
161
161
|
cropStyle() {
|
|
162
|
-
const { top: t, right: r, bottom: b, left: l } = this.crop
|
|
162
|
+
const { top: t, right: r, bottom: b, left: l } = this.crop;
|
|
163
163
|
// 指定裁剪窗口的位置、大小、周边的遮罩层效果
|
|
164
164
|
return {
|
|
165
165
|
top: `${t}px`,
|
|
166
166
|
right: `${r}px`,
|
|
167
167
|
bottom: `${b}px`,
|
|
168
168
|
left: `${l}px`,
|
|
169
|
-
outline: `50000px solid ${this.maskBackground}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
169
|
+
outline: `50000px solid ${this.maskBackground}`,
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
172
|
},
|
|
173
173
|
watch: {
|
|
174
174
|
src() {
|
|
175
|
-
this.initImage()
|
|
176
|
-
}
|
|
175
|
+
this.initImage();
|
|
176
|
+
},
|
|
177
177
|
},
|
|
178
|
-
|
|
178
|
+
mounted() {
|
|
179
179
|
uni.getSystemInfo({
|
|
180
|
-
success: result => {
|
|
181
|
-
this.ratio = result.windowWidth / 750
|
|
180
|
+
success: (result) => {
|
|
181
|
+
this.ratio = result.windowWidth / 750;
|
|
182
182
|
this.$nextTick(() => {
|
|
183
|
-
this.initUI()
|
|
184
|
-
})
|
|
185
|
-
}
|
|
186
|
-
})
|
|
183
|
+
this.initUI();
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
187
|
},
|
|
188
188
|
methods: {
|
|
189
189
|
async initUI() {
|
|
190
|
-
const { width, height } = await this.getContainerSize()
|
|
190
|
+
const { width, height } = await this.getContainerSize();
|
|
191
191
|
this.root = {
|
|
192
192
|
width,
|
|
193
|
-
height
|
|
194
|
-
}
|
|
195
|
-
this.initImage()
|
|
196
|
-
this.initCropWindow()
|
|
193
|
+
height,
|
|
194
|
+
};
|
|
195
|
+
this.initImage();
|
|
196
|
+
this.initCropWindow();
|
|
197
197
|
},
|
|
198
198
|
getContainerSize() {
|
|
199
|
-
return new Promise(resolve => {
|
|
200
|
-
let query = uni.createSelectorQuery()
|
|
199
|
+
return new Promise((resolve) => {
|
|
200
|
+
let query = uni.createSelectorQuery();
|
|
201
201
|
// #ifndef MP-ALIPAY
|
|
202
|
-
query = query.in(this)
|
|
202
|
+
query = query.in(this);
|
|
203
203
|
// #endif
|
|
204
|
-
const node = query.select(
|
|
204
|
+
const node = query.select(".pi-img-cropper");
|
|
205
205
|
node
|
|
206
|
-
.boundingClientRect(result => {
|
|
207
|
-
resolve(result)
|
|
206
|
+
.boundingClientRect((result) => {
|
|
207
|
+
resolve(result);
|
|
208
208
|
})
|
|
209
|
-
.exec()
|
|
210
|
-
})
|
|
209
|
+
.exec();
|
|
210
|
+
});
|
|
211
211
|
},
|
|
212
212
|
async initImage() {
|
|
213
213
|
if (!this.src) {
|
|
214
|
-
return
|
|
214
|
+
return;
|
|
215
215
|
}
|
|
216
|
-
const img = await this.loadImageInfo(this.src)
|
|
216
|
+
const img = await this.loadImageInfo(this.src);
|
|
217
217
|
let w = 0,
|
|
218
|
-
h = 0
|
|
219
|
-
const root = this.root
|
|
218
|
+
h = 0;
|
|
219
|
+
const root = this.root;
|
|
220
220
|
if (img.height > img.width) {
|
|
221
221
|
// 竖向图片:高度铺满屏幕高度、宽度等比计算
|
|
222
|
-
h = root.height
|
|
223
|
-
w = (h / img.height) * img.width
|
|
222
|
+
h = root.height;
|
|
223
|
+
w = (h / img.height) * img.width;
|
|
224
224
|
if (w > root.width) {
|
|
225
225
|
// 若计算后的宽度超出屏幕宽度 则等比缩小
|
|
226
|
-
w = root.width
|
|
227
|
-
h = (w / img.width) * img.height
|
|
226
|
+
w = root.width;
|
|
227
|
+
h = (w / img.width) * img.height;
|
|
228
228
|
}
|
|
229
229
|
} else {
|
|
230
230
|
// 横向图片
|
|
231
|
-
w = root.width
|
|
232
|
-
h = (w / img.width) * img.height
|
|
231
|
+
w = root.width;
|
|
232
|
+
h = (w / img.width) * img.height;
|
|
233
233
|
if (h > root.height) {
|
|
234
|
-
h = root.height
|
|
235
|
-
w = (h / img.height) * img.width
|
|
234
|
+
h = root.height;
|
|
235
|
+
w = (h / img.height) * img.width;
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
|
-
const x = (root.width - img.width) / 2
|
|
239
|
-
const y = (root.height - img.height) / 2
|
|
240
|
-
const scale = w / img.width
|
|
238
|
+
const x = (root.width - img.width) / 2;
|
|
239
|
+
const y = (root.height - img.height) / 2;
|
|
240
|
+
const scale = w / img.width;
|
|
241
241
|
this.img = {
|
|
242
242
|
path: img.path,
|
|
243
243
|
width: img.width,
|
|
@@ -246,35 +246,35 @@ export default {
|
|
|
246
246
|
x,
|
|
247
247
|
y,
|
|
248
248
|
rotate: 0,
|
|
249
|
-
scale
|
|
250
|
-
}
|
|
251
|
-
}
|
|
249
|
+
scale,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
252
|
},
|
|
253
253
|
async loadImageInfo(src) {
|
|
254
|
-
return new Promise(resolve => {
|
|
254
|
+
return new Promise((resolve) => {
|
|
255
255
|
uni.getImageInfo({
|
|
256
256
|
src: this.src,
|
|
257
|
-
success: result => {
|
|
258
|
-
resolve(result)
|
|
257
|
+
success: (result) => {
|
|
258
|
+
resolve(result);
|
|
259
|
+
},
|
|
260
|
+
fail: (error) => {
|
|
261
|
+
resolve(null);
|
|
259
262
|
},
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
})
|
|
264
|
-
})
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
265
|
},
|
|
266
266
|
initCropWindow() {
|
|
267
267
|
// 初始化裁剪窗口
|
|
268
|
-
const root = this.root
|
|
269
|
-
let w = this.toPx(this.cropSize.width)
|
|
270
|
-
let h = this.toPx(this.cropSize.height)
|
|
271
|
-
const minW = this.toPx(this.minCropSize.width)
|
|
272
|
-
const minH = this.toPx(this.minCropSize.height)
|
|
273
|
-
w = w < minW ? minW : w > root.width ? root.width : w
|
|
274
|
-
h = h < minH ? minH : h > root.height ? root.height : h
|
|
275
|
-
const ratio = h / w
|
|
276
|
-
const left = (root.width - w) / 2
|
|
277
|
-
const top = (root.height - h) / 2
|
|
268
|
+
const root = this.root;
|
|
269
|
+
let w = this.toPx(this.cropSize.width);
|
|
270
|
+
let h = this.toPx(this.cropSize.height);
|
|
271
|
+
const minW = this.toPx(this.minCropSize.width);
|
|
272
|
+
const minH = this.toPx(this.minCropSize.height);
|
|
273
|
+
w = w < minW ? minW : w > root.width ? root.width : w;
|
|
274
|
+
h = h < minH ? minH : h > root.height ? root.height : h;
|
|
275
|
+
const ratio = h / w;
|
|
276
|
+
const left = (root.width - w) / 2;
|
|
277
|
+
const top = (root.height - h) / 2;
|
|
278
278
|
this.crop = {
|
|
279
279
|
top,
|
|
280
280
|
left,
|
|
@@ -282,28 +282,28 @@ export default {
|
|
|
282
282
|
bottom: top,
|
|
283
283
|
width: w,
|
|
284
284
|
height: h,
|
|
285
|
-
ratio
|
|
286
|
-
}
|
|
285
|
+
ratio,
|
|
286
|
+
};
|
|
287
287
|
},
|
|
288
288
|
toPx(unit) {
|
|
289
|
-
let value = 0
|
|
289
|
+
let value = 0;
|
|
290
290
|
if (unit) {
|
|
291
291
|
if (/%$/.test(unit)) {
|
|
292
|
-
value = Number(unit.replace(/%$/,
|
|
293
|
-
value = !value ? 0 : value
|
|
294
|
-
value = value * 0.01 * this.root.width
|
|
292
|
+
value = Number(unit.replace(/%$/, ""));
|
|
293
|
+
value = !value ? 0 : value;
|
|
294
|
+
value = value * 0.01 * this.root.width;
|
|
295
295
|
} else if (/rpx$/i.test(unit)) {
|
|
296
|
-
value = Number(unit.replace(/rpx$/i,
|
|
297
|
-
value = !value ? 0 : value
|
|
298
|
-
value = value * this.ratio
|
|
296
|
+
value = Number(unit.replace(/rpx$/i, ""));
|
|
297
|
+
value = !value ? 0 : value;
|
|
298
|
+
value = value * this.ratio;
|
|
299
299
|
} else {
|
|
300
|
-
value = Number(unit.replace(/px$/i,
|
|
300
|
+
value = Number(unit.replace(/px$/i, ""));
|
|
301
301
|
}
|
|
302
302
|
}
|
|
303
|
-
return value
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
303
|
+
return value;
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
307
|
</script>
|
|
308
308
|
|
|
309
309
|
<style lang="scss" scoped>
|