ts-useful
Version:
Functions for animation, color transitions, ecliptic, bezier, decasteljau, curves, three dimensional curves, smooth scrolling, random range, randomItem, mobius index, vectors, physics vectors, and easing.
59 lines • 2.29 kB
JavaScript
export class Swipe {
handleTouchStart(event) {
const initTouch = event.touches[0];
this.x1 = initTouch.clientX;
this.y1 = initTouch.clientY;
}
handleTouchMove(event) {
if (!this.x1 || !this.y1) {
return;
}
const nowTouch = event.touches[0];
const [x2, y2] = [nowTouch.clientX, nowTouch.clientY];
const [xDiff, yDiff] = [this.x1 - x2, this.y1 - y2];
if (Math.abs(xDiff) > Math.abs(yDiff)) {
if (xDiff > 0) {
this.swipeEvents.left ? this.swipeEvents.left() : () => { };
}
else {
this.swipeEvents.right ? this.swipeEvents.right() : () => { };
}
}
else {
if (yDiff > 0) {
this.swipeEvents.up ? this.swipeEvents.up() : () => { };
}
else {
this.swipeEvents.down ? this.swipeEvents.down() : () => { };
}
}
[this.x1, this.y1] = [null, null];
}
assignSwipe(events) {
return {
left: !events.left ? this.defaultSwipe.left : events.left,
right: !events.right ? this.defaultSwipe.right : events.right,
up: !events.up ? this.defaultSwipe.up : events.up,
down: !events.down ? this.defaultSwipe.down : events.down
};
}
Destroy() {
if (this.elm) {
this.elm.removeEventListener('touchstart', this.handleTouchStart, { capture: false });
this.elm.removeEventListener('touchmove', this.handleTouchMove, { capture: false });
}
}
SetSwipe(events) {
this.swipeEvents = this.assignSwipe(events);
}
constructor(element, events) {
this.x1 = 0;
this.y1 = 0;
this.defaultSwipe = { left: () => { }, right: () => { }, up: () => { }, down: () => { } };
this.swipeEvents = events ? this.assignSwipe(events) : this.defaultSwipe;
this.elm = element;
this.elm.addEventListener('touchstart', (e) => this.handleTouchStart(e), { passive: true });
this.elm.addEventListener('touchmove', (e) => this.handleTouchMove(e), { passive: true });
}
}
//# sourceMappingURL=swipe.js.map