summaryrefslogtreecommitdiff
path: root/packages/client/src/scripts/touch.ts
blob: 5251bc2e27e0c0b04e5576e1e2ec1275bfa62543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const isTouchSupported = 'maxTouchPoints' in navigator && navigator.maxTouchPoints > 0;

export let isTouchUsing = false;

export let isScreenTouching = false;

if (isTouchSupported) {
	window.addEventListener('touchstart', () => {
		// maxTouchPointsなどでの判定だけだと、「タッチ機能付きディスプレイを使っているがマウスでしか操作しない」場合にも
		// タッチで使っていると判定されてしまうため、実際に一度でもタッチされたらtrueにする
		isTouchUsing = true;

		isScreenTouching = true;
	}, { passive: true });
	
	window.addEventListener('touchend', () => {
		// 子要素のtouchstartイベントでstopPropagation()が呼ばれると親要素に伝搬されずタッチされたと判定されないため、
		// touchendイベントでもtouchstartイベントと同様にtrueにする
		isTouchUsing = true;

		isScreenTouching = false;
	}, { passive: true });
}