riku
2024-08-12 65124213af664a68ad88ce7f6dcb133116d7702f
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
// component/mygallery/mygallery.js
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    imgUrls: {
      type: Array,
      value: [],
 
      observer(newVal) {
        this.setData({
          currentImgs: newVal
        });
      }
 
    },
    title: {
      type: String,
      value: ''
    },
    remark: {
      type: String,
      value: ''
    },
    show: {
      type: Boolean,
      value: true,
 
      observer(newVal) {
        if (newVal) {
          let that = this;
          let query = this.createSelectorQuery().in(this); //必须要先创建一个查询
          setTimeout(() => {
            query.select('.fyui-gallery__img').boundingClientRect(function (rect) {
              that.setData({
                imgHeight: rect.height,
                imgWidth: rect.width,
                xLimit: [0, 0],
                yLimit: [0, 0],
              });
            }).exec();
          }, 500);
        }
      }
    },
    current: {
      type: Number,
      value: 0
    },
    hideOnClick: {
      type: Boolean,
      value: true
    },
    extClass: {
      type: String,
      value: ''
    }
  },
  data: {
    currentImgs: [],
    tabList: [],
    pageList: [],
    showRemark: true,
 
    imgHeight: 0, //图片高度
    imgWidth: 0, //图片宽度
    xLimit: [], //缩放后x轴移动范围
    yLimit: [], //缩放后y轴移动范围
    translateX: 0, // 位移x坐标 单位px
    translateY: 0, // 位移y坐标 单位px
    distance: undefined, // 双指接触点距离
    scale: 1, // 缩放倍数
    endScale: 1,
    rotate: 0, // 旋转角度
    oldRotate: 0, // 上一次旋转停止后的角度
    startMove: { // 起始位移距离
      x: 0,
      y: 0,
    },
    startTouches: [] // 起始点touch数组
  },
 
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () { 
 
    },
  },
 
  ready() {
    const data = this.data;
    this.setData({
      currentImgs: data.imgUrls
    });
  },
 
  methods: {
    change(e) {
      this.setData({
        current: e.detail.current
      });
      this.triggerEvent('change', {
        current: e.detail.current
      }, {});
 
      //图片复位
      this.setData({
        scale: 1,
        translateX: 0,
        translateY: 0,
        rotate: 0
      })
    },
 
    deleteImg() {
      const data = this.data;
      const imgs = data.currentImgs;
      const url = imgs.splice(data.current, 1);
      this.triggerEvent('delete', {
        url: url[0],
        index: data.current
      }, {});
 
      if (imgs.length === 0) {
        // @ts-ignore
        this.hideGallery();
        return;
      }
 
      this.setData({
        current: 0,
        currentImgs: imgs
      });
    },
 
    hideGallery() {
      const data = this.data;
 
      if (data.hideOnClick) {
        this.setData({
          show: false,
          scale: 1,
          endScale: 1,
          showRemark: true,
          translateX: 0,
          translateY: 0,
          rotate: 0
        });
        this.triggerEvent('hide', {}, {});
      }
    },
 
    /**
     * 底部图片导航栏选择事件
     */
    swichNav(e) {
      var that = this;
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        that.setData({
          current: e.target.dataset.current,
        })
      }
    },
 
    /**
     * 底部图片导航栏、文本显隐事件
     */
    toggle() {
      const showRemark = !this.data.showRemark
      this.setData({
        showRemark
      })
    },
 
    rotateImg(e) {
      const clockwise = e.currentTarget.dataset.clockwise
      let rotate = this.data.rotate
      if (clockwise) {
        rotate += 90
      } else {
        rotate -= 90
      }
      rotate = rotate > 360 ? rotate - 360 : rotate
      rotate = rotate < 0 ? rotate + 360 : rotate
 
      const imgWidth = this.data.imgHeight
      const imgHeight = this.data.imgWidth
      this.setData({rotate, imgWidth, imgHeight})
    },
 
    touchStart(e) {
      const touches = e.touches
      const { translateX, translateY } = this.data
      const { clientX, clientY } = touches[0]
      this.setData({
        startMove:{
          x: clientX - translateX,
          y: clientY - translateY
        }
      })
      this.setData({
        startTouches:touches,
      })
    },
    
    touchMove(e) {
      const touches = e.touches
      const { clientX: onePageX, clientY: onePageY } = touches[0]
      const { startMove, scale, distance: oldDistance, startTouches, oldRotate  } = this.data
      if (touches.length === 2 && startTouches.length === 2) {
        // 双指缩放
        const { pageX: twoPageX, pageY: twoPageY } = touches[1]
        // 求出当前双指距离
        const distance = Math.sqrt((twoPageX - onePageX) ** 2 + (twoPageY - onePageY) ** 2)
        this.setData({distance})
 
        // 双指旋转
        let rotate = this.getAngle(touches[0], touches[1]) - this.getAngle(startTouches[0], startTouches[1]) + oldRotate
        // 如果大于360度,就减去360
        rotate = rotate > 360 ? rotate - 360 : rotate
 
        this.setData({
          scale: scale * (distance / (oldDistance || distance)),
          // rotate
        })
      } else if (scale > 1 && startTouches.length !== 2) {
        // 单指拖拽
        // const xL = this.data.xLimit
        // const yL = this.data.yLimit
        let x = onePageX - startMove.x
        let y = onePageY - startMove.y
        // if (x < xL[0]) {
        //   x = xL[0]
        // } else if (x > xL[1]) {
        //   x = xL[1]
        // }
        // if (y < yL[0]) {
        //   y = yL[0]
        // } else if (y > yL[1]) {
        //   y = yL[1]
        // }
        this.setData({
          translateX: x,
          translateY: y
        })
      }
    },
    getAngle(p1, p2) {
      const x = p1.clientX - p2.clientX
      const y = p1.clientY- p2.clientY
      return Math.atan2(y, x) * 180 / Math.PI
    },
    touchEnd() {
      // 保存当前旋转角度,清空双指距离
      const oldRotate = this.data.rotate
      this.setData({ 
        oldRotate,
        distance: undefined,
       })
      // 最小缩放为1倍,同时图片移动复位
      if (this.data.scale < 1) {
        this.setData({
          scale: 1,
          translateX: 0,
          translateY: 0
        })
      }
 
      // 保存最后缩放的倍率
      const s = this.data.scale
      this.setData({
        endScale: s,
      })
 
      // 计算当前缩放倍率下,图片可移动范围
      const h = this.data.imgHeight
      const w = this.data.imgWidth
      const sH = h * s
      const sW = w * s
      const rH = (sH - h) / 2
      const rW = (sW - w) / 2
      const xLimit = [-rW, rW]
      const yLimit = [-rH, rH]
      this.setData({
        xLimit,
        yLimit
      })
 
      let that = this;
      let query = this.createSelectorQuery().in(this); //必须要先创建一个查询
      query.select('.fyui-gallery__img').boundingClientRect(function (rect) {
      }).exec();
 
      // 缩放后回归至当前移动位置
      let translateX = this.data.translateX
      let translateY = this.data.translateY
      const xL = this.data.xLimit
      const yL = this.data.yLimit
      if (translateX < xL[0]) {
        translateX = xL[0]
      } else if (translateX > xL[1]) {
        translateX = xL[1]
      }
      if (translateY < yL[0]) {
        translateY = yL[0]
      } else if (translateY > yL[1]) {
        translateY = yL[1]
      }
      this.setData({ translateX, translateY})
    },
    
  }
})