westures-core 1.0.0 → 1.2.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 +10 -0
- package/README.md +137 -56
- package/dist/index.js +1249 -22
- package/dist/index.js.map +1 -1
- package/package.json +29 -16
- package/src/Gesture.js +7 -7
- package/src/Input.js +9 -2
- package/src/Point2D.js +12 -0
- package/src/Region.js +23 -30
- package/src/Smoothable.js +2 -2
- package/src/State.js +2 -2
- package/src/constants.js +27 -16
- package/src/utils.js +2 -2
- package/.travis.yml +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
- Switch to using pointer events by default, combined with setting touch-action:
|
|
6
|
+
none on the gesture elements (not the region itself).
|
|
7
|
+
- Provide options on the Region for choosing whether to prefer pointer events
|
|
8
|
+
over mouse/touch events (preferPointer) and what to set the touch-action
|
|
9
|
+
property to on gesture elements (touchAction).
|
|
10
|
+
- Default to using the window as the region if no element provided.
|
|
11
|
+
- Add mouseleave to the CANCEL_EVENTS
|
|
12
|
+
|
|
3
13
|
## 1.0.0
|
|
4
14
|
|
|
5
15
|
- Official first release! This engine is no longer considered to be in beta.
|
package/README.md
CHANGED
|
@@ -10,14 +10,27 @@ https://coveralls.io/repos/github/mvanderkamp/westures-core/badge.svg?branch=mas
|
|
|
10
10
|
https://api.codeclimate.com/v1/badges/a5f4a4745352d6e2520c/maintainability)
|
|
11
11
|
](https://codeclimate.com/github/mvanderkamp/westures-core/maintainability)
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
Westures is a robust n-pointer multitouch gesture detection library for
|
|
14
|
+
JavaScript. This means that each gesture is be capable of working seamlessly as
|
|
15
|
+
input points are added and removed, with no limit on the number of input points,
|
|
16
|
+
and with each input point contributing to the gesture. It is also capable of
|
|
17
|
+
working across a wide range of devices.
|
|
18
|
+
|
|
19
|
+
This module contains the core functionality of the Westures gesture library for
|
|
20
|
+
JavaScript. It is intended for use as a lighter-weight module to use if you do
|
|
21
|
+
not want to use the base gestures included with the standard [westures](
|
|
16
22
|
https://mvanderkamp.github.io/westures/) module.
|
|
17
23
|
|
|
18
24
|
Visit this page for an example of the system in action: [Westures Example](
|
|
19
|
-
https://mvanderkamp.github.io/westures-example/).
|
|
20
|
-
|
|
25
|
+
https://mvanderkamp.github.io/westures-example/).
|
|
26
|
+
|
|
27
|
+
Westures aims to achieve its goals without using any dependencies, yet maintain
|
|
28
|
+
usability across the main modern browsers. Transpilation may be necessary for
|
|
29
|
+
this last point to be achieved, as the library is written using many of the
|
|
30
|
+
newer features of the JavaScript language. A transpiled bundle is provided, but
|
|
31
|
+
the browser target list is arbitrary and likely includes some bloat. In most
|
|
32
|
+
cases you will be better off performing bundling, transpilation, and
|
|
33
|
+
minification yourself.
|
|
21
34
|
|
|
22
35
|
Westures is a fork of [ZingTouch](https://github.com/zingchart/zingtouch).
|
|
23
36
|
|
|
@@ -25,22 +38,30 @@ Westures is a fork of [ZingTouch](https://github.com/zingchart/zingtouch).
|
|
|
25
38
|
|
|
26
39
|
```javascript
|
|
27
40
|
// Import the module.
|
|
28
|
-
const
|
|
41
|
+
const wes = require('westures-core');
|
|
42
|
+
|
|
43
|
+
// Declare a region. The default is the window object, but other elements like
|
|
44
|
+
// the document body work too.
|
|
45
|
+
const region = new wes.Region();
|
|
29
46
|
|
|
30
|
-
//
|
|
31
|
-
|
|
47
|
+
// Define a Gesture subclass
|
|
48
|
+
class Follow extends wes.Gesture {
|
|
49
|
+
move(state) {
|
|
50
|
+
return state.centroid; // Reports the {x, y} of the average input position
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Locate an element to attach the gesture to.
|
|
55
|
+
const element = document.querySelector('#follow');
|
|
32
56
|
|
|
33
57
|
// Instantiate a Gesture for an element within the region.
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
// data.translation.x ...
|
|
38
|
-
// data.translation.y ...
|
|
39
|
-
// and so on, depending on the Gesture
|
|
58
|
+
const follow = new Follow(element, (data) => {
|
|
59
|
+
// data.x ...
|
|
60
|
+
// data.y ...
|
|
40
61
|
});
|
|
41
62
|
|
|
42
63
|
// Add the gesture to the region.
|
|
43
|
-
region.addGesture(
|
|
64
|
+
region.addGesture(follow);
|
|
44
65
|
```
|
|
45
66
|
|
|
46
67
|
## Table of Contents
|
|
@@ -49,6 +70,9 @@ region.addGesture(pan);
|
|
|
49
70
|
- [Overview](#overview)
|
|
50
71
|
- [Basic Usage](#basic-usage)
|
|
51
72
|
- [Implementing Custom Gestures](#implementing-custom-gestures)
|
|
73
|
+
- [Nomenclature and Origins](#nomenclature-and-origins)
|
|
74
|
+
- [Changes](#changes)
|
|
75
|
+
- [Issues](#issues)
|
|
52
76
|
- [Links](#links)
|
|
53
77
|
|
|
54
78
|
## Features
|
|
@@ -68,34 +92,41 @@ region.addGesture(pan);
|
|
|
68
92
|
|
|
69
93
|
## Overview
|
|
70
94
|
|
|
71
|
-
There are seven classes
|
|
95
|
+
There are seven classes made available by this module:
|
|
72
96
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
Name | Description
|
|
98
|
+
----------- | -----------
|
|
99
|
+
Gesture | Base class for defining westures gestures
|
|
100
|
+
Input | Track a single pointer through its lifetime
|
|
101
|
+
Point2D | Store and act on a 2-dimensional point
|
|
102
|
+
PointerData | Record data pertaining to a single user input event for a single pointer.
|
|
103
|
+
Region | Listen for user input events and respond appropriately
|
|
104
|
+
Smoothable | Datatype which provides inertial smoothing capabilities
|
|
105
|
+
State | Track inputs within a Region
|
|
82
106
|
|
|
83
107
|
Additionally, two support files are defined:
|
|
84
108
|
|
|
85
|
-
|
|
86
|
-
|
|
109
|
+
Name | Description
|
|
110
|
+
--------- | -----------
|
|
111
|
+
constants | Constant values used throughout the engine
|
|
112
|
+
utils | Helpful utility functions
|
|
87
113
|
|
|
88
|
-
|
|
114
|
+
Here is a graph to help you understand the relationships between these classes:
|
|
89
115
|
|
|
90
116
|

|
|
92
118
|
|
|
93
119
|
## Basic Usage
|
|
94
120
|
|
|
121
|
+
- [Declaring a Region](#declaring-a-region)
|
|
122
|
+
- [Defining a Gesture Subclass](#defining-a-gesture-subclass)
|
|
123
|
+
- [Instantiating a Gesture](#instantiating-a-gesture)
|
|
124
|
+
- [Adding a Gesture to a Region](#adding-a-gesture-to-a-region)
|
|
125
|
+
|
|
95
126
|
### Importing the module
|
|
96
127
|
|
|
97
128
|
```javascript
|
|
98
|
-
const
|
|
129
|
+
const wes = require('westures-core');
|
|
99
130
|
```
|
|
100
131
|
|
|
101
132
|
### Declaring a Region
|
|
@@ -111,9 +142,31 @@ If you have lots of interactable elements on your page, you may find it
|
|
|
111
142
|
convenient to use smaller elements as regions. Test it out in any case, and see
|
|
112
143
|
what works better for you.
|
|
113
144
|
|
|
145
|
+
By default, the window object is used.
|
|
146
|
+
|
|
114
147
|
```javascript
|
|
115
|
-
const region = new Region(document.body);
|
|
148
|
+
const region = new wes.Region(document.body);
|
|
116
149
|
```
|
|
150
|
+
|
|
151
|
+
### Defining a Gesture Subclass
|
|
152
|
+
|
|
153
|
+
In order to use the engine, you'll need to define gestures. This is done by
|
|
154
|
+
extending the Gesture class provided by this module, and overriding any or all
|
|
155
|
+
of the four phase hooks ('start', 'move', 'end', and 'cancel') as is appropriate
|
|
156
|
+
for your gesture.
|
|
157
|
+
|
|
158
|
+
Defined here is a very simple gesture that simply reports the centroid of the
|
|
159
|
+
input points. Note that the returned value must be an Object!
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
// Define a Gesture subclass
|
|
163
|
+
class Follow extends wes.Gesture {
|
|
164
|
+
move(state) {
|
|
165
|
+
return state.centroid;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
117
170
|
### Instantiating a Gesture
|
|
118
171
|
|
|
119
172
|
When you instantiate a gesture, you need to provide a handler as well as an
|
|
@@ -122,38 +175,48 @@ with the region was inside the given Element. Therefore unless you want to try
|
|
|
122
175
|
something fancy the gesture element should probably be contained inside the
|
|
123
176
|
region element. It could even be the region element.
|
|
124
177
|
|
|
125
|
-
Now for an example. Suppose you have a div within which you want to detect
|
|
126
|
-
gesture
|
|
127
|
-
|
|
178
|
+
Now for an example. Suppose you have a div within which you want to detect the
|
|
179
|
+
Follow gesture we defined above. The div has id 'follow'. We need to find the
|
|
180
|
+
element first.
|
|
128
181
|
|
|
129
182
|
```javascript
|
|
130
|
-
const
|
|
183
|
+
const element = document.querySelector('#follow');
|
|
131
184
|
```
|
|
132
185
|
|
|
133
|
-
|
|
134
|
-
|
|
186
|
+
And we also need a handler. This function will be called whenever a gesture hook
|
|
187
|
+
returns non-null data. For Follow, this is just the move phase, but the handler
|
|
188
|
+
doesn't need to know that. The data returned by the hook will be available
|
|
189
|
+
inside the handler.
|
|
135
190
|
|
|
136
191
|
```javascript
|
|
137
|
-
function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
192
|
+
function followLogger(data) {
|
|
193
|
+
console.log(
|
|
194
|
+
'The centroid of the points interacting with #follow is:',
|
|
195
|
+
'x:', data.x,
|
|
196
|
+
'y:', data.y,
|
|
197
|
+
)
|
|
141
198
|
}
|
|
142
199
|
```
|
|
143
200
|
|
|
144
|
-
|
|
145
|
-
|
|
201
|
+
Now we're ready to combine the element and its handler into a gesture.
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
const follow = new Follow(element, followLogger);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
We're not quite done though, as none of this will actually work until you add
|
|
208
|
+
the gesture to the region.
|
|
146
209
|
|
|
147
210
|
### Adding a Gesture to a Region
|
|
148
211
|
|
|
149
212
|
Simple:
|
|
150
213
|
|
|
151
214
|
```javascript
|
|
152
|
-
region.addGesture(
|
|
215
|
+
region.addGesture(follow);
|
|
153
216
|
```
|
|
154
217
|
|
|
155
|
-
Now the `
|
|
156
|
-
on the `#
|
|
218
|
+
Now the `followLogger` function will be called whenever a `follow` gesture is
|
|
219
|
+
detected on the `#follow` element inside the region.
|
|
157
220
|
|
|
158
221
|
## Implementing Custom Gestures
|
|
159
222
|
|
|
@@ -201,8 +264,11 @@ forwarded to bound handlers when a non-null value is returned by a hook.
|
|
|
201
264
|
Returned values should be packed inside an object. For example, instead of just
|
|
202
265
|
`return 42;`, a custom hook should do `return { value: 42 };`
|
|
203
266
|
|
|
267
|
+
If your Gesture subclass needs to track any kind of complex state, remember that
|
|
268
|
+
it may be necessary to reset the state in the `cancel` phase.
|
|
269
|
+
|
|
204
270
|
For information about what data is accessible via the State object, see the full
|
|
205
|
-
documentation [here](https://mvanderkamp.github.io/westures-core/State.html).
|
|
271
|
+
documentation [here](https://mvanderkamp.github.io/westures-core/westures-core.State.html).
|
|
206
272
|
Note that his documentation was generated with `jsdoc`.
|
|
207
273
|
|
|
208
274
|
### Default Data Passed to Handlers
|
|
@@ -212,16 +278,31 @@ to handlers, and for the most part what that data will be. Note though that a
|
|
|
212
278
|
few properties will get added to the outgoing data object before the handler is
|
|
213
279
|
called. Those properties are:
|
|
214
280
|
|
|
215
|
-
Name | Type
|
|
216
|
-
|
|
217
|
-
centroid | Point2D
|
|
218
|
-
event | Event
|
|
219
|
-
phase | String
|
|
220
|
-
type | String
|
|
221
|
-
target | Element
|
|
281
|
+
Name | Type | Value
|
|
282
|
+
-------- | -------- | -----
|
|
283
|
+
centroid | Point2D | The centroid of the input points.
|
|
284
|
+
event | Event | The input event which caused the gesture to be recognized
|
|
285
|
+
phase | String | `'start'`, `'move'`, `'end'`, or `'cancel'`
|
|
286
|
+
type | String | The name of the gesture as specified by its designer.
|
|
287
|
+
target | Element | The Element that is associated with the recognized gesture.
|
|
288
|
+
|
|
289
|
+
If data properties returned by a hook have a name collision with one of these
|
|
290
|
+
properties, the value from the hook gets precedent and the default is
|
|
291
|
+
overwritten.
|
|
292
|
+
|
|
293
|
+
## Nomenclature and Origins
|
|
294
|
+
|
|
295
|
+
In my last year of univerisity, I was working on an API for building
|
|
296
|
+
multi-device interfaces called "WAMS" (Workspaces Across Multiple Surfaces),
|
|
297
|
+
which included the goal of supporting multi-device gestures.
|
|
298
|
+
|
|
299
|
+
After an extensive search I found that none of the available multitouch
|
|
300
|
+
libraries for JavaScript provided the fidelity I needed, and concluded that I
|
|
301
|
+
would need to write my own, or at least fork an existing one. ZingTouch proved
|
|
302
|
+
to the be the most approachable, so I decided it would make a good starting
|
|
303
|
+
point.
|
|
222
304
|
|
|
223
|
-
|
|
224
|
-
value from the hook gets precedent and the default is overwritten.
|
|
305
|
+
The name "westures" is a mash-up of "WAMS" and "gestures".
|
|
225
306
|
|
|
226
307
|
## Changes
|
|
227
308
|
|
|
@@ -231,7 +312,7 @@ most recent updates.
|
|
|
231
312
|
|
|
232
313
|
## Issues
|
|
233
314
|
|
|
234
|
-
If you find any
|
|
315
|
+
If you find any issues, please let me know!
|
|
235
316
|
|
|
236
317
|
## Links
|
|
237
318
|
|