# misc/pointergesture (vanilla.js 0.4.0)

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

Pointer gesture recognizer. (headless)

## PointerGesture extends Object

Pointer gesture recognizer. (headless)
- Classifies tap / drag / long press by thresholds. Not tied to nodes, so it can be used anywhere.
- Usage: pass press(x, y) → movePointer(x, y) → tick(dt) → release() in order,
  and receive the results through callbacks (setTapEvent / setDragEvent / setLongPressEvent).
- Standardizes the pressedTime / dragMoved variables that several games each carried around.

Example: Tell taps, drags and long presses apart

```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): Constructor.
  - `options` object: { dragThreshold = 12, longPressSeconds = 0.6 }
- `press(x, y)` (method, since 0.3.0): Press.
  - `x` number: Pointer X coordinate.
  - `y` number: Pointer Y coordinate.
- `movePointer(x, y)` (method, since 0.4.0): Move.
  - Once it exceeds the threshold from the press point, it is confirmed as a drag and dragEvent is called on every move after that.
  - `x` number: Pointer X coordinate.
  - `y` number: Pointer Y coordinate.
- `tick(timeDelta)` (method, since 0.3.0): Updates. (accumulates long press time)
  - If longPressSeconds passes without being confirmed as a drag, notifies a long press once.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `release()` (method, since 0.3.0): Release.
  - If it was neither a drag nor a long press, notifies a tap.
- `cancel()` (method, since 0.3.0): Cancels. (notifies nothing and only resets the state)
- `getLongPressRatio()` (method, since 0.3.0): Returns the long press progress ratio. (for gauge display, 0~1)
  - returns number: Long press progress ratio. (0 ~ 1, 0 if not pressed or while dragging)
- `isPressed()` (method, since 0.3.0): State query.
  - returns boolean: True while pressed.
- `isDragging()` (method, since 0.3.0): Whether dragging.
  - returns boolean: True if confirmed as a drag.
- `setTapEvent(tapEvent)` (method, since 0.3.0): Sets the callback.
  - `tapEvent` Function: (position) => void
- `setDragEvent(dragEvent)` (method, since 0.3.0): Sets the drag callback. (on every move after it is confirmed as a drag)
  - `dragEvent` Function: (currentPosition, pressPosition) => void
- `setDragEndEvent(dragEndEvent)` (method, since 0.3.0): Sets the drag end callback. (when released while dragging)
  - `dragEndEvent` Function: (currentPosition, pressPosition) => void
- `setLongPressEvent(longPressEvent)` (method, since 0.3.0): Sets the long press callback. (once, when longPressSeconds passes without dragging)
  - `longPressEvent` Function: (pressPosition) => void
- `setDragThreshold(dragThreshold)` (method, since 0.3.0): Sets the thresholds.
  - `dragThreshold` number: Minimum movement distance treated as a drag. (same coordinate units as press, default 12)
- `setLongPressSeconds(longPressSeconds)` (method, since 0.3.0): Sets the long press threshold time. (seconds)
  - `longPressSeconds` number: Press time treated as a long press. (seconds, default 0.6)
