Switching to pointer events
It is easy to think that everything will work with mouse clicks on the web. However, many devices support other types of pointing input devices, such as pen/stylus and touch surfaces so we need a way to work with all of them without writing duplicate code. According to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events): > The [pointer](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events#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 Web sites 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 define a [pointerType property](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) 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 For this equivalency testing, we'll work with basic pointer events. The idea is that these pointer events are compatible with existing code (pointer events will also fire mouse compatibility events as described in [Compatibility mapping with mouse events](https://www.w3.org/TR/pointerevents3/#compatibility-mapping-with-mouse-events)) and will work with future code. The following table shows the pointer events we will work with. | Event | On Event Handler | Bubbles | Cancelable | Default Action | Description | | --- | --- | --- | --- | --- | --- | | [pointerover](https://developer.mozilla.org//en-US/docs/Web/Events/pointerover) | [onpointerover](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerover) | Yes | Yes | None | Fired when a pointer is moved into an element's hit test boundaries. | | [pointerenter](https://developer.mozilla.org//en-US/docs/Web/Events/pointerenter) | [onpointerenter](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerenter) | No | No | None | 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). | | [pointerdown](https://developer.mozilla.org//en-US/docs/Web/Events/pointerdown) | [onpointerdown](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerdown) | Yes | Yes | Varies: when the pointer is primary, all default actions of the mousedown event.| Canceling this event also prevents subsequent firing of compatibility mouse events.
Fired when a pointer becomes ***active***. | | [pointermove](https://developer.mozilla.org//en-US/docs/Web/Events/pointermove) | [onpointermove](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointermove) | Yes | Yes | Varies: when the pointer is primary, all default actions of mousemove | Fired when a pointer changes coordinates. This event is also used if the change in pointer state can not be reported by other events. | | [pointerup](https://developer.mozilla.org//en-US/docs/Web/Events/pointerup) | [onpointerup](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerup) | Yes | Yes | Varies: when the pointer is primary, all default actions of mouseup | Fired when a pointer is no longer *active*. | | [pointercancel](https://developer.mozilla.org//en-US/docs/Web/Events/pointercancel) | [onpointercancel](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointercancel) | Yes | No | None | A browser fires this event if it concludes the pointer will no longer be able to generate events (for example the related device is deactived). | | [pointerout](https://developer.mozilla.org//en-US/docs/Web/Events/pointerout) | [onpointerout](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerout) | Yes | Yes | None | 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. | | [pointerleave](https://developer.mozilla.org//en-US/docs/Web/Events/pointerleave) | [onpointerleave](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onpointerleave) | No | No | None | 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. | | [gotpointercapture](https://developer.mozilla.org//en-US/docs/Web/Events/gotpointercapture) | [ongotpointercapture](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/ongotpointercapture) | Yes | No | None | Fired when an element receives pointer capture. | | [lostpointercapture](https://developer.mozilla.org//en-US/docs/Web/Events/lostpointercapture) | [onlostpointercapture](https://developer.mozilla.org//en-US/docs/Web/API/GlobalEventHandlers/onlostpointercapture) | Yes | No | None | Fired after pointer capture is released for a pointer. | The idea is that pointer events will work regardless of the platform. Where necessary, pointer events will fire mouse events. Furthermore, we can query the event to get more information about the type of pointer device used and react accordingly. ## The HTML code For the examples in the post, we'll use the following HTML code. It's a simple div with a class of Box and an `h1` element inside it. ```html