How to use pointer events in JavaScript

Why only care about mouse and touchscreen input? In fact, you can handle both at the same time using pointer events .

How to use pointer events in JavaScript Picture 1

Many web applications assume that the user's pointing device will be a mouse, so they use mouse events to handle the interaction. However, with the rise of touchscreen devices, users no longer need a mouse to interact with the web. Therefore, it is necessary to support a variety of input devices to have the largest possible audience.

JavaScript has a newer standard called pointer events. It handles both mouse and touch operations, so you don't have to worry about implementing each separately.

What is a pointer event?

Pointer event - The pointer event is a web standard that defines a unified way to handle different input methods in web browsers, including mouse, touch, and pen. These events provide a consistent and platform-independent way to interact with web content across different devices.

In short, pointer events help you handle user interactions across all sources.

Pointer event types

Pointer events are named similarly to the mouse events you may be familiar with. For each mouseEvent in JavaScript, you have a corresponding pointerEvent. That means you can revisit the old code and switch 'mouse' to 'pointer' to start supporting touch and pen input.

The following table shows how pointer events differ from mouse events:

Pointer events

Mouse events

pointerdown

mousedown

pointerup

mouseup

pointermove

mousemove

pointerleave

mouseleave

pointerover

mouseover

pointerenter

mouesenter

pointerout

mouseout

pointercancel

none

gotpointercapture

none

lostpointercapture

none

You can see that there are 3 more pointer events without corresponding mouse events. You will learn more about these events later.

Using pointer events in JavaScript

You can use pointer events the same way you use mouse events. Like most event handling, this process typically follows this sample flow:

  1. Use a DOM selector to fetch an element.
  2. Use the addEventListener method , specify the event of interest and a callback function.
  3. Use the callback argument's properties and methods , an Event object .

An example of how to use the pointermove event is as follows:

// Nhận thành phần mục tiêu const target = document.getElementById('target'); // Thêm một trình nghe sự kiện vào thành phần mục tiêu target.addEventListener('pointermove', handlePointerMove); // Hàm để xử lý sự kiện di chuyển con trỏ function handlePointerMove(ev) { // Xử lý sự kiện theo cách bạn muốn target.textContent = `Moved at x: ${ev.clientX}, y: ${ev.clientY}`; }

This code block defines a target element and a JavaScript function to handle the pointermove event , then it uses a JavaScript event listener to attach the pointer event and function to the target element.

Using this code, an element with ID 'target' will display control coordinates when you move the cursor, finger or touch the pen over it.

How to use pointer events in JavaScript Picture 2

You can use other pointer events in the same way.

Pointercancel event

The pointercancel event is fired when the pointer event is interrupted before it completes its originally intended operation. Normally, this browser cancels any pointer events that may have previously been executed. There are many reasons why pointercancel might trigger, for example:

  1. When a user receives a call or other interruptive notification while dragging an element on the screen.
  2. When receiving device orientation change.
  3. When the browser window loses focus.
  4. When the user switches to another tab or application.

With the pointercancel event , you can handle these situations in the desired way. For example:

const target = document.getElementById('target'); target.addEventListener('pointercancel', (event) => { // Xử lý tình huống tại nơi hủy con trỏ. Ví dụ, bạn có thể kích hoạt ghi hình chuột. target.releasePointerCapture(event.pointerId); });

Record cursor

Pointer capture is a feature that allows a particular HTML element to capture and respond to all pointer events for a particular pointer, even if those events occur outside the boundaries of that element.

You can set pointer capture for an element using the setpointercapture() method and enable it with releasepointercapture() .

The pointer events gotpointercapture and lostpointercapture are useful in capturing pointer images.

Gotpointercapture event

The gotpointercapture event is fired whenever an element acquires a pointer. You can use this event to initialize a state for an HTML element that handles pointer events. For example, in a painting application, you can use the gotpointercapture event to set the initial position of the cursor.

lostpointercapture event

The lostpointercapture event is fired when a component loses the ability to capture the pointer. You can use it to clean up or remove any state created when the component achieves cursor rotation. In a painting application, you may want to hide the cursor.

Hope the article is useful to you.

4 ★ | 2 Vote

May be interested

  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • Cursor in CCursor in C
    pointer in the c language is easy to learn. some tasks in c language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a cursor.
  • How to use Head Pointer on a MacHow to use Head Pointer on a Mac
    head pointer is a feature in accessibility, which allows users to control the cursor on the screen with the movement of the head, through a mac camera.
  • How to broadcast communication events between components in VueHow to broadcast communication events between components in Vue
    broadcasting custom events is one way you can handle communication between components. here are detailed instructions on how to broadcast communication events between components in vue.
  • 20 outstanding IoT events by 202020 outstanding IoT events by 2020
    a bright new year is filled with exciting technology seminars and events. save the 2020 calendar of a series of interesting iot events taking place around the world!
  • How to fix the mouse pointer disappearing on Windows 10How to fix the mouse pointer disappearing on Windows 10
    does your mouse pointer disappear in the air after downloading the windows 10 build 2004 update?
  • Guide to creating events, activities on FacebookGuide to creating events, activities on Facebook
    in addition to finding new events on facebook, we can completely create events so we can gather friends right on facebook.
  • Cursor NULL in C / C ++Cursor NULL in C / C ++
    it is always a good practice to assign a null pointer to a pointer variable in case you don't know the exact address to be assigned. this is done at the time of variable declaration. a pointer that is assigned null is called a null pointer.
  • How to change the mouse pointer on Windows 11 with many styles and colorsHow to change the mouse pointer on Windows 11 with many styles and colors
    windows 11 allows you to change the color and customize the mouse pointer to your liking. the following will be instructions on how to change the mouse pointer on windows 11...