Add swipe-click handler to media modal

Now swiping will correctly change the current media, and with a good
preview. Clicking without swiping closes the overlay.
This commit is contained in:
Tusooa Zhu 2021-08-02 19:11:59 -04:00
parent a36673a6a8
commit 29cd8fbd3b
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
5 changed files with 94 additions and 168 deletions

View file

@ -4,25 +4,27 @@ const DIRECTION_RIGHT = [1, 0]
const DIRECTION_UP = [0, -1]
const DIRECTION_DOWN = [0, 1]
const DISTANCE_MIN = 1
// const DISTANCE_MIN = 1
const isSwipeEvent = e => (e.touches.length === 1)
const isSwipeEventEnd = e => (e.changedTouches.length === 1)
// const isSwipeEvent = e => (e.touches.length === 1)
// const isSwipeEventEnd = e => (e.changedTouches.length === 1)
const isScaleEvent = e => (e.targetTouches.length === 2)
const isScaleEventEnd = e => (e.targetTouches.length === 1)
// const isScaleEvent = e => (e.targetTouches.length === 2)
// const isScaleEventEnd = e => (e.targetTouches.length === 1)
const deltaCoord = (oldCoord, newCoord) => [newCoord[0] - oldCoord[0], newCoord[1] - oldCoord[1]]
const vectorMinus = (a, b) => a.map((k, n) => k - b[n])
const vectorAdd = (a, b) => a.map((k, n) => k + b[n])
// const vectorMinus = (a, b) => a.map((k, n) => k - b[n])
// const vectorAdd = (a, b) => a.map((k, n) => k + b[n])
const avgCoord = (coords) => [...coords].reduce(vectorAdd, [0, 0]).map(d => d / coords.length)
// const avgCoord = (coords) => [...coords].reduce(vectorAdd, [0, 0]).map(d => d / coords.length)
const touchCoord = touch => [touch.screenX, touch.screenY]
const touchEventCoord = e => touchCoord(e.touches[0])
const pointerEventCoord = e => [e.clientX, e.clientY]
const vectorLength = v => Math.sqrt(v[0] * v[0] + v[1] * v[1])
const perpendicular = v => [v[1], -v[0]]
@ -78,104 +80,87 @@ const updateSwipe = (event, gesture) => {
gesture._swiping = false
}
class SwipeAndScaleGesture {
class SwipeAndClickGesture {
// swipePreviewCallback(offsets: Array[Number])
// offsets: the offset vector which the underlying component should move, from the starting position
// pinchPreviewCallback(offsets: Array[Number], scaling: Number)
// offsets: the offset vector which the underlying component should move, from the starting position
// scaling: the scaling factor we should apply to the underlying component, from the starting position
// swipeEndcallback(sign: 0|-1|1)
// swipeEndCallback(sign: 0|-1|1)
// sign: if the swipe does not meet the threshold, 0
// if the swipe meets the threshold in the positive direction, 1
// if the swipe meets the threshold in the negative direction, -1
constructor ({
direction,
// swipeStartCallback, pinchStartCallback,
swipePreviewCallback, pinchPreviewCallback,
swipeEndCallback, pinchEndCallback,
// swipeStartCallback
swipePreviewCallback,
swipeEndCallback,
swipeCancelCallback,
swipelessClickCallback,
threshold = 30, perpendicularTolerance = 1.0
}) {
const nop = () => { console.log('Warning: Not implemented') }
this.direction = direction
this.swipePreviewCallback = swipePreviewCallback || nop
this.pinchPreviewCallback = pinchPreviewCallback || nop
this.swipeEndCallback = swipeEndCallback || nop
this.pinchEndCallback = pinchEndCallback || nop
this.swipeCancelCallback = swipeCancelCallback || nop
this.swipelessClickCallback = swipelessClickCallback || nop
this.threshold = threshold
this.perpendicularTolerance = perpendicularTolerance
this._reset()
}
_reset () {
this._startPos = [0, 0]
this._startDistance = DISTANCE_MIN
this._pointerId = -1
this._swiping = false
this._swiped = false
}
start (event) {
console.log('start() called', event)
if (isSwipeEvent(event)) {
this._startPos = touchEventCoord(event)
console.log('start pos:', this._startPos)
this._swiping = true
} else if (isScaleEvent(event)) {
const coords = [...event.targetTouches].map(touchCoord)
this._startPos = avgCoord(coords)
this._startDistance = vectorLength(deltaCoord(coords[0], coords[1]))
if (this._startDistance < DISTANCE_MIN) {
this._startDistance = DISTANCE_MIN
}
this._scalePoints = [...event.targetTouches]
this._swiping = false
console.log(
'is scale event, start =', this._startPos,
'dist =', this._startDistance)
}
this._startPos = pointerEventCoord(event)
this._pointerId = event.pointerId
console.log('start pos:', this._startPos)
this._swiping = true
this._swiped = false
}
move (event) {
// console.log('move called', event)
if (isSwipeEvent(event)) {
const touch = event.changedTouches[0]
const delta = deltaCoord(this._startPos, touchCoord(touch))
if (this._swiping && this._pointerId === event.pointerId) {
this._swiped = true
const coord = pointerEventCoord(event)
const delta = deltaCoord(this._startPos, coord)
this.swipePreviewCallback(delta)
} else if (isScaleEvent(event)) {
console.log('is scale event')
const coords = [...event.targetTouches].map(touchCoord)
const curPos = avgCoord(coords)
const curDistance = vectorLength(deltaCoord(coords[0], coords[1]))
const scaling = curDistance / this._startDistance
const posDiff = vectorMinus(curPos, this._startPos)
// const delta = vectorAdd(numProduct((1 - scaling), this._startPos), posDiff)
const delta = posDiff
// console.log(
// 'is scale event, cur =', curPos,
// 'dist =', curDistance,
// 'scale =', scaling,
// 'delta =', delta)
this.pinchPreviewCallback(delta, scaling)
}
}
end (event) {
console.log('end() called', event)
if (isScaleEventEnd(event)) {
this.pinchEndCallback()
}
if (!isSwipeEventEnd(event)) {
console.log('not swipe event')
cancel (event) {
if (!this._swiping || this._pointerId !== event.pointerId) {
return
}
this.swipeCancelCallback()
}
end (event) {
if (!this._swiping) {
console.log('not swiping')
return
}
this.swiping = false
console.log('is swipe event')
if (this._pointerId !== event.pointerId) {
console.log('pointer id does not match')
return
}
this._swiping = false
console.log('end: is swipe event')
// movement too small
const touch = event.changedTouches[0]
const delta = deltaCoord(this._startPos, touchCoord(touch))
this.swipePreviewCallback(delta)
const coord = pointerEventCoord(event)
const delta = deltaCoord(this._startPos, coord)
const sign = (() => {
if (vectorLength(delta) < this.threshold) {
@ -198,7 +183,12 @@ class SwipeAndScaleGesture {
return isPositive ? 1 : -1
})()
this.swipeEndCallback(sign)
if (this._swiped) {
this.swipeEndCallback(sign)
} else {
this.swipelessClickCallback()
}
this._reset()
}
}
@ -210,7 +200,7 @@ const GestureService = {
swipeGesture,
beginSwipe,
updateSwipe,
SwipeAndScaleGesture
SwipeAndClickGesture
}
export default GestureService