Much of today's web content assumes the user's pointing device will be a mouse. However, since many devices support other types of pointing input devices, such as pen/stylus and touch surfaces, extensions to the existing pointing device event models are needed. Pointer events address that need.
Note: Pointer events are not available in Web Workers.
Pointer events are DOM events that are fired for a pointing device. They are designed to create a single DOM event model to handle pointing input devices such as a mouse, pen/stylus or touch (such as one or more fingers).
The pointer is a hardware-agnostic device that can target a specific set of screen coordinates. Having a single event model for pointers can simplify creating websites and applications and provide a good user experience regardless of the user's hardware. However, for scenarios when device-specific handling is desired, pointer events defines a pointerType property to inspect the device type which produced the event.
The events needed to handle generic pointer input are analogous to mouse events (mousedown/pointerdown, mousemove/pointermove, etc.). Consequently, pointer event types are intentionally similar to mouse event types.
Additionally, a pointer event contains the usual properties present in mouse events (client coordinates, target element, button states, etc.) in addition to new properties for other forms of input: pressure, contact geometry, tilt, etc. In fact, the PointerEvent interface inherits all of the MouseEvent properties, thus facilitating the migration of content from mouse events to pointer events.
Terminology
active buttons state
The condition when a pointer has a non-zero value for the buttons property. For example, in the case of a pen, when the pen has physical contact with the digitizer, or at least one button is pressed while hovering.
active pointer
Any pointer input device that can produce events. A pointer is considered active if it can still produce further events. For example, a pen that is a down state is considered active because it can produce additional events when the pen is lifted or moved.
digitizer
A sensing device with a surface that can detect contact. Most commonly, the sensing device is a touch-enabled screen that can sense input from an input device such as a pen, stylus, or finger. Some sensing devices can detect the close proximity of the input device, and the state is expressed as a hover following the mouse.
hit test
The process the browser uses to determine a target element for a pointer event. Typically, this is determined by considering the pointer's location and also the visual layout of elements in a document on screen media.
pointer
A hardware-agnostic representation of input devices that can target a specific coordinate (or set of coordinates) on a screen. Examples of pointer input devices are mouse, pen/stylus, and touch contacts.
pointer capture
Pointer capture allows the events for a pointer to be retargeted to a particular element other than the normal hit test result of the pointer's location.
the normalized pressure of the pointer input in the range of 0 to 1, where 0 and 1 represent the minimum and maximum pressure the hardware is capable of detecting, respectively.
The normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1, where 0 is the neutral position of the control.
The plane angle (in degrees, in the range of -90 to 90) between the Y–Z plane and the plane containing both the pointer (e.g. pen stylus) axis and the Y axis.
the plane angle (in degrees, in the range of -90 to 90) between the X–Z plane and the plane containing both the pointer (e.g. pen stylus) axis and the X axis.
Indicates if the pointer represents the primary pointer of this pointer type.
Event types and Global Event Handlers
Pointer events have ten event types, seven of which have similar semantics to their mouse event counterparts (down, up, move, over, out, enter, and leave).
Fired when a pointer is moved into the hit test boundaries of an element or one of its descendants, including as a result of a pointerdown event from a device that does not support hover (see pointerdown).
Fired for several reasons including: pointer is moved out of the hit test boundaries of an element; firing the pointerup event for a device that does not support hover (see pointerup); after firing the pointercancel event (see pointercancel); when a pen stylus leaves the hover range detectable by the digitizer.
Fired when a pointer is moved out of the hit test boundaries of an element. For pen devices, this event is fired when the stylus leaves the hover range detectable by the digitizer.
This method releases (stops) pointer capture that was previously set for a specific pointer event.
Navigator extension
The Navigator.maxTouchPoints property is used to determine the maximum number of simultaneous touch points that are supported at any single point in time.
Examples
This section contains examples of basic usage of using the pointer events interfaces.
Registering event handlers
This example registers a handler for every event type for the given element.
This example illustrates accessing all of a pointer event's properties.
html
<htmllang="en"><script>const id =-1;functionprocess_id(event){// Process this event based on the event's identifier}functionprocess_mouse(event){// Process the mouse pointer event}functionprocess_pen(event){// Process the pen pointer event}functionprocess_touch(event){// Process the touch pointer event}functionprocess_tilt(tiltX, tiltY){// Tilt data handler}functionprocess_pressure(pressure){// Pressure handler}functionprocess_non_primary(event){// Non primary handler}functiondown_handler(ev){// Calculate the touch point's contact areaconst area = ev.width * ev.height;// Compare cached id with this event's id and process accordinglyif(id === ev.identifier)process_id(ev);// Call the appropriate pointer type handlerswitch(ev.pointerType){case"mouse":process_mouse(ev);break;case"pen":process_pen(ev);break;case"touch":process_touch(ev);break;default:
console.log(`pointerType ${ev.pointerType} is not supported`);}// Call the tilt handlerif(ev.tiltX !==0&& ev.tiltY !==0)process_tilt(ev.tiltX, ev.tiltY);// Call the pressure handlerprocess_pressure(ev.pressure);// If this event is not primary, call the non primary handlerif(!ev.isPrimary)process_non_primary(ev);}functioninit(){const el = document.getElementById("target");// Register pointerdown handler
el.onpointerdown = down_handler;}</script><bodyonload="init();"><divid="target">Touch me…</div></body></html>
Determining the Primary Pointer
In some scenarios there may be multiple pointers (for example a device with both a touchscreen and a mouse), or a pointer that supports multiple contact points (for example a touchscreen that supports multiple finger touches). The application can use the isPrimary property to identify a master pointer among the set of active pointers for each pointer type. If an application only wants to support a primary pointer, it can ignore all pointer events that are not primary.
A mouse has only one pointer, so it will always be the primary pointer. For touch input, a pointer is considered primary if the user touched the screen when there were no other active touches. For pen and stylus input, a pointer is considered primary if the user's pen initially contacted the screen when there were no other active pens contacting the screen.
Determining button states
Some pointer devices (such as mouse and pen) support multiple buttons, and the button presses can be chorded (i.e. pressing an additional button while another button on the pointer device is already pressed).
To determine the state of button presses, pointer events uses the button and buttons properties of the MouseEvent interface (that PointerEvent inherits from).
The following table provides the values of button and buttons for the various device button states.
Device Button State
button
buttons
Neither buttons nor touch/pen contact changed since last event
-1
—
Mouse move with no buttons pressed, Pen moved while hovering with no buttons pressed
—
0
Left Mouse, Touch Contact, Pen contact
0
1
Middle Mouse
1
4
Right Mouse, Pen barrel button
2
2
X1 (back) Mouse
3
8
X2 (forward) Mouse
4
16
Pen eraser button
5
32
Note: The button property indicates a change in the state of the button. However, as in the case of touch, when multiple events occur with one event, all of them have the same value.
Capturing the pointer
Pointer capture allows events for a particular pointer event to be re-targeted to a particular element instead of the normal hit test at a pointer's location. This can be used to ensure that an element continues to receive pointer events even if the pointer device's contact moves off the element (for example by scrolling).
The following example shows pointer capture being set on an element.
html
<htmllang="en"><script>functiondownHandler(ev){let el = document.getElementById("target");// Element 'target' will receive/capture further events
el.setPointerCapture(ev.pointerId);}functioninit(){let el = document.getElementById("target");
el.onpointerdown = downHandler;}</script><bodyonload="init();"><divid="target">Touch me…</div></body></html>
The following example shows a pointer capture being released (when a pointercancel event occurs. The browser does this automatically when a pointerup or pointercancel event occurs.
html
<htmllang="en"><script>functiondownHandler(ev){let el = document.getElementById("target");// Element "target" will receive/capture further events
el.setPointerCapture(ev.pointerId);}functioncancelHandler(ev){let el = document.getElementById("target");// Release the pointer capture
el.releasePointerCapture(ev.pointerId);}functioninit(){let el = document.getElementById("target");// Register pointerdown and pointercancel handlers
el.onpointerdown = downHandler;
el.onpointercancel = cancelHandler;}</script><bodyonload="init();"><divid="target">Touch me…</div></body></html>
touch-action CSS property
The touch-action CSS property is used to specify whether or not the browser should apply its default (native) touch behavior (such as zooming or panning) to a region. This property may be applied to all elements except: non-replaced inline elements, table rows, row groups, table columns, and column groups.
A value of auto means the browser is free to apply its default touch behavior (to the specified region) and the value of none disables the browser's default touch behavior for the region. The values pan-x and pan-y, mean that touches that begin on the specified region are only for horizontal and vertical scrolling, respectively. The value manipulation means the browser may consider touches that begin on the element are only for scrolling and zooming.
In the following example, the browser's default touch behavior is disabled for the div element.
In the following example, default touch behavior is disabled for some button elements.
css
button#tiny{touch-action: none;}
In the following example, when the target element is touched, it will only pan in the horizontal direction.
css
#target{touch-action: pan-x;}
Compatibility with mouse events
Although the pointer event interfaces enable applications to create enhanced user experiences on pointer enabled devices, the reality is the vast majority of today's web content is designed to only work with mouse input. Consequently, even if a browser supports pointer events, the browser must still process mouse events so content that assumes mouse-only input will work as is without direct modification. Ideally, a pointer enabled application does not need to explicitly handle mouse input. However, because the browser must process mouse events, there may be some compatibility issues that need to be handled. This section contains information about pointer event and mouse event interaction and the ramifications for application developers.
The browser may map generic pointer input to mouse events for compatibility with mouse-based content. This mapping of events is called compatibility mouse events. Authors can prevent the production of certain compatibility mouse events by canceling the pointerdown event but note that:
Mouse events can only be prevented when the pointer is down.
Hovering pointers (e.g. a mouse with no buttons pressed) cannot have their mouse events prevented.
The mouseover, mouseout, mouseenter, and mouseleave events are never prevented (even if the pointer is down).
Best practices
Here are some best practices to consider when using pointer events:
Minimize the amount of work performed in event handlers.
Add the event handlers to a specific target element (rather than the entire document or nodes higher up in the document tree).
The target element (node) should be large enough to accommodate the largest contact surface area (typically a finger touch). If the target area is too small, touching it could result in firing other events for adjacent elements.
79The method always returns an empty array, regardless of the user's actions.
43
No
7.0
getPredictedEvents
77
79
89
No
64
No
77
77
89
55
No
12.0
height
55
12
59
1110Returns values in screen pixels instead of CSS document pixels.
42
13
55
55
79
42
13
6.0
isPrimary
55
12
59
10
42
13
55
55
79
42
13
6.0
pointerId
55
12
59
10
42
13
55
55
79
42
13
6.0
pointerType
55
12
59
1110Returns an integer enumeration instead of a string.
42
13
55
55
79
42
13
6.0
pressure
55
12
59
1110Returns 0 instead of 0.5 on hardware that doesn't support pressure.
42
13
55
55
79
42
13
6.0
tangentialPressure
57
79
59
No
44
13
57
57
79
43
13
7.0
tiltX
55
12
59
10
42
13
55
55
79
42
13
6.0
tiltY
55
12
59
10
42
13
55
55
79
42
13
6.0
twist
57
18
59
No
44
13
57
57
79
43
13
7.0
width
55
12
59
1110Returns values in screen pixels instead of CSS document pixels.
42
13
55
55
79
42
13
6.0
Some additional values have been defined for the CSS touch-action property as part of the Pointer Events specification, but currently those values have limited implementation support.