|
@@ -3,113 +3,112 @@ import { isWap } from '@/utils'
|
|
|
export default {
|
|
|
mounted(el, binding, vnode) {
|
|
|
if (!isWap()) return
|
|
|
- let type = binding.arg // 传入点击的类型
|
|
|
- let coordinate = {} // 记录坐标点的对象
|
|
|
- let timeOutTap
|
|
|
- let timeOutLong
|
|
|
- let scaleSize // 缩放尺寸
|
|
|
- let displacement = {} //移动的位移
|
|
|
- // 勾股定理计算距离
|
|
|
- function getDistance(bg, end) {
|
|
|
- return Math.sqrt(Math.pow(end.x - bg.x, 2) + Math.pow(end.y - bg.y, 2))
|
|
|
+ function point2D(x, y) {
|
|
|
+ return { x: x, y: y }
|
|
|
}
|
|
|
- // 点击开始的时候
|
|
|
- el.addEventListener(
|
|
|
- 'touchstart',
|
|
|
- function (e) {
|
|
|
- // 获取第一个手指点击的坐标
|
|
|
- let x = e.touches[0].pageX
|
|
|
- let y = e.touches[0].pageY
|
|
|
- // 如果点击的时间很长,那么点击的类型就是长按 --- longTouch
|
|
|
- timeOutLong = setTimeout(() => {
|
|
|
- timeOutLong = 0
|
|
|
- if (type === 'longTouch') {
|
|
|
- binding.value.func(binding.value.param)
|
|
|
- }
|
|
|
- }, 2000)
|
|
|
- timeOutTap = setTimeout(() => {
|
|
|
- timeOutTap = 0
|
|
|
- if (type === 'tap' && e.touches.length === 1) {
|
|
|
- binding.value.func(binding.value.param)
|
|
|
- }
|
|
|
- }, 200)
|
|
|
- // 如果是两个手指,而且类型是缩放 --- scaleTocuh
|
|
|
- if (e.touches.length > 1 && type === 'scaleTouch') {
|
|
|
- // 记录双指的间距长度
|
|
|
- coordinate.start = getDistance(
|
|
|
- {
|
|
|
- x: e.touches[0].pageX,
|
|
|
- y: e.touches[0].pageY
|
|
|
- },
|
|
|
- {
|
|
|
- x: e.touches[1].pageX,
|
|
|
- y: e.touches[1].pageY
|
|
|
- }
|
|
|
- )
|
|
|
- }
|
|
|
- // 如果是移动的话,还要记录下来最开始的位置,只能一个手指位移
|
|
|
- if (type === 'slideTouch' && e.touches.length == 1) {
|
|
|
- // debugger
|
|
|
- displacement.start = {
|
|
|
- x: e.touches[0].pageX,
|
|
|
- y: e.touches[0].pageY
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- false
|
|
|
- )
|
|
|
- el.addEventListener(
|
|
|
- 'touchmove',
|
|
|
- function (e) {
|
|
|
- clearTimeout(timeOutTap)
|
|
|
- clearTimeout(timeOutLong)
|
|
|
- timeOutTap = 0
|
|
|
- timeOutLong = 0
|
|
|
- // 如果是缩放类型
|
|
|
- if (type == 'scaleTouch' && e.touches.length === 2) {
|
|
|
- // 计算移动过程中的两个手指的距离
|
|
|
- coordinate.stop = getDistance(
|
|
|
- {
|
|
|
- x: e.touches[0].pageX,
|
|
|
- y: e.touches[0].pageY
|
|
|
- },
|
|
|
- {
|
|
|
- x: e.touches[1].pageX,
|
|
|
- y: e.touches[1].pageY
|
|
|
- }
|
|
|
- )
|
|
|
- // 设置缩放尺寸
|
|
|
- scaleSize = coordinate.stop / coordinate.start - 1
|
|
|
- // 这里设置图片不能无限放大与缩小
|
|
|
- // 这里设置放大缩小速度慢一点,所以 /4一下
|
|
|
- binding.value.func(scaleSize / 2, false)
|
|
|
- }
|
|
|
- // 如果是移动类型
|
|
|
- if (type == 'slideTouch' && e.touches.length === 1) {
|
|
|
- displacement.end = {
|
|
|
- x: e.changedTouches[0].pageX,
|
|
|
- y: e.changedTouches[0].pageY
|
|
|
- }
|
|
|
- binding.value.func({
|
|
|
- x: displacement.end.x - displacement.start.x,
|
|
|
- y: displacement.end.y - displacement.start.y,
|
|
|
- is_endMove: false
|
|
|
- })
|
|
|
- }
|
|
|
- },
|
|
|
- false
|
|
|
- )
|
|
|
- el.addEventListener(
|
|
|
- 'touchend',
|
|
|
- function (e) {
|
|
|
- if (type === 'scaleTouch') {
|
|
|
- binding.value.func(0, true)
|
|
|
- }
|
|
|
- if (type === 'slideTouch') {
|
|
|
- binding.value.func({ x: 0, y: 0, is_endMove: true })
|
|
|
+
|
|
|
+ var reqAnimationFrame = (function () {
|
|
|
+ return (
|
|
|
+ window[window.Hammer.prefixed(window, 'requestAnimationFrame')] ||
|
|
|
+ function (callback) {
|
|
|
+ window.setTimeout(callback, 1000 / 60)
|
|
|
}
|
|
|
- },
|
|
|
- false
|
|
|
- )
|
|
|
+ )
|
|
|
+ })()
|
|
|
+
|
|
|
+ var tMatrix = [1, 0, 0, 1, 0, 0] //x缩放,无,无,y缩放,x平移,y平移
|
|
|
+
|
|
|
+ var initScale = 1 //初始化scale
|
|
|
+ var mc = new window.Hammer.Manager(el)
|
|
|
+ var ticking = false
|
|
|
+ var poscenter = point2D(0, 0) //缓存双指的中心坐标
|
|
|
+ var duration = '' //设置过渡效果,用于双击缩放效果
|
|
|
+ var lastTranslate = point2D(0, 0) //记录上次的偏移值
|
|
|
+ var lastcenter = point2D(el.offsetWidth / 2, el.offsetHeight / 2) //图像的中心点,用于对比双指中心点
|
|
|
+
|
|
|
+ var center = lastcenter
|
|
|
+ mc.add(new window.Hammer.Pan({ threshold: 0, pointers: 1 }))
|
|
|
+ mc.add(new window.Hammer.Pinch({ threshold: 0 }))
|
|
|
+ mc.add(new window.Hammer.Tap({ event: 'doubletap', taps: 2 }))
|
|
|
+ mc.on('panmove', onPan)
|
|
|
+ mc.on('panstart', onPanStart)
|
|
|
+ mc.on('pinchmove', onPinch)
|
|
|
+ mc.on('pinchstart', onPinchStart)
|
|
|
+ mc.on('doubletap', onDoubleTap)
|
|
|
+
|
|
|
+ function onPanStart(ev) {
|
|
|
+ lastTranslate = point2D(tMatrix[4], tMatrix[5]) //缓存上一次的偏移值
|
|
|
+ }
|
|
|
+ function onPan(ev) {
|
|
|
+ duration = ''
|
|
|
+ el.className = ''
|
|
|
+ tMatrix[4] = lastTranslate.x + ev.deltaX
|
|
|
+ tMatrix[5] = lastTranslate.y + ev.deltaY
|
|
|
+ requestElementUpdate('onpan')
|
|
|
+ }
|
|
|
+ function onPinchStart(ev) {
|
|
|
+ duration = ''
|
|
|
+ lastTranslate = point2D(tMatrix[4], tMatrix[5]) //记录上一次的偏移值
|
|
|
+ initScale = tMatrix[0] || 1
|
|
|
+ poscenter = point2D(ev.center.x, ev.center.y)
|
|
|
+
|
|
|
+ lastcenter = point2D(center.x + lastTranslate.x, center.y + lastTranslate.y) //重新计算放大后的中心坐标
|
|
|
+ poscenter = point2D(ev.center.x - lastcenter.x, ev.center.y - lastcenter.y)
|
|
|
+ console.log('center', lastcenter.x, lastcenter.y)
|
|
|
+
|
|
|
+ requestElementUpdate('onpinchStart')
|
|
|
+ }
|
|
|
+ function onPinch(ev) {
|
|
|
+ var nowScale = (tMatrix[0] = tMatrix[3] = initScale * ev.scale)
|
|
|
+ var composscal = 1 - ev.scale
|
|
|
+ //tMatrix[4] = poscenter.x - ((poscenter.x - lastcenter.x) * ev.scale + lastcenter.x) + lastTranslate.x//最后加上上一次的偏移值
|
|
|
+ //tMatrix[5] = poscenter.y - ((poscenter.y - lastcenter.y) * ev.scale + lastcenter.y) + lastTranslate.y
|
|
|
+ tMatrix[4] = (1 - ev.scale) * poscenter.x + lastTranslate.x
|
|
|
+ tMatrix[5] = (1 - ev.scale) * poscenter.y + lastTranslate.y
|
|
|
+ requestElementUpdate('onpinch')
|
|
|
+ }
|
|
|
+
|
|
|
+ function onDoubleTap(ev) {
|
|
|
+ duration = '.3s ease all'
|
|
|
+ var nowScale = tMatrix[0]
|
|
|
+ if (nowScale != 1 || tMatrix[4] != 0) {
|
|
|
+ //scale不等于1,要重回1
|
|
|
+ tMatrix[0] = tMatrix[3] = 1
|
|
|
+ tMatrix[4] = tMatrix[5] = 0
|
|
|
+ } else {
|
|
|
+ var pointer = ev.center
|
|
|
+ var scale = 2
|
|
|
+ tMatrix[0] = tMatrix[3] = scale
|
|
|
+ //var last = point2D
|
|
|
+ //tMatrix[4] = pointer.x - ((pointer.x-lastcenter.x) * scale + lastcenter.x);
|
|
|
+ //tMatrix[5] = pointer.y - ((pointer.y-lastcenter.y) * scale + lastcenter.y);
|
|
|
+ tMatrix[4] = (1 - scale) * (pointer.x - center.x)
|
|
|
+ tMatrix[5] = (1 - scale) * (pointer.y - center.y)
|
|
|
+ }
|
|
|
+ requestElementUpdate('doubleTap')
|
|
|
+ }
|
|
|
+
|
|
|
+ function updateElementTransform() {
|
|
|
+ el.style.transition = duration
|
|
|
+ var tmp = tMatrix.join(',')
|
|
|
+ console.log(tmp)
|
|
|
+ el.style.transform = 'matrix(' + tmp + ')'
|
|
|
+ ticking = false
|
|
|
+ }
|
|
|
+
|
|
|
+ function requestElementUpdate() {
|
|
|
+ arguments && console.log(arguments[0])
|
|
|
+
|
|
|
+ if (!ticking) {
|
|
|
+ reqAnimationFrame(updateElementTransform)
|
|
|
+ ticking = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ 初始化设置
|
|
|
+ */
|
|
|
+
|
|
|
+ requestElementUpdate()
|
|
|
}
|
|
|
}
|