-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTouchControlRotate.ts
More file actions
197 lines (164 loc) · 4.56 KB
/
TouchControlRotate.ts
File metadata and controls
197 lines (164 loc) · 4.56 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
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
/**
* Copyright (c) 2017 Alibaba Group Holding Limited
*/
import Hammer from 'hammerjs'
import { CameraProxy } from './CameraProxy'
/**
* @experimental
*/
export interface TouchControlRotateProps {
/**
* 被控制的 CameraProxy
*/
camera: CameraProxy
/**
* 监控事件的HTML元素
*/
element: HTMLElement
/**
* 只在水平(地平面)方向上运动,如果设为false,则在视觉平面上运动
* [=true]
*/
horizontal?: boolean
/**
* 画布缩放比例
*/
scale?: number
revert?: boolean
}
const defaultProps = {
horizontal: true,
scale: 1.0,
}
export class TouchControlRotate {
private camera: CameraProxy
private elm: HTMLElement
private hammer: HammerManager
/**
* 画布缩放比例
*/
public scale = 1.0
constructor(props: TouchControlRotateProps) {
props = { ...defaultProps, ...props }
this.camera = props.camera
this.elm = props.element
document.body.addEventListener(
'touchmove',
function (e) {
e.preventDefault() //阻止默认的处理方式(阻止下拉滑动的效果)
},
{ passive: false }
) //passive 参数不能省略,用来兼容ios和android
this.hammer = new Hammer(this.elm, {})
const rotate = new Hammer.Rotate()
const pan = new Hammer.Pan()
const pinch = new Hammer.Pinch()
this.hammer.add([pan, pinch, rotate])
pan.set({
pointers: 1,
})
let deltaX = 0
let deltaY = 0
let panning = false // 尝试 fix hammer的bug
this.hammer.on('panstart', (e) => {
// console.log('panstart', e)
deltaX = e.deltaX
deltaY = e.deltaY
panning = true
})
this.hammer.on('panend', (e) => {
// console.log('panend', e)
panning = false
})
this.hammer.on('pan', (e) => {
if (panning) {
const dx = e.deltaX - deltaX
const dy = e.deltaY - deltaY
deltaX = e.deltaX
deltaY = e.deltaY
const scale = (Math.pow(2, this.camera.zoom) / 156000) * this.scale
// this.camera.goRight(-dx / scale, props.horizontal)
// this.camera.goUp(dy / scale, props.horizontal)
this.camera.pan(-dx / scale, dy / scale, props.horizontal)
}
})
// create a pinch and rotate recognizer
// these require 2 pointers
// pan.recognizeWith([pinch, rotate])
// pan.recognizeWith(pinch)
// pan.requireFailure(rotate)
// we want to detect both the same time
pinch.recognizeWith(rotate)
// pinch.recognizeWith(pan)
let rotation = 0
let pitchDeltaY = 0
let scale = 1
this.hammer.on('pinchstart', (e) => {
rotation = e.rotation
pitchDeltaY = e.deltaY
scale = e.scale
//
// deltaX = e.deltaX
// deltaY = e.deltaY
})
this.hammer.on('pinch', (e) => {
// rotation
// const dRotateDeltaX = (e.deltaX - rotateDeltaX) * -0.01
// rotateDeltaX = e.deltaX
// this.camera.setRotation(this.camera.rotation + dRotateDeltaX)
// 这种控制方案交互不方便
// const drotation = (e.rotation - rotation) * 0.01
// // fix hammer bug
// // 如果指头不保持贴在屏幕上,这里会出现角度变化
// if (Math.abs(drotation) < 1) {
// rotation = e.rotation
// this.camera.setRotation(this.camera.rotation + drotation)
// } else {
// console.log('忽略rotate', drotation)
// }
// pitch
const dPitchDeltaY = (e.deltaY - pitchDeltaY) * -0.01
pitchDeltaY = e.deltaY
this.camera.setPitch(this.camera.pitch + dPitchDeltaY)
// zoom
const dZoom = e.scale - scale
scale = e.scale
this.camera.setZoom(this.camera.zoom + dZoom)
// if (panning) {
// const dx = e.deltaX - deltaX
// const dy = e.deltaY - deltaY
// deltaX = e.deltaX
// deltaY = e.deltaY
// const scale = Math.pow(2, this.camera.zoom) / 156000
// this.camera.goRight(-dx / scale)
// this.camera.goUp(dy / scale)
// }
//
// {
// const dx = e.deltaX - deltaX
// const dy = e.deltaY - deltaY
// deltaX = e.deltaX
// deltaY = e.deltaY
// const scale = (Math.pow(2, this.camera.zoom) / 156000) * this.scale
// // this.camera.goRight(-dx / scale, props.horizontal)
// // this.camera.goUp(dy / scale, props.horizontal)
// this.camera.pan(-dx / scale, dy / scale, props.horizontal)
// }
})
this.hammer.on('rotate', (e) => {
// 这种控制方案交互不方便
const drotation = (e.rotation - rotation) * 0.02 * (props.revert ? -1 : 1)
// fix hammer bug
// 如果指头不保持贴在屏幕上,这里会出现角度变化
if (Math.abs(drotation) < 1) {
rotation = e.rotation
this.camera.setRotation(this.camera.rotation + drotation)
} else {
console.log('忽略rotate', drotation)
}
})
}
dispose() {
this.hammer.destroy()
}
}