uni-app 圖片裁剪
hbuilderX 官方demo 中有這個示例,可直接使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
|
<
template
>
<
view
class
=
"container"
>
<
view
class
=
"page-body uni-content-info"
>
<
view
class
=
'cropper-content'
>
<
view
v-if
=
"isShowImg"
class
=
"uni-corpper"
:style
=
"'width:'+cropperInitW+'px;height:'+cropperInitH+'px;background:#000'"
>
<
view
class
=
"uni-corpper-content"
:style
=
"'width:'+cropperW+'px;height:'+cropperH+'px;left:'+cropperL+'px;top:'+cropperT+'px'"
>
<
image
:src
=
"imageSrc"
:style
=
"'width:'+cropperW+'px;height:'+cropperH+'px'"
></
image
>
<
view
class
=
"uni-corpper-crop-box"
@
touchstart.stop
=
"contentStartMove"
@
touchmove.stop
=
"contentMoveing"
@
touchend.stop
=
"contentTouchEnd"
:style
=
"'left:'+cutL+'px;top:'+cutT+'px;right:'+cutR+'px;bottom:'+cutB+'px'"
>
<
view
class
=
"uni-cropper-view-box"
>
<
view
class
=
"uni-cropper-dashed-h"
></
view
>
<
view
class
=
"uni-cropper-dashed-v"
></
view
>
<
view
class
=
"uni-cropper-line-t"
data-drag
=
"top"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-line-r"
data-drag
=
"right"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-line-b"
data-drag
=
"bottom"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-line-l"
data-drag
=
"left"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-point point-t"
data-drag
=
"top"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-point point-tr"
data-drag
=
"topTight"
></
view
>
<
view
class
=
"uni-cropper-point point-r"
data-drag
=
"right"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-point point-rb"
data-drag
=
"rightBottom"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-point point-b"
data-drag
=
"bottom"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
@
touchend.stop
=
"dragEnd"
></
view
>
<
view
class
=
"uni-cropper-point point-bl"
data-drag
=
"bottomLeft"
></
view
>
<
view
class
=
"uni-cropper-point point-l"
data-drag
=
"left"
@
touchstart.stop
=
"dragStart"
@
touchmove.stop
=
"dragMove"
></
view
>
<
view
class
=
"uni-cropper-point point-lt"
data-drag
=
"leftTop"
></
view
>
</
view
>
</
view
>
</
view
>
</
view
>
</
view
>
<
view
class
=
'cropper-config'
>
<
button
type
=
"primary reverse"
@
click
=
"getImage"
style
=
'margin-top: 30upx;'
> 選擇圖片 </
button
>
<
button
type
=
"warn"
@
click
=
"getImageInfo"
style
=
'margin-top: 30upx;'
> 點擊生成圖片 </
button
>
</
view
>
<
canvas
canvas-id
=
"myCanvas"
:style
=
"'position:absolute;border: 1px solid red; width:'+imageW+'px;height:'+imageH+'px;top:-9999px;left:-9999px;'"
></
canvas
>
</
view
>
<
page-foot
:name
=
"name"
></
page-foot
>
</
view
>
</
template
>
<
script
>
let sysInfo = uni.getSystemInfoSync();
let SCREEN_WIDTH = sysInfo.screenWidth
let PAGE_X, // 手按下的x位置
PAGE_Y, // 手按下y的位置
PR = sysInfo.pixelRatio, // dpi
T_PAGE_X, // 手移動的時候x的位置
T_PAGE_Y, // 手移動的時候Y的位置
CUT_L, // 初始化拖拽元素的left值
CUT_T, // 初始化拖拽元素的top值
CUT_R, // 初始化拖拽元素的
CUT_B, // 初始化拖拽元素的
CUT_W, // 初始化拖拽元素的寬度
CUT_H, // 初始化拖拽元素的高度
IMG_RATIO, // 圖片比例
IMG_REAL_W, // 圖片實際的寬度
IMG_REAL_H, // 圖片實際的高度
DRAFG_MOVE_RATIO = 1, //移動時候的比例,
INIT_DRAG_POSITION = 100, // 初始化屏幕寬度和裁剪區域的寬度之差,用於設置初始化裁剪的寬度
DRAW_IMAGE_W = sysInfo.screenWidth // 設置生成的圖片寬度
export default {
/**
* 頁面的初始數據
*/
data() {
return {
name:'楊大寶',
imageSrc: 'https://img-cdn-qiniu.dcloud.net.cn/demo_crop.jpg',
isShowImg: false,
// 初始化的寬高
cropperInitW: SCREEN_WIDTH,
cropperInitH: SCREEN_WIDTH,
// 動態的寬高
cropperW: SCREEN_WIDTH,
cropperH: SCREEN_WIDTH,
// 動態的left top值
cropperL: 0,
cropperT: 0,
transL: 0,
transT: 0,
// 圖片縮放值
scaleP: 0,
imageW: 0,
imageH: 0,
// 裁剪框 寬高
cutL: 0,
cutT: 0,
cutB: SCREEN_WIDTH,
cutR: '100%',
qualityWidth: DRAW_IMAGE_W,
innerAspectRadio: DRAFG_MOVE_RATIO
}
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {},
/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function () {
this.loadImage();
},
methods: {
setData: function (obj) {
let that = this;
Object.keys(obj).forEach(function (key) {
that.$set(that.$data, key, obj[key])
});
},
getImage: function () {
var _this = this
uni.chooseImage({
success: function (res) {
_this.setData({
imageSrc: res.tempFilePaths[0],
})
_this.loadImage();
},
})
},
loadImage: function () {
var _this = this
uni.showLoading({
title: '圖片加載中...',
})
uni.getImageInfo({
src: _this.imageSrc,
success: function success(res) {
IMG_RATIO = res.width / res.height
if (IMG_RATIO >= 1) {
IMG_REAL_W = SCREEN_WIDTH
IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO
} else {
IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO
IMG_REAL_H = SCREEN_WIDTH
}
let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H
INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange
// 根據圖片的寬高顯示不同的效果 保證圖片可以正常顯示
if (IMG_RATIO >= 1) {
let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2);
let cutB = cutT;
let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2);
let cutR = cutL;
_this.setData({
cropperW: SCREEN_WIDTH,
cropperH: SCREEN_WIDTH / IMG_RATIO,
// 初始化left right
cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
cutL: cutL,
cutT: cutT,
cutR: cutR,
cutB: cutB,
// 圖片縮放值
imageW: IMG_REAL_W,
imageH: IMG_REAL_H,
scaleP: IMG_REAL_W / SCREEN_WIDTH,
qualityWidth: DRAW_IMAGE_W,
innerAspectRadio: IMG_RATIO
})
} else {
let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - (SCREEN_WIDTH * IMG_RATIO)) / 2);
let cutR = cutL;
let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2);
let cutB = cutT;
_this.setData({
cropperW: SCREEN_WIDTH * IMG_RATIO,
cropperH: SCREEN_WIDTH,
// 初始化left right
cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
cutL: cutL,
cutT: cutT,
cutR: cutR,
cutB: cutB,
// 圖片縮放值
imageW: IMG_REAL_W,
imageH: IMG_REAL_H,
scaleP: IMG_REAL_W / SCREEN_WIDTH,
qualityWidth: DRAW_IMAGE_W,
innerAspectRadio: IMG_RATIO
})
}
_this.setData({
isShowImg: true
})
uni.hideLoading()
}
})
},
// 拖動時候觸發的touchStart事件
contentStartMove(e) {
PAGE_X = e.touches[0].pageX
PAGE_Y = e.touches[0].pageY
},
// 拖動時候觸發的touchMove事件
contentMoveing(e) {
var _this = this
var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
// 左移
if (dragLengthX > 0) {
if (this.cutL - dragLengthX <
0
)
dragLengthX
=
this
.cutL
} else {
if (this.cutR + dragLengthX < 0) dragLengthX = -this.cutR
}
if (dragLengthY > 0) {
if (this.cutT - dragLengthY <
0
)
dragLengthY
=
this
.cutT
} else {
if (this.cutB + dragLengthY < 0) dragLengthY = -this.cutB
}
this.setData({
cutL: this.cutL - dragLengthX,
cutT: this.cutT - dragLengthY,
cutR: this.cutR + dragLengthX,
cutB: this.cutB + dragLengthY
})
PAGE_X
=
e
.touches[0].pageX
PAGE_Y
=
e
.touches[0].pageY
},
contentTouchEnd() {
},
// 獲取圖片
getImageInfo() {
var
_this
= this;
uni.showLoading({
title: '圖片生成中...',
});
// 將圖片寫入畫布
const
ctx
=
uni
.createCanvasContext('myCanvas');
ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
ctx.draw(true, () => {
// 獲取畫布要裁剪的位置和寬度 均為百分比 * 畫布中圖片的寬度 保證了在微信小程序中裁剪的圖片模糊 位置不對的問題 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
var canvasW = ((_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW) * IMG_REAL_W;
var canvasH = ((_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH) * IMG_REAL_H;
var canvasL = (_this.cutL / _this.cropperW) * IMG_REAL_W;
var canvasT = (_this.cutT / _this.cropperH) * IMG_REAL_H;
uni.canvasToTempFilePath({
x: canvasL,
y: canvasT,
width: canvasW,
height: canvasH,
destWidth: canvasW,
destHeight: canvasH,
quality: 0.5,
canvasId: 'myCanvas',
success: function (res) {
uni.hideLoading()
// 成功獲得地址的地方
uni.previewImage({
current: '', // 當前顯示圖片的http鏈接
urls: [res.tempFilePath] // 需要預覽的圖片http鏈接列表
})
}
});
});
},
// 設置大小的時候觸發的touchStart事件
dragStart(e) {
T_PAGE_X = e.touches[0].pageX
T_PAGE_Y = e.touches[0].pageY
CUT_L = this.cutL
CUT_R = this.cutR
CUT_B = this.cutB
CUT_T = this.cutT
},
// 設置大小的時候觸發的touchMove事件
dragMove(e) {
var _this = this
var dragType = e.target.dataset.drag
switch (dragType) {
case 'right':
var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
if (CUT_R + dragLength <
0
) dragLength = -CUT_R
this.setData({
cutR: CUT_R + dragLength
})
break;
case 'left':
var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
if (CUT_L - dragLength < 0)
dragLength
=
CUT_L
if ((CUT_L - dragLength) > (this.cropperW - this.cutR)) dragLength = CUT_L - (this.cropperW - this.cutR)
this.setData({
cutL: CUT_L - dragLength
})
break;
case 'top':
var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
if (CUT_T - dragLength <
0
)
dragLength
=
CUT_T
if ((CUT_T - dragLength) > (this.cropperH - this.cutB)) dragLength = CUT_T - (this.cropperH - this.cutB)
this.setData({
cutT: CUT_T - dragLength
})
break;
case 'bottom':
var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
if (CUT_B + dragLength <
0
) dragLength = -CUT_B
this.setData({
cutB: CUT_B + dragLength
})
break;
case 'rightBottom':
var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
if (CUT_B + dragLengthY < 0) dragLengthY = -CUT_B
if (CUT_R + dragLengthX < 0) dragLengthX = -CUT_R
let
cutB
=
CUT_B
+ dragLengthY;
let
cutR
=
CUT_R
+ dragLengthX;
this.setData({
cutB: cutB,
cutR: cutR
})
break;
default:
break;
}
}
}
}
</script>
<
style
>
/* pages/uni-cropper/index.wxss */
.uni-content-info {
/* position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: block;
align-items: center;
flex-direction: column; */
}
.cropper-config {
padding: 20upx 40upx;
}
.cropper-content {
min-height: 750upx;
width: 100%;
}
.uni-corpper {
position: relative;
overflow: hidden;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
box-sizing: border-box;
}
.uni-corpper-content {
position: relative;
}
.uni-corpper-content image {
display: block;
width: 100%;
min-width: 0 !important;
max-width: none !important;
height: 100%;
min-height: 0 !important;
max-height: none !important;
image-orientation: 0deg !important;
margin: 0 auto;
}
/* 移動圖片效果 */
.uni-cropper-drag-box {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
cursor: move;
background: rgba(0, 0, 0, 0.6);
z-index: 1;
}
/* 內部的信息 */
.uni-corpper-crop-box {
position: absolute;
background: rgba(255, 255, 255, 0.3);
z-index: 2;
}
.uni-corpper-crop-box .uni-cropper-view-box {
position: relative;
display: block;
width: 100%;
height: 100%;
overflow: visible;
outline: 1upx solid #69f;
outline-color: rgba(102, 153, 255, .75)
}
/* 橫向虛線 */
.uni-cropper-dashed-h {
position: absolute;
top: 33.33333333%;
left: 0;
width: 100%;
height: 33.33333333%;
border-top: 1upx dashed rgba(255, 255, 255, 0.5);
border-bottom: 1upx dashed rgba(255, 255, 255, 0.5);
}
/* 縱向虛線 */
.uni-cropper-dashed-v {
position: absolute;
left: 33.33333333%;
top: 0;
width: 33.33333333%;
height: 100%;
border-left: 1upx dashed rgba(255, 255, 255, 0.5);
border-right: 1upx dashed rgba(255, 255, 255, 0.5);
}
/* 四個方向的線 為了之后的拖動事件*/
.uni-cropper-line-t {
position: absolute;
display: block;
width: 100%;
top: 0;
left: 0;
height: 1upx;
opacity: 0.1;
cursor: n-resize;
}
.uni-cropper-line-t::before {
content: '';
position: absolute;
top: 50%;
right: 0upx;
width: 100%;
-webkit-transform: translate3d(0, -50%, 0);
transform: translate3d(0, -50%, 0);
bottom: 0;
height: 41upx;
background: transparent;
z-index: 11;
}
.uni-cropper-line-r {
position: absolute;
display: block;
top: 0;
right: 0upx;
width: 1upx;
opacity: 0.1;
height: 100%;
cursor: e-resize;
}
.uni-cropper-line-r::before {
content: '';
position: absolute;
top: 0;
left: 50%;
width: 41upx;
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
bottom: 0;
height: 100%;
background: transparent;
z-index: 11;
}
.uni-cropper-line-b {
position: absolute;
display: block;
width: 100%;
bottom: 0;
left: 0;
height: 1upx;
opacity: 0.1;
cursor: s-resize;
}
.uni-cropper-line-b::before {
content: '';
position: absolute;
top: 50%;
right: 0upx;
width: 100%;
-webkit-transform: translate3d(0, -50%, 0);
transform: translate3d(0, -50%, 0);
bottom: 0;
height: 41upx;
background: transparent;
z-index: 11;
}
.uni-cropper-line-l {
position: absolute;
display: block;
top: 0;
left: 0;
width: 1upx;
opacity: 0.1;
height: 100%;
cursor: w-resize;
}
.uni-cropper-line-l::before {
content: '';
position: absolute;
top: 0;
left: 50%;
width: 41upx;
-webkit-transform: translate3d(-50%, 0, 0);
transform: translate3d(-50%, 0, 0);
bottom: 0;
height: 100%;
background: transparent;
z-index: 11;
}
.uni-cropper-point {
width: 5upx;
height: 5upx;
opacity: .75;
position: absolute;
z-index: 3;
}
.point-t {
top: -3upx;
left: 50%;
margin-left: -3upx;
cursor: n-resize;
}
.point-tr {
top: -3upx;
left: 100%;
margin-left: -3upx;
cursor: n-resize;
}
.point-r {
top: 50%;
left: 100%;
margin-left: -3upx;
margin-top: -3upx;
cursor: n-resize;
}
.point-rb {
left: 100%;
top: 100%;
-webkit-transform: translate3d(-50%, -50%, 0);
transform: translate3d(-50%, -50%, 0);
cursor: n-resize;
width: 36upx;
height: 36upx;
position: absolute;
z-index: 1112;
opacity: 1;
}
.point-b {
left: 50%;
top: 100%;
margin-left: -3upx;
margin-top: -3upx;
cursor: n-resize;
}
.point-bl {
left: 0%;
top: 100%;
margin-left: -3upx;
margin-top: -3upx;
cursor: n-resize;
}
.point-l {
left: 0%;
top: 50%;
margin-left: -3upx;
margin-top: -3upx;
cursor: n-resize;
}
.point-lt {
left: 0%;
top: 0%;
margin-left: -3upx;
margin-top: -3upx;
cursor: n-resize;
}
/* 裁剪框預覽內容 */
.uni-cropper-viewer {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.uni-cropper-viewer image {
position: absolute;
z-index: 2;
}
</
style
>
|
