Create a Swipe Tracker
See original GitHub issueI made this same issue previously, but on an alternate account where I would not be subscribed properly to this issue. I’ve remade it.
It would be useful to have a feature that allows you to track swipes on a mobile device. I’ve created my own using Excaliburs’ current features. I’m not sure if it’s suitable for the engines’ scope, however. The code is below for reference, but I believe that a utility for swipe gestures would be handy.
The math isn’t fantastic in this Swipe Tracker either. I’m still playing around with it.
import { Vector, Class, Engine, GameEvent } from "excalibur";
import { PointerDownEvent, PointerMoveEvent, PointerUpEvent } from "excalibur/dist/Input";
export enum STSensitivity {
High,
Medium,
Low
}
export enum SwipeDirection {
NONE,
UP,
RIGHT,
DOWN,
LEFT
}
export class SwipeEvent extends GameEvent<any> {
constructor(public direction: SwipeDirection) {
super();
}
}
export default class SwipeTracker extends Class {
public sensitivity: STSensitivity;
get swipeTimeDelay(): number {
return this.sensitivity == STSensitivity.High ? 50 : STSensitivity.Medium ? 100 : 200;
}
get swipePositionThreshold(): number {
return this.sensitivity == STSensitivity.High ? 80 : STSensitivity.Medium ? 120 : 180;
}
private _timerComplete: Boolean;
private _initialPointerPosition: Vector;
private _swipeEventEmitted: Boolean;
constructor(_engine : Engine) {
super();
this.sensitivity = STSensitivity.High;
_engine.input.pointers.primary.on('down', (pe) => {
let castPE = pe as PointerDownEvent;
this._initialPointerPosition = new Vector(castPE.screenPos.x, castPE.screenPos.x);
this._swipeEventEmitted = false;
this._timerComplete = false;
setTimeout(() => {
this._timerComplete = true
}, this.swipeTimeDelay);
});
_engine.input.pointers.primary.on('move', (pe) => {
let castPE = pe as PointerMoveEvent;
this.emitSwipeIfNecessary(castPE);
});
_engine.input.pointers.primary.on('up', (pe) => {
let castPE = pe as PointerMoveEvent;
this.emitSwipeIfNecessary(castPE);
});
}
private emitSwipeIfNecessary( pe: PointerMoveEvent | PointerUpEvent) {
let swipeDirection = SwipeDirection.NONE;
if (this._timerComplete && !this._swipeEventEmitted) {
if (pe.screenPos.x > this._initialPointerPosition.x + this.swipePositionThreshold) {
swipeDirection = SwipeDirection.RIGHT;
} else if (pe.screenPos.x < this._initialPointerPosition.x - this.swipePositionThreshold) {
swipeDirection = SwipeDirection.LEFT;
} else if (pe.screenPos.y > this._initialPointerPosition.y + this.swipePositionThreshold) {
swipeDirection = SwipeDirection.DOWN;
} else if (pe.screenPos.y < this._initialPointerPosition.y - this.swipePositionThreshold) {
swipeDirection = SwipeDirection.UP;
}
if (swipeDirection != undefined) {
this.emit('swipe', new SwipeEvent(swipeDirection));
this._swipeEventEmitted = true;
}
}
}
}
Usage:
let tracker = new SwipeTracker(_engine);
tracker.on('swipe', (sw: SwipeEvent) => { console.log(sw.direction) })
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Element swipe tracking in GTM and GA4 -
In this post, I will share one way how we can track slider swipe events in GTM and GA4. Since those event types...
Read more >Google Tag Manager: tracking mobile swipes
Learn how to track mobile swipe gestures via GTM. Enrich your data set and obtain valuable insigths about your users' behavior to improve ......
Read more >Swipe Pages: Landing Page Builder
Quickly create insanely fast, mobile optimized AMP landing pages without any code using Swipe Pages, the landing page builder for ROI driven marketers....
Read more >Swipe Cards and Attendance Tracking: What You Need to Know
Swipe cards are one type of employee attendance tracking technology that can help businesses effectively monitor and follow the attendance ...
Read more >Build High Converting Landing Pages with Swipe Pages
If you're in the market for an easy tool to build eye-catching and high converting lading pages - Check out Swipe Pages in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@justjoeyuk As for working on the engine while building a game I’ve done a couple approaches
I’ll make an issue later to update our readme to include this in our documentation 👍
This issue hasn’t had any recent activity lately and is being marked as stale automatically.