MrShi
2024-02-28 3a7cbae4f79e0043b9a75c9e419a841fc220c35a
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
<template>
    <view class="settingHeadImage" @touchstart="onTouchstart" @touchmove.stop.prevent="onTouchmove" @touchend="touchE">
        <!-- 蒙层 -->
        <canvas class="pre-canvas" canvas-id="firstCanvas" :style="{ width: 100 + 'vw', height: 100 + 'vh' }"></canvas>
        <!-- img预览 -->
        <view class="preImage" :style="{ width: preImgW + 'px' }">
            <canvas canvas-id="mycanvas" class="pre-i"
                :style="{ width: preImgW + 'px', height: preImgH + 'px', transform: `translate(${x}px,${y}px)` }"></canvas>
        </view>
        <!-- 工具 -->
        <view class="setting-btns"><text @click="onCrop">确定</text></view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                maxW: 250, // 最大宽度
                maxH: 250,
                screenWidth: '', // 屏幕宽
                screenHeight: '',
                xToTop: 0, // x方向距离顶部距离
                scale: 1, // 缩放
                preSrc: '',
                preImgW: '',
                preImgH: '',
                x: 0,
                y: 0,
                oldx: 0,
                oldy: 0,
                isMove: false,
                start: {
                    left: 0,
                    top: 0
                }
            };
        },
        computed: {},
        onLoad(option) {
            // 选择照片信息
            let data = JSON.parse(decodeURIComponent(option.item));
            const query = uni.createSelectorQuery();
            query.select('.settingHeadImage').boundingClientRect();
            query.exec(res => {
                // 设置屏幕大小
                this.screenWidth = res[0].width;
                this.screenHeight = res[0].height;
                // 设置图像基准值,图像基准值按屏幕宽度设置,两边留白各40
                this.maxH = res[0].width - 80;
                this.maxW = res[0].width - 80;
                // 设置X轴值,算式:屏幕高度的一半减去基准框高度的一半
                this.xToTop = this.screenHeight / 2 - this.maxH / 2;
                this.setImageSize(data);
            });
        },
        methods: {
            // 宽高处理
            setImageSize(data) {
                const {
                    tempFilePath
                } = data;
                const {
                    maxH,
                    maxW
                } = this;
                uni.getImageInfo({
                    src: tempFilePath,
                    success: res => {
                        const {
                            errMsg,
                            path,
                            width,
                            height
                        } = res;
                        let w = '';
                        let h = '';
                        if (errMsg === 'getImageInfo:ok') {
                            w = width;
                            h = height;
                            // 宽大与高大于最大宽度
                            if (width > height && width > maxW) {
                                w = Math.floor((width / height) * maxW);
                                h = maxH;
                            }
                            // 高大于宽大于最大高度
                            if (height > width && height > maxH) {
                                h = Math.floor((height / width) * maxH);
                                w = maxW;
                            }
                            // 宽高相等或者宽高小于最大值
                            if (width === height || (width < maxW && height < maxH)) {
                                h = maxH;
                                w = maxW;
                            }
                            this.preSrc = path;
                            this.preImgH = h;
                            this.preImgW = w;
                            // 设置蒙层
                            this.setBgBox();
                            // 图像预览
                            this.previewCanvas({
                                w,
                                h,
                                path
                            });
                        }
                    }
                });
            },
 
            // 设置蒙层
            setBgBox() {
                const {
                    maxW,
                    maxH,
                    screenHeight,
                    screenWidth,
                    xToTop
                } = this;
                const ctx = uni.createCanvasContext('firstCanvas');
                // 先清除矩形
                ctx.clearRect(0, 0, screenWidth, screenHeight);
                // 设置canvas透明度
                ctx.setGlobalAlpha(0.7);
                // 设置蒙层颜色
                ctx.setFillStyle('#000000');
                // 绘制蒙层
                ctx.fillRect(0, 0, screenWidth, screenHeight);
                // 基准框留白
                ctx.clearRect(40, xToTop, maxW, maxH);
                // 绘制基准框
                ctx.beginPath();
                ctx.setStrokeStyle('#FFFFFF');
                ctx.strokeRect(40, xToTop, maxW, maxH);
                ctx.closePath();
                ctx.draw();
            },
            // 预览
            previewCanvas({
                w,
                h,
                path
            }) {
                const ctx = uni.createCanvasContext('mycanvas');
                ctx.drawImage(path, 0, 0, w, h);
                ctx.draw();
            },
            onTouchstart(ev) {
                if (ev.changedTouches.length === 1) {
                    this.isMove = true;
                    this.start.left = ev.changedTouches[0].clientX;
                    this.start.top = ev.changedTouches[0].clientY;
                }
            },
            onTouchmove(ev) {
                const {
                    maxW,
                    maxH,
                    preImgH,
                    preImgW,
                    xToTop
                } = this;
                if (this.isMove && ev.changedTouches.length === 1) {
                    let clientX = ev.changedTouches[0].clientX,
                        clientY = ev.changedTouches[0].clientY;
                    this.x = preImgW <= maxW ? 0 : this.oldx + clientX - this.start.left;
                    this.y = preImgH <= maxH ? 0 : this.oldy + clientY - this.start.top;
                }
            },
            touchE(val) {
                const {
                    preImgH,
                    preImgW,
                    maxH,
                    maxW
                } = this;
                const query = uni.createSelectorQuery();
                const fx = this.x;
                query.select('.pre-i').boundingClientRect();
                query.exec(res => {
                    // x、y回弹计算
                    let y = (res[0].height - res[0].width) / 2;
                    /**
                     * 判断照片可移动的距离是否大于留白的值,如果大于向右划时图片的宽减去基准框的宽减去留白向左时留白,小于时按图片的可移动值
                     * */ 
                    let x = (preImgW - maxW) / 2 > 40 ? (fx < 0 ? preImgW - maxW - 40 : 40) : (preImgW - maxW) / 2;
                    if (preImgH > maxH) {
                        this.y = this.y > y ? y : this.y < -y ? -y : this.y;
                    }
                    if (preImgW > maxW) {
                        this.x = this.x > x ? x : this.x < -x ? -x : this.x;
                    }
                    this.oldx = this.x;
                    this.oldy = this.y;
                    this.isMove = false;
                });
            },
            // 裁剪
            onCrop() {
                let y = 0;
                let x = 0;
                const query = uni.createSelectorQuery();
                query.select('.pre-i').boundingClientRect();
                query.exec(res => {
                    // 获取预览img距离左上的距离
                    y = Math.abs(res[0].top);
                    x = Math.abs(res[0].left);
                    const {
                        maxW,
                        maxH,
                        preImgH,
                        preImgW,
                        xToTop
                    } = this;
                    uni.canvasToTempFilePath({
                        x: Math.abs(res[0].left < 0 ? x + 40 : x - 40),
                        y: Math.abs(res[0].top < 0 ? xToTop + y : xToTop - y),
                        width: maxW,
                        height: maxH,
                        destWidth: maxW,
                        destHeight: maxH,
                        canvasId: 'mycanvas',
                        success: fileRes => {
                            console.log(fileRes);
                            uni.previewImage({
                                count: 1,
                                urls: [fileRes.tempFilePath]
                            });
                        },
                        fail: function(err) {
                            console.log(err);
                            uni.showToast({
                                title: '上传失败:图片生成过程中遇到错误',
                                icon: 'none'
                            });
                        }
                    });
                });
            }
        }
    };
</script>
 
<style lang="scss" scoped>
    .settingHeadImage {
        background-color: #000000;
        overflow: hidden;
 
        .pre-canvas {
            position: fixed;
            top: 0;
            left: 0;
            z-index: 20;
        }
 
        .preImage {
            min-width: 100vw;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            z-index: 1;
 
            .pre-i {
                // transition: all 0.1s;
            }
        }
 
        .setting-btns {
            position: fixed;
            bottom: 0;
            left: 0;
            z-index: 20;
            font-size: 14px;
            color: #ffffff;
        }
    }
</style>