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
+20-20
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Pointer events
2
2
3
-
Pointer events are a modern way to handle input from a variety of pointing devices, such as a mouse, a pen/stylus, a touchscreen and so on.
3
+
Pointer events are a modern way to handle input from a variety of pointing devices, such as a mouse, a pen/stylus, a touchscreen, and so on.
4
4
5
5
## The brief history
6
6
@@ -16,13 +16,13 @@ Let's make a small overview, so that you understand the general picture and the
16
16
17
17
- To solve these issues, the new standard Pointer Events was introduced. It provides a single set of events for all kinds of pointing devices.
18
18
19
-
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while the [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works. Unless you code for Internet Explorer 10 or Safari 12 and below, there's no point in using mouse or touch events any more. We can switch to pointer events.
19
+
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works. Unless you code for 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.
20
20
21
-
That said, there are important peculiarities, one should know them to use them correctly and avoid surprises. We'll pay attention to them in this article.
21
+
That said, there are important peculiarities one should know them to use them correctly and avoid surprises. We'll make note of them in this article.
22
22
23
23
## Pointer event types
24
24
25
-
Pointer events are named similar to mouse events:
25
+
Pointer events are named similarly to mouse events:
26
26
27
27
| Pointer Event | Mouse event |
28
28
|---------------|-------------|
@@ -42,25 +42,25 @@ As we can see, for every `mouse<event>`, there's a `pointer<event>` that plays a
42
42
```smart header="Replacing `mouse<event>` with `pointer<event>` in our code"
43
43
We can replace `mouse<event>` events with `pointer<event>` in our code and expect things to continue working fine with mouse.
44
44
45
-
The support for touch devices will also "magically" improve, but we'll probably need to add `touch-action: none`rule in CSS. See the details below in the section about `pointercancel`.
45
+
The support for touch devices will also "magically" improve, but we'll probably need to add `touch-action: none` in CSS. See the details below in the section about `pointercancel`.
46
46
```
47
47
48
48
## Pointer event properties
49
49
50
-
Pointer events have the same properties as mouse events, such as `clientX/Y`, `target` etc, plus some extra:
50
+
Pointer events have the same properties as mouse events, such as `clientX/Y`, `target`, etc., plus some others:
51
51
52
52
- `pointerId` - the unique identifier of the pointer causing the event.
53
53
54
-
Allows to handle multiple pointers, such as a touchscreen with stylus and multi-touch (explained below).
55
-
- `pointerType` - the pointing device type, must be a string, one of: "mouse", "pen" or "touch".
54
+
Allows us to handle multiple pointers, such as a touchscreen with stylus and multi-touch (explained below).
55
+
- `pointerType` - the pointing device type. Must be a string, one of: "mouse", "pen" or "touch".
56
56
57
57
We can use this property to react differently on various pointer types.
58
58
- `isPrimary` - `true` for the primary pointer (the first finger in multi-touch).
59
59
60
-
For pointers that measure a contact area and pressure, e.g. a finger on the touchscreen, the additional properties can be useful:
60
+
For pointers that measure contact area and pressure, e.g. a finger on the touchscreen, the additional properties can be useful:
61
61
62
-
- `width` - the width of the area where the pointer touches the device. Where unsupported, e.g. for mouse it's always `1`.
63
-
- `height` - the height of the area where the pointer touches the device. Where unsupported, always `1`.
62
+
- `width` - the width of the area where the pointer touches the device. Where unsupported, e.g. for a mouse, it's always `1`.
63
+
- `height` - the height of the area where the pointer touches the device. Where unsupported, it's always `1`.
64
64
- `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`.
65
65
- `tangentialPressure` - the normalized tangential pressure.
66
66
- `tiltX`, `tiltY`, `twist` - pen-specific properties that describe how the pen is positioned relative the surface.
@@ -69,11 +69,11 @@ These properties aren't very well supported across devices, so they are rarely u
69
69
70
70
## Multi-touch
71
71
72
-
One of the things that mouse events totally don't support is multi-touch: a user can touch them in several places at once on their phone or tablet, or perform special gestures.
72
+
One of the things that mouse events totally don't support is multi-touch: a user can touch in several places at once on their phone or tablet, or perform special gestures.
73
73
74
-
Pointer Events allow handling multi-touch with the help of `pointerId` and `isPrimary` properties.
74
+
Pointer Events allow handling multi-touch with the help of the `pointerId` and `isPrimary` properties.
75
75
76
-
Here's what happens when a user touches a screen in one place, and then puts another finger somewhere else on it:
76
+
Here's what happens when a user touches a screen in one place, then puts another finger somewhere else on it:
77
77
78
78
1. At the first touch:
79
79
- `pointerdown` with `isPrimary=true` and some `pointerId`.
@@ -91,7 +91,7 @@ Here's the demo that logs `pointerdown` and `pointerup` events:
91
91
92
92
[iframe src="multitouch" edit height=200]
93
93
94
-
Please note: you must be using a touchscreen device, such as a phone or a tablet to actually see the difference. For single-touch devices, such as a mouse, there'll be always same `pointerId` with `isPrimary=true`, for all pointer events.
94
+
Please note: you must be using a touchscreen device, such as a phone or a tablet, to actually see the difference. For single-touch devices, such as a mouse, there'll be always same `pointerId` with `isPrimary=true`, for all pointer events.
95
95
```
96
96
97
97
## Event: pointercancel
@@ -109,7 +109,7 @@ We'll demonstrate `pointercancel` on a practical example to see how it affects u
109
109
110
110
Let's say we're impelementing drag'n'drop for a ball, just as in the beginning of the article <info:mouse-drag-and-drop>.
111
111
112
-
Here are the flow of user actions and corresponding events:
112
+
Here is the flow of user actions and the corresponding events:
113
113
114
114
1) The user presses the mouse button on an image, to start dragging
115
115
-`pointerdown` event fires
@@ -134,7 +134,7 @@ We'd like to implement our own drag'n'drop, so let's tell the browser not to tak
134
134
We need to do two things:
135
135
136
136
1. Prevent native drag'n'drop from happening:
137
-
-Can do it by setting `ball.ondragstart = () => false`, just as described in the article <info:mouse-drag-and-drop>.
137
+
-We can do this by setting `ball.ondragstart = () => false`, just as described in the article <info:mouse-drag-and-drop>.
138
138
- That works well for mouse events.
139
139
2. For touch devices, there are also touch-related browser actions. We'll have problems with them too.
140
140
- We can prevent them by setting `#ball { touch-action: none }` in CSS.
Pointer events extend mouse events. We can replace `mouse` with `pointer` in event names and expect our code to continue working for mouse, with better support for other device types.
220
220
221
-
Remember to set `touch-events: none` in CSS for elements that we engage, otherwise the browser hijacks many types of touch interactions and pointer events won't be generated.
221
+
Remember to set `touch-events: none` in CSS for elements that we engage, otherwise the browser will hijack many types of touch interactions, and pointer events won't be generated.
222
222
223
223
Additional abilities of Pointer events are:
224
224
225
225
- Multi-touch support using `pointerId` and `isPrimary`.
226
-
- Device-specific properties, such as `pressure`, `width/height` and others.
226
+
- Device-specific properties, such as `pressure`, `width/height`, and others.
227
227
- Pointer capturing: we can retarget all pointer events to a specific element until `pointerup`/`pointercancel`.
228
228
229
-
As of now, pointer events are supported in all major browsers, so we can safely switch to them, if IE10- and Safari 12- are not needed. And even with those browsers, there are polyfills that enable the support of pointer events.
229
+
As of now, pointer events are supported in all major browsers, so we can safely switch to them, as long as IE10- and Safari 12- are not needed. And even with those browsers, there are polyfills that enable the support of pointer events.
0 commit comments