# Pointers and touch

> [!NOTE]
> New in 0.10.

A surface works with a mouse, a pen, a touchscreen and a touchpad. Most of it
needs nothing from your program: CSS adapts the look to the device, and the
host scrolls, taps and long-presses on its own. This page covers what your
program can hear when it wants more:

- which kind of pointer the user is using;
- a drag for each finger;
- two fingers on an element as one gesture: a pinch, a turn, a two-finger pan;
- the wheel over an element, in pixels;

and how a canvas app, such as a map or a whiteboard, puts them together.
[Input](https://hotty.neuroplast.io/docs/concepts/input) covers clicks, presses and drags in general.

## Styling for touch

The media features `pointer`, `any-pointer`, `hover` and `any-hover` follow
the device the host draws on, as they do in a browser
([§8](https://hotty.neuroplast.io/docs/protocol#8-the-host-stylesheet)). A phone is `pointer: coarse` and
`hover: none`. A laptop with a touchpad and a touchscreen is `pointer: fine`
and `hover: hover`, with `any-pointer: coarse`. When the host cannot tell, it
reports a mouse.

```css
button { min-height: 1lh; }
@media (pointer: coarse) {
  button { min-height: 44px; padding-inline: 1em; }
}
@media (hover: none) {
  .row .actions { visibility: visible; }  /* no hover to reveal them */
}
```

Your program hears nothing of this, and there is no capability to check. When
the user plugs in a mouse or docks a tablet, the host redraws every surface, as
it does for a new theme. A surface that a multiplexer shows on a phone and on a
desktop at once matches each device: big rows on the phone, fine ones at the
desk, from one document.

## The kind of pointer

A `click`, `press`, `contextmenu`, `dragstart`, `gesturestart` and `wheel`
carry `pointer`: `"mouse"`, `"pen"` or `"touch"`, the names Pointer Events
use ([§9](https://hotty.neuroplast.io/docs/protocol#9-events)):

```text
host → ESC ] 7279 ; a=ev:s=list:e=click:t=row-3 ; {"area": {"c": 0, "r": 3, "w": 40, "h": 1}, "pointer": "touch"} ST
```

- **A touchpad is `"mouse"`:** its clicks, its scrolls and its pinches, as in
  the DOM.
- **The keyboard is no pointer.** A click from Enter or Space, and a
  `contextmenu` from the menu key, carry no `pointer`.
- **The rest of a gesture** belongs to the event that began it: `drag` and
  `dragend` carry no `pointer`, and neither do `gesture` and `gestureend`.
- **Take a kind you don't know as `"mouse"`.** Later versions may add kinds.

### The `pointer` event

A program that draws in its cells gets mouse reports that say nothing about
the device. It can still adapt: the terminal sends a `pointer` event when the
user presses, touches or scrolls with another kind of pointer than the one
your program last heard of, and once for the first input after the query:

```text
host → ESC ] 7279 ; a=ev:e=pointer ; {"pointer": "touch"} ST
```

It belongs to no surface, so it has no `s` or `t`. It counts input over your
surfaces and your cells alike, including a touch that only scrolls the
scrollback. A move alone counts for nothing, and neither does the keyboard.
The event comes before whatever the input itself makes (a `press`, a mouse
report, a `wheel`), so your program can switch before it handles the input.

Use it for hints and for targets in the cells: "hold for options" for touch,
"right-click" for a mouse, wider hit areas under a finger. Until the first
`pointer` event, draw for a mouse. Each query starts over: the next input sends
`pointer` again, since a new query often means a new program.

## Fingers and drags

A touch drags an element only where the element opts out of panning, as in a
browser ([Input](https://hotty.neuroplast.io/docs/concepts/input#touch-on-a-surface)). An element with
`data-on="drag"` and `touch-action: none` drags in any direction. One with
`touch-action: pan-y` drags across and still lets a vertical swipe scroll.

When a first move goes as far across as down, it pans if the `touch-action`
allows a pan along either axis, and drags under `none`.

### A drag per finger

Each finger is decided on its own while the others drag or are still
undecided. Two thumbs can drag two faders, or a joystick and a fire button, at
the same time. Every event of a touch's drag carries `finger`: the lowest
number no other finger down holds, so `0` for the first finger.

```html
<div id=fader-a data-on=drag data-steps="0 100" style="touch-action: none">…</div>
<div id=fader-b data-on=drag data-steps="0 100" style="touch-action: none">…</div>
```

```text
host → ESC ] 7279 ; a=ev:s=mixer:e=dragstart:t=fader-a ; {"c": 2, "r": 8, "y": 40, "pointer": "touch", "finger": 0} ST
host → ESC ] 7279 ; a=ev:s=mixer:e=dragstart:t=fader-b ; {"c": 9, "r": 6, "y": 25, "pointer": "touch", "finger": 1} ST
host → ESC ] 7279 ; a=ev:s=mixer:e=drag:t=fader-a ; {"c": 2, "r": 7, "y": 47, "finger": 0} ST
host → ESC ] 7279 ; a=ev:s=mixer:e=dragend:t=fader-b ; {"c": 9, "r": 5, "y": 31, "finger": 1} ST
```

- **Key drags by `s` and `finger`,** not by the surface alone. A mouse's and a
  pen's drags have no `finger`.
- **A finger never pans beside a drag.** A finger that would pan while another
  drags does nothing until it lifts: it neither scrolls nor reaches the
  terminal.
- **A tap among fingers is a click.** A finger that lifts quickly while others
  are down is a press and a click of what it touched, which the host makes
  (a browser makes none).
- **Once a finger pans, the rest are the host's.** Fingers that touch while one
  pans, or are still undecided when one starts to pan, do nothing on any
  surface until every finger lifts. Two fingers there pinch-zoom or scroll as
  the host does.

### A drag cut short

A `dragend` with `"cut": true` means something other than the user's release
ended the drag:

- the placement went away, or a new document replaced the surface's;
- the host lost the pointer, or the platform cancelled a finger's touch;
- a gesture took the finger over (below).

```text
host → ESC ] 7279 ; a=ev:s=board:e=dragend ; {"c": 14, "r": 6, "finger": 0, "cut": true} ST
```

A release off every element also has no `t`, so check `cut` to tell them
apart. A whiteboard drops a stroke that a pinch cut short, and keeps one the
user let go of.

## Gestures

Add `gesture` to an element's `data-on`, with an `id`, and two fingers on it
become one gesture ([§9.8](https://hotty.neuroplast.io/docs/protocol#98-gestures)). From its transform you
take what you need: a pinch's scale, a turn's angle, or the moving centre of a
two-finger pan.

```html
<div id=chart data-on=gesture style="touch-action: pan-y">…</div>
```

```text
host → ESC ] 7279 ; a=ev:s=stats:e=gesturestart:t=chart ; {"c": 20, "r": 5, "fingers": 2, "pointer": "touch"} ST
host → ESC ] 7279 ; a=ev:s=stats:e=gesture:t=chart ; {"scale": 1.4, "angle": -3.5, "c": 21, "r": 5, "fingers": 2} ST
host → ESC ] 7279 ; a=ev:s=stats:e=gestureend:t=chart ; {"scale": 1.8, "angle": -4.0, "c": 22, "r": 6, "fingers": 2} ST
```

- **`scale`** is the fingers' spread over their spread at the start: `2` for
  fingers twice as far apart.
- **`angle`** is how far they have turned since the start, in degrees,
  clockwise, and counted on past a full turn: a turn and a half is `540`.
- **`c` and `r`** are the cell under the fingers' centre. On an element with
  `data-steps`, `x` and `y` give the centre's step too.
- **`fingers`** is how many fingers are down. A third finger joins the
  gesture, and one that lifts leaves it; `scale` and `angle` carry on without
  a jump.
- **Every `gesture` holds the whole transform since the start,** never a step,
  so a missed event loses nothing. Apply it to the view as it was at
  `gesturestart`.
- **`gesture` comes at most once a frame.** `gestureend` comes once, when fewer
  than two fingers are left, or when the placement goes away, the document is
  replaced, or the host loses a finger.

A gesture starts once a second finger touches and one of the two moves past
the tap slop. A second finger that lifts before anything moved is a tap, and
the first finger goes on as it was.

- **It takes over.** If the first finger was dragging, its drag ends first,
  with a `dragend` with `cut`. If it was scrolling, the scroll stops where it
  is. So a map pans with one finger and pinches with two, and a whiteboard's
  finger draws until a second lands.
- **After it,** with one finger left, the surface hears nothing more of the
  touch until every finger lifts.
- **The host gives up two fingers.** Over an element with `gesture`, a pinch
  does not zoom and a two-finger pan does not scroll.
- **One finger is unchanged.** `touch-action` still decides what a single
  finger does: the chart above scrolls the page with one finger and pinches
  with two.
- **Fingers on two surfaces** make no gesture: each is a finger of its own.

### A touchpad's pinch

On a native host, a touchpad's pinch or rotation over the element is the same
gesture, with `"pointer": "mouse"` and `fingers` 2. It starts when the platform
says the pinch began, with no slop, and its centre starts at the pointer. In a
browser, a touchpad's pinch reaches the page as the wheel with Ctrl held, so
it arrives as a `wheel` (below). [Hosts](https://hotty.neuroplast.io/docs/hosts) says which host does
what.

## The wheel

Add `wheel` to an element's `data-on`, with an `id`, and the wheel over it is
your program's, in pixels ([§9.9](https://hotty.neuroplast.io/docs/protocol#99-the-wheel)). Nothing
scrolls there, not even the document around it, and nothing reaches the
terminal. Ctrl with the wheel is the element's too, whatever the host would do
with it elsewhere.

```text
host → ESC ] 7279 ; a=ev:s=map:e=wheel:t=map ; {"dx": 0.0, "dy": 36.5, "c": 30, "r": 9, "pointer": "mouse", "precise": true} ST
```

- **`dx` and `dy`** are the CSS pixels the wheel would have scrolled, positive
  to the right and down. A wheel that scrolls by lines counts a line as a
  cell's height (down) or width (across).
- **`precise`** says how the device scrolls: `true` for pixels (a touchpad, or
  a mouse wheel that spins freely), `false` for a wheel's notches. When the
  platform doesn't say, the host guesses as browsers do, and keeps its first
  guess until the gesture ends.
- **`mods`** are the keys held, Ctrl included. `c` and `r` are the pointer's
  cell, and on an element with `data-steps`, `x` and `y` its step.
- **`pointer`** is always `"mouse"`: a wheel and a touchpad alike.
- **At most one a frame.** The host sums the moves in between. A change of
  element, of `mods` or of `precise` sends what was summed first.
- **A scroll stays where it began.** One that started over the element stays
  its own until it ends, and one that started elsewhere doesn't become its.
- **No momentum of the host's.** After a flick of a touchpad, the platform's
  own momentum arrives as more `wheel` events. The host adds none.

A touch is no wheel: a finger's pan over the element is a drag or a gesture, as
above.

## A canvas that pans and zooms

A map, a whiteboard or a zoomable chart takes all of this on one element:

```html
<div id=map data-on="drag gesture wheel" data-steps="640 384"
     style="width: 640px; height: 384px; touch-action: none">…</div>
```

`data-steps` equal to the element's size in CSS pixels makes every drag, wheel
and gesture report the pointer's position to the pixel, as `x` and `y`. Size it
from the cell size the host reports (`cellcss` in the capabilities), and size
it again when a `caps` event changes it: the user's zoom changes the cell, and
on some hosts so does moving the window to another display.

What each input does is the canvas's choice. A common one:

| input | it comes as | the canvas |
| --- | --- | --- |
| a drag with the mouse, a pen, or one finger | `dragstart`, `drag`, `dragend`, by `finger` | pans, or draws with the current tool |
| a touchpad's two-finger scroll | `wheel` with `precise: true` | pans by `dx`, `dy` |
| a mouse wheel's notches | `wheel` with `precise: false` | zooms a step per notch, about the pointer |
| Ctrl with the wheel | `wheel` with Ctrl in `mods` | zooms about the pointer |
| a touchpad's pinch in a browser | `wheel` with Ctrl in `mods` | zooms about the pointer |
| a touchpad's pinch on a native host | `gesture`, `pointer` `"mouse"` | zooms by `scale` about the centre |
| two fingers on a touchscreen | `gesture`, `pointer` `"touch"` | zooms by `scale`, pans with the centre |

- **Zoom on notches, pan on pixels.** A notched wheel has no good way to pan
  both ways, and a touchpad's two fingers are a natural pan. `precise` tells
  them apart, and stays the same through a gesture, so a swipe never switches
  from panning to zooming halfway.
- **Sensitivity is yours.** A touchpad's pixels at 1:1 feel slow on a large
  drawing. Scale `dx`, `dy` and a touchpad's `scale` by a factor of your own,
  and let the user change it. A touchscreen's pinch should stay under the
  fingers, at 1:1.
- **A gesture applies to the view at its start.** Keep the camera from
  `gesturestart`, and set the camera on each `gesture` from it: zoom by `scale`
  about the starting centre, then move by how far the centre moved.
- **A cut stroke is dropped.** When a second finger turns a drawing finger into
  a pinch, its `dragend` has `cut`: remove the mark it began.
- **Offer keys and controls too.** A 0.9 host sends no `wheel` and no
  gestures, and there two fingers are the host's. Zoom and pan keys, or
  buttons, work everywhere.

## Bounce and coast

A host may let a touchpad's or a finger's scroll go a little past the edge of a
scrolling document and spring back, and let a fling coast after the fingers
lift, as on a phone ([§5.3](https://hotty.neuroplast.io/docs/protocol#53-geometry)). A wheel's notches do
neither. `overscroll-behavior: none` turns the bounce off for a box, and
`contain` keeps it from chaining outward; reduced motion turns both off.
Your program hears nothing of the bounce: a coast is a scroll like any other,
and `view` comes once it rests.

## In a 0.9 session

A program that asks only for 0.9 hears 0.9's events, even on a 0.10 host: no
`pointer` event or field, no `finger` or `cut`, and `gesture` and `wheel` in
`data-on` are not read, so two fingers and the wheel are the host's there. A
second finger ends a touch's drag, as before. The media features still follow
the device, in every session. See [Degrading](https://hotty.neuroplast.io/docs/concepts/degrading#versions).
