# misc/cameragestures (vanilla.js 0.4.0)

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

Camera gestures. (gestures that rotate, pan and zoom a 3D board or map, mouse right and middle button drag, two fingers)

## CameraGestures

Camera gestures. (gestures that rotate, pan and zoom a 3D board or map, mouse right and middle button drag, two fingers)
- The engine's input only passes press, drag and release of one finger (or any mouse button). Rotate, pan and zoom gestures are outside that, so the canvas DOM events are read separately. (the engine's behavior is left as is)
  - Dragging with the right mouse button held: rotates by the horizontal drag (the screen width is one full turn) and tilts by the vertical drag. Releasing without dragging is a right click.
  - Dragging with the middle mouse button held: pans.
  - Two fingers: dragging in the same direction pans, pinching in and out zooms in and out around the midpoint of the two fingers, and twisting past the twist threshold rotates. (prevents slight rotation while pinch-zooming)
- The wheel is passed by the engine. Here it only records whether Ctrl was held. (laptop touchpad pinches also arrive as Ctrl wheel)
- During a gesture, isGestureActive signals not to use the presses and releases passed by the engine for gameplay.
- The accumulated amounts (pan, rotate, tilt, zoom) are taken out every frame with take. Pan is in view units, rotate and tilt in radians, zoom is a ratio to multiply.
- Also reads whether the last input was a finger or a mouse. Mouse events the browser emulates right after a finger is lifted are ignored.
- Constructing attaches events to the engine's canvas. When done, dispose().

Example: Rotate, pan and zoom a 2D camera with two-finger and mouse gestures

```js
import { Engine, EngineConfiguration, Scene, CameraGestures, Camera2D, Rect, Colors } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const cameraGestures = new CameraGestures();
const camera = new Camera2D();

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const viewManager = engine.getViewManager();
		const viewSize = viewManager.getViewSize();
		camera.setViewSize(viewSize);
		const pan = cameraGestures.takePan();
		camera.panByScreen(pan.x, pan.y);
		const rotation = cameraGestures.takeRotation();
		const currentRotation = camera.getRotation();
		camera.setRotation(currentRotation + rotation * 180 / Math.PI);
		const zoom = cameraGestures.takeZoom();
		const zoomAnchor = cameraGestures.getZoomAnchor();
		if (zoom !== 1 && zoomAnchor !== null) {
			camera.zoomAt(zoomAnchor, zoom);
		}
		camera.tick(timeDelta);
	}

	draw(graphic) {
		camera.begin(graphic);
		graphic.setFillColor(Colors.white);
		graphic.drawRect(Rect.create(-100, -100, 200, 200));
		camera.end(graphic);
		super.draw(graphic);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		if (cameraGestures.isGestureActive()) {
			return;
		}
		console.log("tap", viewInputPosition.x, viewInputPosition.y);
	}
}

engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): Constructor. (attaches events to the canvas, dispose when done)
- `addListener(target, type, handler, options)` (method, since 0.4.0): Attaches DOM events. (dispose detaches them)
  - `target` EventTarget: Target to attach the event to.
  - `type` string: Event name.
  - `handler` Function: Event handler function.
  - `options` object | boolean: options of addEventListener. (dispose passes the same when removing it)
- `dispose()` (method, since 0.4.0): Disposes. (detaches the attached events)
- `isGestureActive()` (method, since 0.4.0): Whether a gesture is in progress. (the right or middle button is held or two fingers are down, during which the engine's presses are not used for gameplay)
  - returns boolean: True during a gesture.
- `isTouchInput()` (method, since 0.4.0): Whether the last input was a finger. (initially, whether the device's primary input is touch)
  - returns boolean: True if the last input was a finger.
- `isCoarsePointer()` (method, since 0.4.0): Whether the device's primary input is touch. (phones and tablets)
  - returns boolean: True on a device whose primary input is a finger. (false if matchMedia is unavailable)
- `isLastWheelCtrl()` (method, since 0.4.0): Whether the last wheel was a wheel with Ctrl held. (even with wheel panning enabled, that wheel zooms)
  - returns boolean: True if the last wheel was a wheel with Ctrl held.
- `takePan()` (method, since 0.4.0): Take the gathered pan. (Vector2 in view units, emptied once taken)
  - returns Vector2: The gathered pan. (x and y are in view units)
- `takeRotation()` (method, since 0.4.0): Takes the accumulated rotation. (radians, cleared when taken)
  - returns number: Accumulated rotation. (radians)
- `takeTilt()` (method, since 0.4.0): Takes the accumulated tilt. (radians, cleared when taken)
  - returns number: Accumulated tilt. (radians, + is toward looking from above)
- `takeZoom()` (method, since 0.4.0): Takes the accumulated zoom. (ratio to multiply, reset to 1 when taken)
  - returns number: Accumulated zoom. (ratio to multiply, 1 means unchanged)
- `getZoomAnchor()` (method, since 0.4.0): Zoom center. (view coordinates, midpoint of the two fingers, null if none)
  - returns Vector2 | null: Center of the zoom. (view coordinates, null if never zoomed with two fingers)
- `takeRightClick()` (method, since 0.4.0): Takes whether a right click occurred. (right button released without dragging, cleared when taken)
  - returns boolean: True if there was a right click.
- `takeRotationReleased()` (method, since 0.4.0): Takes whether rotation was released. (cleared when taken)
  - returns boolean: True if the rotation was released. (when the right button is released or both fingers are lifted)
- `readUnitsPerCssPixel()` (method, since 0.4.0): How many view units one CSS pixel is. (same as a pt on phones)
  - returns number: Size of one CSS pixel in view units. (1 if there is no engine)
- `readViewPosition(clientX, clientY)` (method, since 0.4.0): Browser window position to view coordinates. (same as how the engine converts press positions, null if there is no engine)
  - `clientX` number: X relative to the browser window. (CSS pixels)
  - `clientY` number: Y relative to the browser window. (CSS pixels)
  - returns Vector2 | null: View coordinates. (null if there is no engine or canvas)
- `isCompatibilityMouse()` (method, since 0.4.0): Whether it is a mouse event right after a finger was lifted. (emulated by the browser)
  - returns boolean: True if within the ignore time after the last finger event.
- `handleMouseDown(mouseEvent)` (method, since 0.4.0): Mouse press. (the right button starts rotating, the middle button starts panning)
  - `mouseEvent` MouseEvent: Browser mouse event.
- `readMouseDragCursor()` (method, since 0.4.0): Mouse drag cursor. (grabbing while rotating with the right button or panning with the middle button, grab if the right button is held but not yet dragged, otherwise an empty string)
  - returns string: CSS cursor name. ("grabbing", "grab", empty string)
- `handleMouseMove(mouseEvent)` (method, since 0.4.0): Mouse move. (with the right button held, rotates by the horizontal movement and tilts by the vertical movement, with the middle button pans)
  - `mouseEvent` MouseEvent: Browser mouse event.
- `handleMouseUp(mouseEvent)` (method, since 0.4.0): Mouse release. (the right button ends rotating, a right click if not dragged, the middle button ends panning)
  - `mouseEvent` MouseEvent: Browser mouse event.
- `handleWheel(wheelEvent)` (method, since 0.4.0): Wheel. (only records whether Ctrl was held, zoom and pan are done by the screen from the wheel passed by the engine)
  - `wheelEvent` WheelEvent: Browser wheel event.
- `handleTouchChange(touchEvent)` (method, since 0.4.0): Finger count changed. (two or more starts a gesture, lifting all ends it)
  - `touchEvent` TouchEvent: Browser touch event.
- `captureTouchPose(touches)` (method, since 0.4.0): Captures the midpoint, angle and distance of the two fingers as the reference.
  - `touches` TouchList: List of fingers currently touching. (uses the first two)
- `handleTouchMove(touchEvent)` (method, since 0.4.0): Two fingers moved. (pans by how much the midpoint moved, zooms by how much the distance changed, rotates when twisted past the threshold)
  - `touchEvent` TouchEvent: Browser touch event.
- `setThresholds(twistStartRadians, tiltPerHalfHeightRadians, rightClickDistance, compatibilityMouseMilliseconds)` (method, since 0.4.0): Sets the thresholds. (angle to start twisting, tilt angle when dragging half the screen height, distance counted as a right click, milliseconds to ignore emulated mouse)
  - `twistStartRadians` number: Angle at which a two-finger twist starts rotating. (radians, default 15 degrees)
  - `tiltPerHalfHeightRadians` number: Tilt angle when a right drag moves vertically by half the screen height. (radians, default 35 degrees)
  - `rightClickDistance` number: Releasing the right button within this distance counts as a right click. (CSS pixels, default 8)
  - `compatibilityMouseMilliseconds` number: Time to ignore emulated mouse events after a finger is lifted. (milliseconds, default 800)

  Example: Set thresholds converted from degrees

  ```js
  import { CameraGestures } from "./build/vanilla.js";
  
  const cameraGestures = new CameraGestures();
  const twistStartRadians = 10 * Math.PI / 180;
  const tiltPerHalfHeightRadians = 45 * Math.PI / 180;
  cameraGestures.setThresholds(twistStartRadians, tiltPerHalfHeightRadians, 6, 600);
  console.log(cameraGestures.isGestureActive(), cameraGestures.takeZoom());
  cameraGestures.dispose();
  ```

