You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/6-pointer-events/article.md
+67-33
Original file line number
Diff line number
Diff line change
@@ -9,16 +9,16 @@ Let's make a small overview, so that you understand the general picture and the
9
9
- Long ago, in the past, there were only mouse events.
10
10
11
11
Then touch devices became widespread, phones and tablets in particular. For the existing scripts to work, they generated (and still generate) mouse events. For instance, tapping a touchscreen generates `mousedown`. So touch devices worked well with web pages.
12
-
12
+
13
13
But touch devices have more capabilities than a mouse. For example, it's possible to touch multiple points at once ("multi-touch"). Although, mouse events don't have necessary properties to handle such multi-touches.
14
14
15
15
- So touch events were introduced, such as `touchstart`, `touchend`, `touchmove`, that have touch-specific properties (we don't cover them in detail here, because pointer events are even better).
16
16
17
-
Still, it wasn't enough, as there are many other devices, such as pens, that have their own features. Also, writing code that listens for both touch and mouse events was cumbersome.
17
+
Still, it wasn't enough, as there are many other devices, such as pens, that have their own features. Also, writing code that listens for both touch and mouse events was cumbersome.
18
18
19
19
- To solve these issues, the new standard Pointer Events was introduced. It provides a single set of events for all kinds of pointing devices.
20
20
21
-
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while the newer [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works and is mostly compatible with Pointer Events level 2.
21
+
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while the newer [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works and is mostly compatible with Pointer Events level 2.
22
22
23
23
Unless you develop for old browsers, such as Internet Explorer 10, or for Safari 12 or below, there's no point in using mouse or touch events any more -- we can switch to pointer events.
24
24
@@ -43,29 +43,29 @@ Pointer events are named similarly to mouse events:
43
43
|`gotpointercapture`| - |
44
44
|`lostpointercapture`| - |
45
45
46
-
As we can see, for every `mouse<event>`, there's a `pointer<event>` that plays a similar role. Also there are 3 additional pointer events that don't have a corresponding `mouse...` counterpart, we'll explain them soon.
46
+
As we can see, for every `mouse<event>`, there's a `pointer<event>` that plays a similar role. Also there are 3 additional pointer events that don't have a corresponding `mouse...` counterpart, we'll explain them soon.
47
47
48
48
```smart header="Replacing `mouse<event>` with `pointer<event>` in our code"
49
49
We can replace `mouse<event>` events with `pointer<event>` in our code and expect things to continue working fine with mouse.
50
50
51
-
The support for touch devices will also "magically" improve. Although, we may need to add `touch-action: none` in some places in CSS. We'll cover it below in the section about `pointercancel`.
51
+
The support for touch devices will also "magically" improve. Although, we may need to add `touch-action: none` in some places in CSS. We'll cover it below in the section about `pointercancel`.
52
52
```
53
53
54
54
## Pointer event properties
55
55
56
56
Pointer events have the same properties as mouse events, such as `clientX/Y`, `target`, etc., plus some others:
57
57
58
58
- `pointerId` - the unique identifier of the pointer causing the event.
59
-
59
+
60
60
Browser-generated. Allows us to handle multiple pointers, such as a touchscreen with stylus and multi-touch (examples will follow).
61
-
- `pointerType` - the pointing device type. Must be a string, one of: "mouse", "pen" or "touch".
61
+
- `pointerType` - the pointing device type. Must be a string, one of: "mouse", "pen" or "touch".
62
62
63
63
We can use this property to react differently on various pointer types.
64
64
- `isPrimary` - is `true` for the primary pointer (the first finger in multi-touch).
65
65
66
66
Some pointer devices measure contact area and pressure, e.g. for a finger on the touchscreen, there are additional properties for that:
67
67
68
-
- `width` - the width of the area where the pointer (e.g. a finger) touches the device. Where unsupported, e.g. for a mouse, it's always `1`.
68
+
- `width` - the width of the area where the pointer (e.g. a finger) touches the device. Where unsupported, e.g. for a mouse, it's always `1`.
69
69
- `height` - the height of the area where the pointer touches the device. Where unsupported, it's always `1`.
70
70
- `pressure` - the pressure of the pointer tip, in range from 0 to 1. For devices that don't support pressure must be either `0.5` (pressed) or `0`.
71
71
- `tangentialPressure` - the normalized tangential pressure.
@@ -102,11 +102,11 @@ Please note: you must be using a touchscreen device, such as a phone or a tablet
102
102
103
103
## Event: pointercancel
104
104
105
-
The `pointercancel` event fires when there's an ongoing pointer interaction, and then something happens that causes it to be aborted, so that no more pointer events are generated.
105
+
The `pointercancel` event fires when there's an ongoing pointer interaction, and then something happens that causes it to be aborted, so that no more pointer events are generated.
106
106
107
-
Such causes are:
107
+
Such causes are:
108
108
- The pointer device hardware was physically disabled.
109
-
- The device orientation changed (tablet rotated).
109
+
- The device orientation changed (tablet rotated).
110
110
- The browser decided to handle the interaction on its own, considering it a mouse gesture or zoom-and-pan action or something else.
111
111
112
112
We'll demonstrate `pointercancel` on a practical example to see how it affects us.
@@ -126,7 +126,7 @@ Here is the flow of user actions and the corresponding events:
126
126
So the issue is that the browser "hijacks" the interaction: `pointercancel` fires in the beginning of the "drag-and-drop" process, and no more `pointermove` events are generated.
127
127
128
128
```online
129
-
Here's the drag'n'drop demo with loggin of pointer events (only `up/down`, `move` and `cancel`) in the `textarea`:
129
+
Here's the drag'n'drop demo with loggin of pointer events (only `up/down`, `move` and `cancel`) in the `textarea`:
130
130
131
131
[iframe src="ball" height=240 edit]
132
132
```
@@ -141,7 +141,7 @@ We need to do two things:
141
141
- We can do this by setting `ball.ondragstart = () => false`, just as described in the article <info:mouse-drag-and-drop>.
142
142
- That works well for mouse events.
143
143
2. For touch devices, there are other touch-related browser actions (besides drag'n'drop). To avoid problems with them too:
144
-
- Prevent them by setting `#ball { touch-action: none }` in CSS.
144
+
- Prevent them by setting `#ball { touch-action: none }` in CSS.
145
145
- Then our code will start working on touch devices.
146
146
147
147
After we do that, the events will work as intended, the browser won't hijack the process and doesn't emit `pointercancel`.
@@ -163,7 +163,7 @@ Pointer capturing is a special feature of pointer events.
163
163
The idea is very simple, but may seem quite odd at first, as nothing like that exists for any other event type.
164
164
165
165
The main method is:
166
-
-`elem.setPointerCapture(pointerId)` - binds events with the given `pointerId` to `elem`. After the call all pointer events with the same `pointerId` will have `elem` as the target (as if happened on `elem`), no matter where in document they really happened.
166
+
-`elem.setPointerCapture(pointerId)` -- binds events with the given `pointerId` to `elem`. After the call all pointer events with the same `pointerId` will have `elem` as the target (as if happened on `elem`), no matter where in document they really happened.
167
167
168
168
In other words, `elem.setPointerCapture(pointerId)` retargets all subsequent events with the given `pointerId` to `elem`.
169
169
@@ -172,61 +172,95 @@ The binding is removed:
172
172
- automatically when `elem` is removed from the document,
173
173
- when `elem.releasePointerCapture(pointerId)` is called.
174
174
175
+
Now what is it good for? It's time to see a real-life example.
176
+
175
177
**Pointer capturing can be used to simplify drag'n'drop kind of interactions.**
176
178
177
-
As an example, let's recall how one can implement a custom slider, described in the <info:mouse-drag-and-drop>.
179
+
Let's recall how one can implement a custom slider, described in the <info:mouse-drag-and-drop>.
180
+
181
+
We can make a `slider` element to represent the strip and the "runner" (`thumb`) inside it:
182
+
183
+
```html
184
+
<divclass="slider">
185
+
<divclass="thumb"></div>
186
+
</div>
187
+
```
188
+
189
+
With styles, it looks like this:
190
+
191
+
[iframe src="slider-html" height=40 edit]
178
192
179
-
We make a slider element with the strip and the "runner" (`thumb`) inside it.
193
+
<p></p>
180
194
181
-
Then it works like this:
195
+
And here's the working logic, as it was described, after replacing mouse events with similar pointer events:
182
196
183
-
1. The user presses on the slider `thumb` - `pointerdown` triggers.
184
-
2. Then they move the pointer - `pointermove` triggers, and we move the `thumb` along.
185
-
- ...As the pointer moves, it may leave the slider `thumb`: go above or below it. The `thumb` should move strictly horizontally, remaining aligned with the pointer.
197
+
1. The user presses on the slider `thumb` --`pointerdown` triggers.
198
+
2. Then they move the pointer --`pointermove` triggers, and our code moves the `thumb` element along.
199
+
- ...As the pointer moves, it may leave the slider `thumb` element, go above or below it. The `thumb` should move strictly horizontally, remaining aligned with the pointer.
186
200
187
-
So, to track all pointer movements, including when it goes above/below the `thumb`, we had to assign `pointermove` event handler on the whole `document`.
201
+
In the mouse event based solution, to track all pointer movements, including when it goes above/below the `thumb`, we had to assign `mousemove` event handler on the whole `document`.
188
202
189
-
That solution looks a bit "dirty". One of the problems is that pointer movements around the documentmay cause side effects, trigger other event handlers, totally not related to the slider.
203
+
That's not a cleanest solution, though. One of the problems is that when a user moves the pointer around the document, it may trigger event handlers (such as `mouseover`) on some other elements, invoke totally unrelated UI functionality, and we don't want that.
190
204
191
-
Pointer capturing provides a means to bind `pointermove` to `thumb` and avoid any such problems:
205
+
This is the place where `setPointerCapture` comes into play.
192
206
193
207
- We can call `thumb.setPointerCapture(event.pointerId)` in `pointerdown` handler,
194
-
- Then future pointer events until `pointerup/cancel` will be retargeted to `thumb`.
208
+
- Then future pointer events until `pointerup/cancel` will be retargeted to `thumb`.
195
209
- When `pointerup` happens (dragging complete), the binding is removed automatically, we don't need to care about it.
196
210
197
-
So, even if the user moves the pointer around the whole document, events handlers will be called on `thumb`. Besides, coordinate properties of the event objects, such as `clientX/clientY` will still be correct - the capturing only affects `target/currentTarget`.
211
+
So, even if the user moves the pointer around the whole document, events handlers will be called on `thumb`. Nevertheless, coordinate properties of the event objects, such as `clientX/clientY` will still be correct - the capturing only affects `target/currentTarget`.
198
212
199
213
Here's the essential code:
200
214
201
215
```js
202
216
thumb.onpointerdown=function(event) {
203
217
// retarget all pointer events (until pointerup) to thumb
204
218
thumb.setPointerCapture(event.pointerId);
205
-
};
206
219
207
-
thumb.onpointermove=function(event) {
208
-
// moving the slider: listen on the thumb, as all pointer events are retargeted to it
209
-
let newLeft =event.clientX-slider.getBoundingClientRect().left;
210
-
thumb.style.left= newLeft +'px';
220
+
// start tracking pointer moves
221
+
thumb.onpointermove=function(event) {
222
+
// moving the slider: listen on the thumb, as all pointer events are retargeted to it
223
+
let newLeft =event.clientX-slider.getBoundingClientRect().left;
224
+
thumb.style.left= newLeft +'px';
225
+
};
226
+
227
+
// on pointer up finish tracking pointer moves
228
+
thumb.onpointerup=function(event) {
229
+
thumb.onpointermove=null;
230
+
thumb.onpointerup=null;
231
+
// ...also process the "drag end" if needed
232
+
};
211
233
};
212
234
213
-
// note: no need to call thumb.releasePointerCapture,
235
+
// note: no need to call thumb.releasePointerCapture,
214
236
// it happens on pointerup automatically
215
237
```
216
238
217
239
```online
218
240
The full demo:
219
241
220
242
[iframe src="slider" height=100 edit]
243
+
244
+
<p></p>
245
+
246
+
In the demo, there's also an additional element with `onmouseover` handler showing the current date.
247
+
248
+
Please note: while you're dragging the thumb, you may hover over this element, and its handler *does not* trigger.
249
+
250
+
So the dragging is now free of side effects, thanks to `setPointerCapture`.
221
251
```
222
252
253
+
254
+
223
255
At the end, pointer capturing gives us two benefits:
224
256
1. The code becomes cleaner as we don't need to add/remove handlers on the whole `document` any more. The binding is released automatically.
225
-
2. If there are any `pointermove` handlers in the document, they won't be accidentally triggered by the pointer while the user is dragging the slider.
257
+
2. If there are other pointer event handlers in the document, they won't be accidentally triggered by the pointer while the user is dragging the slider.
226
258
227
259
### Pointer capturing events
228
260
229
-
There are two associated pointer events:
261
+
There's one more thing to mention here, for the sake of completeness.
262
+
263
+
There are two events associated with pointer capturing:
230
264
231
265
-`gotpointercapture` fires when an element uses `setPointerCapture` to enable capturing.
232
266
-`lostpointercapture` fires when the capture is released: either explicitly with `releasePointerCapture` call, or automatically on `pointerup`/`pointercancel`.
0 commit comments