zippy-game-engine
Version:
A lightweight game engine for web games
42 lines • 1.32 kB
JavaScript
export class TouchSystem {
touches;
constructor() {
this.touches = new Map();
}
setupCanvasEvents(canvas) {
const options = { passive: false, capture: false };
canvas.addEventListener("touchstart", (e) => {
this.#handleTouchEvent(e, canvas);
e.preventDefault();
}, options);
canvas.addEventListener("touchmove", (e) => {
this.#handleTouchEvent(e, canvas);
e.preventDefault();
}, options);
canvas.addEventListener("touchend", (e) => {
for (const touch of Array.from(e.changedTouches)) {
this.touches.delete(touch.identifier);
}
e.preventDefault();
}, options);
}
#handleTouchEvent(e, canvas) {
for (const touch of Array.from(e.changedTouches)) {
const rect = canvas.getBoundingClientRect();
this.touches.set(touch.identifier, {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
});
}
}
getCount() {
return this.touches.size;
}
getPositions() {
return Array.from(this.touches.values());
}
getPosition(id) {
return this.touches.get(id) || null;
}
}
//# sourceMappingURL=touch-system.js.map