zoom-pan.vue
2.36 KB
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
<template>
<view>
<movable-area
class="container zoom-pan-container"
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchEnd="handleTouchEnd"
>
<movable-view
class="content"
:style="{'transform-origin': transformOriginStyle}"
:scale="!disabled"
:direction="scale == 1?'none':'all'"
:scale-min="scaleMin"
:scale-max="scaleMax"
:disabled="disabled"
@scale="onScale"
:scale-value="defalutscale"
>
<slot name="content"></slot>
</movable-view>
</movable-area>
</view>
</template>
<script>
import utils from "@/utils/utils.js";
export default {
props: {
disabled: Boolean,
scaleMax: {
type: Number,
default: 10
},
scaleMin: {
type: Number,
default: 1
},
},
data () {
return {
scale: 1,
defalutscale: 1,
isScaling: false,
transformOriginStyle: '50% 50%'
};
},
watch: {
watchStatus: {
immediate: true,
handler (newval) {
if (this.scale != 1) this.defalutscale = 1.1
this.scale = 1
this.transformOriginStyle = `50% 50%'`
setTimeout(() => {
this.defalutscale = 1
}, 100)
}
}
},
computed: {
watchStatus () {
return this.$store.state.user.historyStatus
}
},
methods: {
handleTouchStart (e) {
if (this.disabled) return
if (e.touches.length === 2) this.isScaling = true;
},
handleTouchMove: utils.throttle(function (e) {
if (this.disabled) return
if (this.isScaling && e.touches.length === 2) {
const center = this.getTouchCenter(e.touches)
this.transformOriginStyle = `${center.x}px ${center.y}px`;
}
}, 16),
handleTouchEnd () {
this.isScaling = false
},
onScale (e) {
this.scale = e.detail.scale
this.$emit('scale', e.detail)
},
getTouchCenter (touches) {
const centerX = (touches[0].pageX + touches[1].pageX) / 2;
const centerY = (touches[0].pageY + touches[1].pageY) / 2;
return { x: centerX, y: centerY };
},
},
};
</script>
<style>
.zoom-pan-container {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
.content {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.disabled {
pointer-events: none;
}
</style>