@ac-kit/ui-gesture 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +151 -0
- package/LICENSE +28 -0
- package/README.md +310 -2
- package/dist/_validate.d.ts +4 -0
- package/dist/_validate.d.ts.map +1 -0
- package/dist/_validate.js +10 -0
- package/dist/_validate.js.map +1 -0
- package/dist/dom/_claim.d.ts +8 -0
- package/dist/dom/_claim.d.ts.map +1 -0
- package/dist/dom/_claim.js +14 -0
- package/dist/dom/_claim.js.map +1 -0
- package/dist/dom/_pointer-engine.d.ts +121 -0
- package/dist/dom/_pointer-engine.d.ts.map +1 -0
- package/dist/dom/_pointer-engine.js +559 -0
- package/dist/dom/_pointer-engine.js.map +1 -0
- package/dist/dom/_press-driver.d.ts +39 -0
- package/dist/dom/_press-driver.d.ts.map +1 -0
- package/dist/dom/_press-driver.js +121 -0
- package/dist/dom/_press-driver.js.map +1 -0
- package/dist/dom/index.d.ts +5 -0
- package/dist/dom/index.d.ts.map +1 -0
- package/dist/dom/index.js +5 -0
- package/dist/dom/index.js.map +1 -0
- package/dist/dom/pointer-binding.d.ts +111 -0
- package/dist/dom/pointer-binding.d.ts.map +1 -0
- package/dist/dom/pointer-binding.js +35 -0
- package/dist/dom/pointer-binding.js.map +1 -0
- package/dist/dom/press-binding.d.ts +29 -0
- package/dist/dom/press-binding.d.ts.map +1 -0
- package/dist/dom/press-binding.js +53 -0
- package/dist/dom/press-binding.js.map +1 -0
- package/dist/dom/transform-binding.d.ts +81 -0
- package/dist/dom/transform-binding.d.ts.map +1 -0
- package/dist/dom/transform-binding.js +349 -0
- package/dist/dom/transform-binding.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/inertia/inertia.d.ts +32 -0
- package/dist/inertia/inertia.d.ts.map +1 -0
- package/dist/inertia/inertia.js +62 -0
- package/dist/inertia/inertia.js.map +1 -0
- package/dist/keys/key-step.d.ts +25 -0
- package/dist/keys/key-step.d.ts.map +1 -0
- package/dist/keys/key-step.js +30 -0
- package/dist/keys/key-step.js.map +1 -0
- package/dist/pointer/_slop.d.ts +26 -0
- package/dist/pointer/_slop.d.ts.map +1 -0
- package/dist/pointer/_slop.js +74 -0
- package/dist/pointer/_slop.js.map +1 -0
- package/dist/pointer/pointer-input.d.ts +61 -0
- package/dist/pointer/pointer-input.d.ts.map +1 -0
- package/dist/pointer/pointer-input.js +6 -0
- package/dist/pointer/pointer-input.js.map +1 -0
- package/dist/press/press-gesture.d.ts +282 -0
- package/dist/press/press-gesture.d.ts.map +1 -0
- package/dist/press/press-gesture.js +579 -0
- package/dist/press/press-gesture.js.map +1 -0
- package/dist/transform/transform-gesture.d.ts +174 -0
- package/dist/transform/transform-gesture.d.ts.map +1 -0
- package/dist/transform/transform-gesture.js +415 -0
- package/dist/transform/transform-gesture.js.map +1 -0
- package/dist/velocity/velocity-tracker.d.ts +69 -0
- package/dist/velocity/velocity-tracker.d.ts.map +1 -0
- package/dist/velocity/velocity-tracker.js +152 -0
- package/dist/velocity/velocity-tracker.js.map +1 -0
- package/dist/wheel/wheel.d.ts +115 -0
- package/dist/wheel/wheel.d.ts.map +1 -0
- package/dist/wheel/wheel.js +164 -0
- package/dist/wheel/wheel.js.map +1 -0
- package/package.json +79 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @ac-kit/ui-gesture
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [0b4fe43]
|
|
8
|
+
- Updated dependencies [0b4fe43]
|
|
9
|
+
- Updated dependencies [0b4fe43]
|
|
10
|
+
- Updated dependencies [0b4fe43]
|
|
11
|
+
- Updated dependencies [0b4fe43]
|
|
12
|
+
- Updated dependencies [0b4fe43]
|
|
13
|
+
- Updated dependencies [0b4fe43]
|
|
14
|
+
- Updated dependencies [0b4fe43]
|
|
15
|
+
- Updated dependencies [0b4fe43]
|
|
16
|
+
- Updated dependencies [0b4fe43]
|
|
17
|
+
- Updated dependencies [0b4fe43]
|
|
18
|
+
- Updated dependencies [0b4fe43]
|
|
19
|
+
- Updated dependencies [0b4fe43]
|
|
20
|
+
- Updated dependencies [0b4fe43]
|
|
21
|
+
- Updated dependencies [0b4fe43]
|
|
22
|
+
- Updated dependencies [0b4fe43]
|
|
23
|
+
- Updated dependencies [0b4fe43]
|
|
24
|
+
- Updated dependencies [0b4fe43]
|
|
25
|
+
- Updated dependencies [0b4fe43]
|
|
26
|
+
- Updated dependencies [0b4fe43]
|
|
27
|
+
- Updated dependencies [0b4fe43]
|
|
28
|
+
- Updated dependencies [0b4fe43]
|
|
29
|
+
- Updated dependencies [0b4fe43]
|
|
30
|
+
- Updated dependencies [0b4fe43]
|
|
31
|
+
- Updated dependencies [0b4fe43]
|
|
32
|
+
- Updated dependencies [0b4fe43]
|
|
33
|
+
- Updated dependencies [0b4fe43]
|
|
34
|
+
- Updated dependencies [0b4fe43]
|
|
35
|
+
- Updated dependencies [0b4fe43]
|
|
36
|
+
- Updated dependencies [0b4fe43]
|
|
37
|
+
- Updated dependencies [0b4fe43]
|
|
38
|
+
- Updated dependencies [0b4fe43]
|
|
39
|
+
- Updated dependencies [0b4fe43]
|
|
40
|
+
- Updated dependencies [0b4fe43]
|
|
41
|
+
- @ac-kit/core@0.13.0
|
|
42
|
+
- @ac-kit/data@0.5.0
|
|
43
|
+
- @ac-kit/math-linear@0.4.0
|
|
44
|
+
- @ac-kit/math-scalar@0.4.0
|
|
45
|
+
- @ac-kit/math-stats@0.6.0
|
|
46
|
+
|
|
47
|
+
## 0.1.0
|
|
48
|
+
|
|
49
|
+
### Minor Changes
|
|
50
|
+
|
|
51
|
+
- 34e8cfa: `@ac-kit/ui-controls` is removed from the repository, and its npm deprecation
|
|
52
|
+
is pending the publication of these three packages; its last published
|
|
53
|
+
versions stay installable. Its generic controls moved to
|
|
54
|
+
`@ac-kit/ui-common`, its piano keyboard and dBFS meter scale to
|
|
55
|
+
`@ac-kit/ui-music`, and its `TapGesture` to `@ac-kit/ui-gesture`'s
|
|
56
|
+
`PressGesture`. Each package's README has a "Coming from `@ac-kit/ui-controls`"
|
|
57
|
+
table naming every renamed export, and the one stylesheet of `ui-controls`
|
|
58
|
+
becomes `@ac-kit/ui-common/styles.css` followed by `@ac-kit/ui-music/styles.css`.
|
|
59
|
+
- 34e8cfa: First release. Recognise taps, double taps, long presses, drags and swipes of
|
|
60
|
+
one pointer, and pans, pinches and twists of several, from positions and times
|
|
61
|
+
you pass in; turn wheel events into pixels or zoom steps, arrow keys into pan
|
|
62
|
+
steps and a release velocity into inertia. Nothing reads the DOM or owns a
|
|
63
|
+
timer, so the same recognisers run under Node in a test and behind any UI
|
|
64
|
+
framework.
|
|
65
|
+
|
|
66
|
+
What a press is follows from the times of its events, not from when you call
|
|
67
|
+
`resolve`: an event first settles what fell due before it, a release is a
|
|
68
|
+
movement like any other (past the slop it is a drag, not a tap), a pointer that
|
|
69
|
+
stopped before the lift has no release velocity, and every settled tap is
|
|
70
|
+
reported once, never early and never dropped. `PressGesture` has one clock and
|
|
71
|
+
refuses a time before it with a `RangeError` that changes nothing, so clamp the
|
|
72
|
+
times of events and timers to the latest one you passed. A `ctrlKey` wheel under
|
|
73
|
+
50 px is a pinch, from 50 px a Ctrl-wheel; `wheelTransform`'s `action` defaults
|
|
74
|
+
to `"zoom"` only when omitted or `undefined`. `VelocityTracker` ends its sample
|
|
75
|
+
window at a gap longer than `stopAfter` (default 40 ms) between two consecutive
|
|
76
|
+
samples, as Flutter's `VelocityTracker` and Android's impulse strategy do: a
|
|
77
|
+
hold followed by a flick reports the flick's velocity, not a blend of both. A
|
|
78
|
+
transform of pointers whose coordinates sum beyond the double range keeps a
|
|
79
|
+
finite centroid and spread, and a movement beyond the double range is never
|
|
80
|
+
within a finite slop, a zero one included. `TransformGesture` allocates only
|
|
81
|
+
its update record per move.
|
|
82
|
+
|
|
83
|
+
Bind the recognisers to an element with `@ac-kit/ui-gesture/dom`:
|
|
84
|
+
`bindPressGesture` (press, drag, tap, double tap, long press),
|
|
85
|
+
`bindTransformGesture` (pan, pinch and twist, wheel and trackpad steps,
|
|
86
|
+
navigation keys, taps) and `bindPointers` (normalised pointers with capture,
|
|
87
|
+
for callers that track them themselves). Each returns a binding you dispose, or
|
|
88
|
+
whose `signal` you abort, to remove every listener, timer and pointer capture.
|
|
89
|
+
|
|
90
|
+
A pointer ends exactly once however it ends: a release, a `pointercancel`, a
|
|
91
|
+
capture lost before the release, a mouse that moves with no button down, or
|
|
92
|
+
`binding.cancel()`. Nested surfaces are arbitrated by claims: the innermost
|
|
93
|
+
binding that accepts a press owns it and the ones around it skip it, or share it
|
|
94
|
+
with `claimed: "share"`; `claimPointerEvent` does the same for a hand-written
|
|
95
|
+
handler. A press also cancels the browser's `dragstart` and `selectstart` while
|
|
96
|
+
it is down and the `click` that follows a drag, and a `contextmenu` during a
|
|
97
|
+
still touch press is the long press. `cooperative: true` leaves an unmodified
|
|
98
|
+
wheel and a single finger to the page and reports a hint instead; two fingers
|
|
99
|
+
move the view. `acceptStep` and `acceptKeyStep` leave a wheel, trackpad or key
|
|
100
|
+
step the caller would not act on to the page, uncancelled. A handler that
|
|
101
|
+
throws is reported out of band without leaving a timer, capture or claim
|
|
102
|
+
behind, and a non-finite sample is dropped. Error messages describe an
|
|
103
|
+
offending argument with `@ac-kit/core`'s `describeValue`.
|
|
104
|
+
|
|
105
|
+
### Patch Changes
|
|
106
|
+
|
|
107
|
+
- Updated dependencies [34e8cfa]
|
|
108
|
+
- Updated dependencies [34e8cfa]
|
|
109
|
+
- Updated dependencies [34e8cfa]
|
|
110
|
+
- Updated dependencies [34e8cfa]
|
|
111
|
+
- Updated dependencies [34e8cfa]
|
|
112
|
+
- Updated dependencies [34e8cfa]
|
|
113
|
+
- Updated dependencies [34e8cfa]
|
|
114
|
+
- Updated dependencies [34e8cfa]
|
|
115
|
+
- Updated dependencies [34e8cfa]
|
|
116
|
+
- Updated dependencies [34e8cfa]
|
|
117
|
+
- Updated dependencies [34e8cfa]
|
|
118
|
+
- Updated dependencies [34e8cfa]
|
|
119
|
+
- Updated dependencies [34e8cfa]
|
|
120
|
+
- Updated dependencies [34e8cfa]
|
|
121
|
+
- Updated dependencies [34e8cfa]
|
|
122
|
+
- Updated dependencies [34e8cfa]
|
|
123
|
+
- Updated dependencies [34e8cfa]
|
|
124
|
+
- Updated dependencies [34e8cfa]
|
|
125
|
+
- Updated dependencies [34e8cfa]
|
|
126
|
+
- Updated dependencies [34e8cfa]
|
|
127
|
+
- Updated dependencies [34e8cfa]
|
|
128
|
+
- Updated dependencies [34e8cfa]
|
|
129
|
+
- Updated dependencies [34e8cfa]
|
|
130
|
+
- Updated dependencies [34e8cfa]
|
|
131
|
+
- Updated dependencies [34e8cfa]
|
|
132
|
+
- Updated dependencies [34e8cfa]
|
|
133
|
+
- Updated dependencies [34e8cfa]
|
|
134
|
+
- Updated dependencies [34e8cfa]
|
|
135
|
+
- Updated dependencies [34e8cfa]
|
|
136
|
+
- Updated dependencies [34e8cfa]
|
|
137
|
+
- Updated dependencies [34e8cfa]
|
|
138
|
+
- Updated dependencies [34e8cfa]
|
|
139
|
+
- Updated dependencies [34e8cfa]
|
|
140
|
+
- Updated dependencies [34e8cfa]
|
|
141
|
+
- Updated dependencies [34e8cfa]
|
|
142
|
+
- Updated dependencies [34e8cfa]
|
|
143
|
+
- Updated dependencies [34e8cfa]
|
|
144
|
+
- Updated dependencies [34e8cfa]
|
|
145
|
+
- Updated dependencies [34e8cfa]
|
|
146
|
+
- Updated dependencies [34e8cfa]
|
|
147
|
+
- @ac-kit/math-stats@0.5.0
|
|
148
|
+
- @ac-kit/core@0.12.0
|
|
149
|
+
- @ac-kit/math-scalar@0.3.0
|
|
150
|
+
- @ac-kit/data@0.4.0
|
|
151
|
+
- @ac-kit/math-linear@0.3.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,311 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ac-kit/ui-gesture
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Recognise what a person does with a pointer, from positions and times you pass
|
|
4
|
+
in: taps, double taps, long presses, drags and swipes of one pointer; pans,
|
|
5
|
+
pinches and twists of several; the velocity a gesture was released with, and the
|
|
6
|
+
inertia that follows; wheel events as pixels or zoom steps; arrow keys as pan
|
|
7
|
+
steps.
|
|
8
|
+
|
|
9
|
+
The root is headless. It reads no DOM global, owns no timer and imports no
|
|
10
|
+
framework: every input is a plain record (`PointerSample`, `WheelInput`,
|
|
11
|
+
`KeyInput`), so the same recogniser runs under Node in a test, in a page behind
|
|
12
|
+
any UI framework, and in a worker. `@ac-kit/ui-gesture/dom` binds them to an
|
|
13
|
+
element (pointer capture, claims between nested surfaces, timers, wheel, keys,
|
|
14
|
+
hover). The point of a recogniser is to settle, in
|
|
15
|
+
one place, what a press that wobbles, a second finger that lands late or a
|
|
16
|
+
cancelled touch means; an application that handles `pointerdown` itself ends up
|
|
17
|
+
rewriting those rules per control, a little differently each time.
|
|
18
|
+
|
|
19
|
+
## Contents
|
|
20
|
+
|
|
21
|
+
| Symbol | What it does |
|
|
22
|
+
| -------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
23
|
+
| `PressGesture` | One pointer's tap, double tap, long press and drag (slop, axis lock, swipe), arbitrated together |
|
|
24
|
+
| `TransformGesture` | Up to `maxPointers` pointers as one pan, pinch and twist: centroid, spread ratio, angle |
|
|
25
|
+
| `VelocityTracker` | A pointer's release velocity, from a least-squares line through its last 100 ms |
|
|
26
|
+
| `inertiaOffset`, `inertiaDuration` | Where a released gesture coasts to, and for how long |
|
|
27
|
+
| `wheelPixels`, `wheelTransform` | A wheel event in pixels whatever its `deltaMode`; the pan or zoom step it asks for, trackpad pinch included |
|
|
28
|
+
| `keyStep` | An arrow key as a pan step, `+` and `-` as a zoom step |
|
|
29
|
+
| `POINTER_SLOP`, `WHEEL_*`, `PINCH_WHEEL_*` | The named defaults, each with the platform it was taken from |
|
|
30
|
+
| `PointerSample`, `PointerDown`, `TapEvent`, `DragUpdate`, … | The records that go in and come out |
|
|
31
|
+
|
|
32
|
+
Units are the caller's positions (CSS pixels, from a binding), y down; times in
|
|
33
|
+
milliseconds on one monotonic clock (`event.timeStamp` and `performance.now()`
|
|
34
|
+
share an origin); velocities in units per millisecond; angles in radians,
|
|
35
|
+
positive clockwise on screen.
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { PressGesture } from "@ac-kit/ui-gesture";
|
|
41
|
+
|
|
42
|
+
const press = new PressGesture({ doubleTap: true, longPress: true });
|
|
43
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
44
|
+
|
|
45
|
+
// One clock for events and timers: a timer can run before an older event is
|
|
46
|
+
// delivered, and the recogniser never lets a time go back.
|
|
47
|
+
let clock = 0;
|
|
48
|
+
const timeOf = (time: number): number => (clock = Math.max(clock, time));
|
|
49
|
+
|
|
50
|
+
// Reports what fell due, in order: a long press, a tap that can no longer
|
|
51
|
+
// become a double one.
|
|
52
|
+
function flush(time: number): void {
|
|
53
|
+
for (
|
|
54
|
+
let due = press.resolve(time);
|
|
55
|
+
due.kind !== "none";
|
|
56
|
+
due = press.resolve(time)
|
|
57
|
+
) {
|
|
58
|
+
console.log(due.kind); // "single-tap" or "long-press"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// A recogniser owns no timer: after every call, ask when something falls due.
|
|
63
|
+
function schedule(): void {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
const deadline = press.deadline;
|
|
66
|
+
if (deadline === undefined) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
timer = setTimeout(
|
|
70
|
+
() => {
|
|
71
|
+
flush(timeOf(deadline));
|
|
72
|
+
schedule();
|
|
73
|
+
},
|
|
74
|
+
Math.max(0, deadline - performance.now()),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
element.addEventListener("pointerdown", (event) => {
|
|
79
|
+
const time = timeOf(event.timeStamp);
|
|
80
|
+
const kind =
|
|
81
|
+
event.pointerType === "mouse" || event.pointerType === "pen"
|
|
82
|
+
? event.pointerType
|
|
83
|
+
: "touch";
|
|
84
|
+
flush(time); // what fell due before this event comes first
|
|
85
|
+
press.press({
|
|
86
|
+
pointerId: event.pointerId,
|
|
87
|
+
kind,
|
|
88
|
+
x: event.clientX,
|
|
89
|
+
y: event.clientY,
|
|
90
|
+
time,
|
|
91
|
+
buttons: event.buttons,
|
|
92
|
+
button: event.button,
|
|
93
|
+
});
|
|
94
|
+
schedule();
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
A pinch and pan with several pointers, and the inertia that follows a release:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import {
|
|
102
|
+
TransformGesture,
|
|
103
|
+
inertiaDuration,
|
|
104
|
+
inertiaOffset,
|
|
105
|
+
type PointerSample,
|
|
106
|
+
} from "@ac-kit/ui-gesture";
|
|
107
|
+
|
|
108
|
+
const transform = new TransformGesture();
|
|
109
|
+
|
|
110
|
+
// `press`, `move` and `release` take the same kind of record.
|
|
111
|
+
function onMove(sample: PointerSample): void {
|
|
112
|
+
const update = transform.move(sample);
|
|
113
|
+
if (update !== undefined) {
|
|
114
|
+
view.panBy(update.deltaX, update.deltaY);
|
|
115
|
+
view.zoomAbout(update.originX, update.originY, update.scaleFactor);
|
|
116
|
+
view.rotateAbout(update.originX, update.originY, update.rotationDelta);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// After the last pointer lifts, `end` carries the release velocity in px/ms.
|
|
121
|
+
// `inertiaOffset` counts from the release: each frame, `elapsed` ms after it,
|
|
122
|
+
// the view is where it was released plus the offset, until the duration is up.
|
|
123
|
+
function coast(
|
|
124
|
+
releasedAt: { x: number; y: number },
|
|
125
|
+
speedX: number,
|
|
126
|
+
speedY: number,
|
|
127
|
+
elapsed: number,
|
|
128
|
+
): boolean {
|
|
129
|
+
view.panTo(
|
|
130
|
+
releasedAt.x + inertiaOffset(speedX, elapsed),
|
|
131
|
+
releasedAt.y + inertiaOffset(speedY, elapsed),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
return elapsed < Math.max(inertiaDuration(speedX), inertiaDuration(speedY));
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## What a press can mean
|
|
139
|
+
|
|
140
|
+
`PressGesture` follows one pointer at a time and decides, in one place:
|
|
141
|
+
|
|
142
|
+
- **A tap** moves less than the slop: 4 px for a mouse, 8 px for touch and pen,
|
|
143
|
+
measured as a distance. A drag past the slop starts from where it was crossed,
|
|
144
|
+
so the content does not jump. A release is a movement like any other: lifting
|
|
145
|
+
past the slop, with no `move` before it, is a drag that starts and ends at once
|
|
146
|
+
(its `end` update is the only one), not a tap. A press held longer than
|
|
147
|
+
`tapMaxDuration` is not a tap either.
|
|
148
|
+
- **A double tap** is a second tap pressed 40 to 300 ms after the first was
|
|
149
|
+
released, within 100 px of it. A first tap is never a double tap, however early
|
|
150
|
+
after the clock's origin. With `doubleTap` on, a tap is held back until it can
|
|
151
|
+
no longer become a double tap; `deadline` and `resolve` report it then.
|
|
152
|
+
- **A long press** is a press held still for 500 ms. Where the platform signals
|
|
153
|
+
a long press itself (Android's `contextmenu` during a touch), pass it on with
|
|
154
|
+
`signalLongPress`. The system's touch-and-hold setting cannot be read from a
|
|
155
|
+
page; expose `longPress.delay` as the application's own setting.
|
|
156
|
+
- **A second pointer** withdraws the tap and the long press; the drag, if one is
|
|
157
|
+
under way, carries on. A second `pointerdown` for the pointer already followed
|
|
158
|
+
changes nothing.
|
|
159
|
+
- **An axis** can be fixed (a press whose first movement runs along the other
|
|
160
|
+
axis is abandoned, so the page can scroll) or locked to the first movement's.
|
|
161
|
+
- **A swipe** is a drag released past a distance and a speed along an axis. A
|
|
162
|
+
pointer that stopped (kept its position for 40 ms) before the lift has no
|
|
163
|
+
release velocity, so no swipe and no inertia.
|
|
164
|
+
|
|
165
|
+
`TransformGesture` rebases its centroid, spread and angle whenever a pointer is
|
|
166
|
+
added or lifted, so an update never jumps; cancelling one pointer abandons the
|
|
167
|
+
whole gesture. Its pointers share the first one's device kind and button. A
|
|
168
|
+
pointer that lifts at a position other than its last one hands that movement
|
|
169
|
+
over, as a `move` update when others stay and within the `end` update when it is
|
|
170
|
+
the last.
|
|
171
|
+
|
|
172
|
+
## Timers and event order
|
|
173
|
+
|
|
174
|
+
What a press is follows from the times of the events, never from when you call
|
|
175
|
+
`resolve`: an event at time `T` first settles what fell due before `T`, so a
|
|
176
|
+
long press whose delay passed fires, and a release after it ends it, even if no
|
|
177
|
+
timer ran in between. Those resolutions are still reported by the next
|
|
178
|
+
`resolve`, so an event can come back before the resolution that explains it:
|
|
179
|
+
call `resolve` with the event's time before passing the event to keep them in
|
|
180
|
+
order, as the example above does. At exactly the moment something falls due the
|
|
181
|
+
order of your calls decides: an event passed first wins (a second press exactly
|
|
182
|
+
300 ms after the first release is a double tap, a release exactly at the
|
|
183
|
+
long-press delay is a tap), and `resolve(T)` first settles it. Settled taps are
|
|
184
|
+
reported once, no earlier than they settled, and never dropped: a caller that
|
|
185
|
+
never calls `resolve` accumulates one small record per tap.
|
|
186
|
+
|
|
187
|
+
## Bad input
|
|
188
|
+
|
|
189
|
+
Every recogniser and function throws a `RangeError` for a non-finite coordinate,
|
|
190
|
+
time or delta, for one pointer's time going backwards, and for an unknown
|
|
191
|
+
`deltaMode`, before changing any state. `PressGesture` has one clock for its
|
|
192
|
+
press: a `move`, `release`, `resolve` or `signalLongPress` with a time before it
|
|
193
|
+
throws, and the clock moves to the time of every resolution it reports, so a
|
|
194
|
+
timer that ran ahead of an older event makes that event throw unless you clamp
|
|
195
|
+
its time as the example does. Another pointer's older time is accepted:
|
|
196
|
+
coalesced samples of two pointers need not interleave. A pointer id, button or
|
|
197
|
+
kind a recogniser does not follow is ignored, not an error. A zero page size (an
|
|
198
|
+
element with no box) reads as unknown. Coordinates near the edge of the double
|
|
199
|
+
range do not overflow into `NaN`: sums, squares and ratios of them are formed so
|
|
200
|
+
that they stay finite wherever the result is representable.
|
|
201
|
+
|
|
202
|
+
## Binding to an element
|
|
203
|
+
|
|
204
|
+
`@ac-kit/ui-gesture/dom` attaches the recognisers to an element. Each binding
|
|
205
|
+
returns a `GestureBinding`: dispose it (`using`, or `[Symbol.dispose]()`) or
|
|
206
|
+
abort the `signal` option to remove every listener, timer and pointer capture.
|
|
207
|
+
A signal already aborted binds nothing.
|
|
208
|
+
|
|
209
|
+
| Binding | What it does |
|
|
210
|
+
| ---------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
211
|
+
| `bindPointers` | The shared plumbing, for callers that track pointers themselves: samples, capture, claims, one end each |
|
|
212
|
+
| `bindPressGesture` | A `PressGesture` on timers: press, drag, tap, double tap, long press, hover |
|
|
213
|
+
| `bindTransformGesture` | A `TransformGesture`, wheel and trackpad steps, navigation keys, taps, hover, cooperative mode |
|
|
214
|
+
| `claimPointerEvent` | Marks a `pointerdown` as claimed, for hand-written handlers the bindings around them should stand aside for |
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { bindTransformGesture } from "@ac-kit/ui-gesture/dom";
|
|
218
|
+
|
|
219
|
+
const controller = new AbortController();
|
|
220
|
+
|
|
221
|
+
bindTransformGesture(
|
|
222
|
+
canvas,
|
|
223
|
+
{
|
|
224
|
+
onTransform: (update) => {
|
|
225
|
+
view.panBy(update.deltaX, update.deltaY);
|
|
226
|
+
view.zoomAbout(update.originX, update.originY, update.scaleFactor);
|
|
227
|
+
},
|
|
228
|
+
onStep: (step) => view.zoomAbout(step.originX, step.originY, step.scaleFactor),
|
|
229
|
+
onKeyStep: (step) => view.keyStep(step),
|
|
230
|
+
onTap: (tap) => view.pick(tap.x, tap.y),
|
|
231
|
+
},
|
|
232
|
+
{ signal: controller.signal, touchAction: "none" },
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
// Later: remove every listener, timer and capture.
|
|
236
|
+
controller.abort();
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
What a binding does for you, and what stays yours:
|
|
240
|
+
|
|
241
|
+
- **Coordinates** are relative to the element's border box, measured at the
|
|
242
|
+
first press of a gesture (and at each wheel, trackpad or hover event);
|
|
243
|
+
`coordinates: "client"` gives `clientX` and `clientY`. `binding.box` is the
|
|
244
|
+
frame in use.
|
|
245
|
+
- **One end per pointer.** A release, a `pointercancel`, a capture lost before
|
|
246
|
+
the release, a mouse or pen that moves with no button down (a release the page
|
|
247
|
+
never saw), and `binding.cancel()` each end a pointer exactly once; handlers
|
|
248
|
+
see a `cancel` for all but the release.
|
|
249
|
+
- **Nested surfaces** are arbitrated by claims: the innermost binding that
|
|
250
|
+
accepts a `pointerdown` owns it, and the bindings around it skip it, or follow
|
|
251
|
+
it through bubbling without capturing it with `claimed: "share"`. The claim is
|
|
252
|
+
kept under `Symbol.for("@ac-kit/ui-gesture.claim")`, so two installed copies of
|
|
253
|
+
the package agree; a hand-written handler claims with `claimPointerEvent`.
|
|
254
|
+
- **Browser defaults** the gesture must not trigger are cancelled: `dragstart`
|
|
255
|
+
and `selectstart` while an accepted press is down, the one `click` that
|
|
256
|
+
follows a drag (not a keyboard click; a press disarms it), and `contextmenu` for touch
|
|
257
|
+
and pen presses when long press is on, where a `contextmenu` during a still
|
|
258
|
+
press also signals the long press. `touchAction` is set at bind and restored
|
|
259
|
+
at dispose; it has to be in place before the first `pointerdown`, so a
|
|
260
|
+
stylesheet is the better home when the element is not yours to restyle.
|
|
261
|
+
- **Handlers are read at each call** and may throw: the error is reported out of
|
|
262
|
+
band (`throwOutOfBand`) once the binding's own state is consistent, so a
|
|
263
|
+
throwing handler never leaves a timer, capture or claim behind. Whether a
|
|
264
|
+
wheel, key, tap or double-tap handler exists is read once, when binding: a
|
|
265
|
+
wheel listener is not passive only with `onStep`.
|
|
266
|
+
- **Steps the caller does not act on** stay the page's: `acceptStep(step)` and
|
|
267
|
+
`acceptKeyStep(step, event)` refuse a wheel or trackpad step, or a key, that
|
|
268
|
+
would move nothing (an arrow along an axis that does not pan); a refused one
|
|
269
|
+
reaches no handler and is neither cancelled nor stopped.
|
|
270
|
+
- **Samples** with a non-finite coordinate or time are dropped; each pointer's
|
|
271
|
+
times are made non-decreasing; with `coalesced` the coalesced events of a move
|
|
272
|
+
are fed instead of the parent event.
|
|
273
|
+
- **Cooperative mode** (`cooperative: true`) for a view inside a scrolling page:
|
|
274
|
+
an unmodified wheel is left to the page and reports `onCooperativeHint("wheel")`;
|
|
275
|
+
a lone touch stays provisional (not claimed, not captured, no `selectstart`
|
|
276
|
+
cancelled) and reports `"touch"` once it moves past the slop, so the page
|
|
277
|
+
scrolls (give the element `touch-action: pan-x pan-y pinch-zoom`); two touches
|
|
278
|
+
move the view, with `maxPointers` of at least 2, and when one lifts the
|
|
279
|
+
gesture ends and the other goes back aside; a non-passive `touchmove`
|
|
280
|
+
cancels the page's scroll only while two are down.
|
|
281
|
+
- **Hover** (`onHover`, `onHoverEnd`) for a mouse or pen with no button down;
|
|
282
|
+
touch never hovers.
|
|
283
|
+
- **Timers** are one `setTimeout` at the recogniser's `deadline`, cleared at
|
|
284
|
+
dispose. A recogniser has one clock, so a binding raises each event's time to
|
|
285
|
+
the latest it has been given and settles what fell due before it: a late timer
|
|
286
|
+
never makes an older event throw.
|
|
287
|
+
|
|
288
|
+
## Coming from `@ac-kit/ui-controls`
|
|
289
|
+
|
|
290
|
+
`TapGesture` is replaced by `PressGesture`. Its inputs are records, so the
|
|
291
|
+
position, time, device kind and button travel together.
|
|
292
|
+
|
|
293
|
+
| `@ac-kit/ui-controls` | `@ac-kit/ui-gesture` |
|
|
294
|
+
| ------------------------------------------------------------ | --------------------------------------------------------------------------------- |
|
|
295
|
+
| `TapGestureOptions.tapSlop` | `PressGestureOptions.slop`: per device kind, and a distance rather than a square |
|
|
296
|
+
| `TapGestureOptions.doubleTapInterval` | `PressGestureOptions.doubleTap.interval`; `doubleTap` is off unless asked for |
|
|
297
|
+
| `TapGesture.press(pointerId, x, y)` | `PressGesture.press(down)` with a `PointerDown` |
|
|
298
|
+
| `TapGesture.move(pointerId, x, y)` and `PointerDelta` | `PressGesture.move(sample)` and `DragUpdate.movementX` / `movementY` |
|
|
299
|
+
| `TapGesture.release(pointerId, time)` and `TapGestureRelease` | `PressGesture.release(sample)` and `PressRelease` |
|
|
300
|
+
| `TapGesture.resolveTap(time)` and `TapResolution` | `PressGesture.resolve(time)` and `PressResolution` |
|
|
301
|
+
| `TapGesture.cancel(pointerId?)` | `PressGesture.cancel(pointerId?)`, which returns the `cancel` update of a drag |
|
|
302
|
+
|
|
303
|
+
Behaviour that changes with it: a tap may move up to 8 px for touch and pen (4
|
|
304
|
+
for a mouse, as before) and is measured as a distance, not along each axis; a
|
|
305
|
+
double tap is timed from the first release to the second press and must land
|
|
306
|
+
within 100 px, no sooner than 40 ms; a second pointer landing during a press
|
|
307
|
+
withdraws its tap instead of being ignored.
|
|
308
|
+
|
|
309
|
+
See the
|
|
310
|
+
[generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/ui-gesture/)
|
|
311
|
+
for the full reference.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_validate.d.ts","sourceRoot":"","sources":["../src/_validate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEhE,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI,CAMzD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { assertFinite, assertInteger } from "@ac-kit/core";
|
|
2
|
+
/** Rejects what no browser event carries: a non-finite or fractional field. */
|
|
3
|
+
export function assertSample_(sample) {
|
|
4
|
+
assertInteger(sample.pointerId, "pointerId");
|
|
5
|
+
assertInteger(sample.buttons, "buttons");
|
|
6
|
+
assertFinite(sample.x, "x");
|
|
7
|
+
assertFinite(sample.y, "y");
|
|
8
|
+
assertFinite(sample.time, "time");
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=_validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_validate.js","sourceRoot":"","sources":["../src/_validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAI3D,+EAA+E;AAC/E,MAAM,UAAU,aAAa,CAAC,MAAqB;IAClD,aAAa,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IAC7C,aAAa,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACzC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC5B,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC5B,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a `pointerdown` as claimed, for hand-written handlers that want the
|
|
3
|
+
* bindings around them to stand aside.
|
|
4
|
+
*/
|
|
5
|
+
export declare function claimPointerEvent(event: PointerEvent): void;
|
|
6
|
+
/** Whether an inner surface claimed this `pointerdown`. */
|
|
7
|
+
export declare function isPointerEventClaimed(event: PointerEvent): boolean;
|
|
8
|
+
//# sourceMappingURL=_claim.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_claim.d.ts","sourceRoot":"","sources":["../../src/dom/_claim.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAE3D;AAED,2DAA2D;AAC3D,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAElE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// A registry symbol, so two installed copies of the package agree on a claim.
|
|
2
|
+
const CLAIM_ = Symbol.for("@ac-kit/ui-gesture.claim");
|
|
3
|
+
/**
|
|
4
|
+
* Marks a `pointerdown` as claimed, for hand-written handlers that want the
|
|
5
|
+
* bindings around them to stand aside.
|
|
6
|
+
*/
|
|
7
|
+
export function claimPointerEvent(event) {
|
|
8
|
+
Object.defineProperty(event, CLAIM_, { value: true, configurable: true });
|
|
9
|
+
}
|
|
10
|
+
/** Whether an inner surface claimed this `pointerdown`. */
|
|
11
|
+
export function isPointerEventClaimed(event) {
|
|
12
|
+
return event[CLAIM_] === true; // the claim is a symbol-keyed flag the event type does not declare
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=_claim.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_claim.js","sourceRoot":"","sources":["../../src/dom/_claim.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;AAEtD;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAmB;IACpD,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;AAC3E,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,qBAAqB,CAAC,KAAmB;IACxD,OAAQ,KAA4C,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC,CAAC,mEAAmE;AAC3I,CAAC"}
|