# misc/pointergesture (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/pointergesture/

포인터 제스처 인식기. (헤드리스)

## PointerGesture extends Object

포인터 제스처 인식기. (헤드리스)
- 탭 / 드래그 / 롱프레스를 임계값으로 분류한다. 노드에 얽매이지 않아 어디서나 쓴다.
- 사용: press(x, y) → movePointer(x, y) → tick(dt) → release() 순으로 넘기고,
  콜백(setTapEvent / setDragEvent / setLongPressEvent)으로 결과를 받는다.
- 여러 게임이 pressedTime / dragMoved 변수를 제각각 들고 다니던 것을 표준화했다.

예제: 탭, 드래그, 롱프레스 가려 받기

```js
import { PointerGesture } from "./build/vanilla.js";

const pointerGesture = new PointerGesture({ dragThreshold: 12, longPressSeconds: 0.6 });
pointerGesture.setTapEvent((position) => {
	console.log("tap", position.x, position.y);
});
pointerGesture.setDragEvent((currentPosition, pressPosition) => {
	console.log("drag", currentPosition.x - pressPosition.x);
});
pointerGesture.setDragEndEvent((currentPosition, pressPosition) => {
	console.log("drag end", currentPosition.x - pressPosition.x);
});
pointerGesture.setLongPressEvent((pressPosition) => {
	console.log("long press", pressPosition.x, pressPosition.y);
});

pointerGesture.press(100, 100);
pointerGesture.release();

pointerGesture.press(100, 100);
pointerGesture.movePointer(105, 100);
pointerGesture.movePointer(130, 100);
pointerGesture.release();

pointerGesture.press(50, 50);
pointerGesture.tick(0.3);
console.log(pointerGesture.getLongPressRatio());
pointerGesture.tick(0.3);
pointerGesture.release();
```

- `constructor(options = {})` (constructor, since 0.3.0): 생성.
  - `options` object: { dragThreshold = 12, longPressSeconds = 0.6 }
- `press(x, y)` (method, since 0.3.0): 누름.
  - `x` number: 포인터 X 좌표.
  - `y` number: 포인터 Y 좌표.
- `movePointer(x, y)` (method, since 0.4.0): 이동.
  - 누른 지점에서 임계값을 넘으면 드래그로 확정하고 이후 매 이동마다 dragEvent 를 부른다.
  - `x` number: 포인터 X 좌표.
  - `y` number: 포인터 Y 좌표.
- `tick(timeDelta)` (method, since 0.3.0): 갱신. (롱프레스 시간 누적)
  - 드래그로 확정되지 않은 채 longPressSeconds 를 넘기면 롱프레스를 한 번 알린다.
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `release()` (method, since 0.3.0): 뗌.
  - 드래그도 롱프레스도 아니었으면 탭으로 알린다.
- `cancel()` (method, since 0.3.0): 취소. (아무것도 알리지 않고 상태만 되돌린다)
- `getLongPressRatio()` (method, since 0.3.0): 롱프레스 진행 비율 반환. (게이지 표시용, 0~1)
  - returns number: 롱프레스 진행 비율. (0 ~ 1, 누르지 않았거나 드래그 중이면 0)
- `isPressed()` (method, since 0.3.0): 상태 조회.
  - returns boolean: 누르고 있으면 참.
- `isDragging()` (method, since 0.3.0): 드래그 중인지 여부.
  - returns boolean: 드래그로 확정되었으면 참.
- `setTapEvent(tapEvent)` (method, since 0.3.0): 콜백 설정.
  - `tapEvent` Function: (position) => void
- `setDragEvent(dragEvent)` (method, since 0.3.0): 드래그 알림 설정. (드래그로 확정된 뒤 이동마다)
  - `dragEvent` Function: (currentPosition, pressPosition) => void
- `setDragEndEvent(dragEndEvent)` (method, since 0.3.0): 드래그 끝 알림 설정. (드래그 중에 뗄 때)
  - `dragEndEvent` Function: (currentPosition, pressPosition) => void
- `setLongPressEvent(longPressEvent)` (method, since 0.3.0): 롱프레스 알림 설정. (드래그 없이 longPressSeconds 를 넘길 때 한 번)
  - `longPressEvent` Function: (pressPosition) => void
- `setDragThreshold(dragThreshold)` (method, since 0.3.0): 임계값 설정.
  - `dragThreshold` number: 드래그로 볼 최소 이동 거리. (press 와 같은 좌표 단위, 기본 12)
- `setLongPressSeconds(longPressSeconds)` (method, since 0.3.0): 길게 누름 판정 시간 설정. (초)
  - `longPressSeconds` number: 롱프레스로 볼 누름 시간. (초, 기본 0.6)
