Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

PointerEvent: pointer ID and viewport coordinates

Use `pointerId` to tell simultaneously active pointers apart, and `clientX` and `clientY` to locate an event in the viewport in CSS pixels.

Version: Current web platform; runtime support varies

Syntax

event.pointerId
 event.clientX
 event.clientY

PointerEvent.pointerId/clientX/clientY refers to three read-only properties you read on a PointerEvent instance. pointerId is a numeric identifier that distinguishes a pointer from other simultaneously active pointers, such as two fingers on a screen. An ID can be reused after an interaction ends. Do not treat it as a permanent device ID or infer the device type from it. For browser-generated events, -1 indicates that no pointing device generated the event, as when a button is activated from the keyboard.

clientX (horizontal) and clientY (vertical) are inherited from MouseEvent, even for touch and pen input. They locate the event in CSS pixels from the viewport's top-left corner, increasing rightward and downward, respectively. Unlike pageX and pageY, they are not document-relative. Nor are they relative to the target element or the screen. Scrolling changes the viewport coordinates of a fixed point in the document, not the viewport's origin.

To track multiple interactions, store each starting position in a Map keyed by pointerId on pointerdown. On pointermove, look up that ID and ignore the event if there is no entry. Delete the entry on either pointerup or pointercancel. PointerEvent and the coordinate properties it inherits from MouseEvent are web-platform APIs. Neither these properties nor the example requires ECMAScript 2024. API availability depends on the runtime.

Examples

(() => {
  const target = new EventTarget();
  const starts = new Map();

  target.addEventListener("pointerdown", ({ pointerId, clientX, clientY }) => {
    starts.set(pointerId, { x: clientX, y: clientY });
  });
  target.addEventListener("pointermove", ({ pointerId, clientX, clientY }) => {
    const start = starts.get(pointerId);
    if (!start) return;
    console.log(`${pointerId}: dx=${clientX - start.x}, dy=${clientY - start.y}`);
  });
  const end = ({ pointerId }) => { starts.delete(pointerId); };
  target.addEventListener("pointerup", end);
  target.addEventListener("pointercancel", end);

  const send = (type, pointerId, clientX, clientY) => {
    const fields = { pointerId, clientX, clientY };
    // Without PointerEvent, use a plain Event to exercise the same listeners.
    const event = typeof PointerEvent === "function"
      ? new PointerEvent(type, fields)
      : Object.assign(new Event(type), fields);
    target.dispatchEvent(event);
  };

  send("pointerdown", 7, 100, 80);
  send("pointerdown", 8, 200, 150);
  send("pointermove", 7, 112, 75);
  send("pointermove", 8, 197, 159);
  send("pointercancel", 8, 197, 159);
  send("pointermove", 8, 198, 160);
  send("pointerup", 7, 112, 75);
  console.log(`tracked=${starts.size}`);
})();

Result

7: dx=12, dy=-5
8: dx=-3, dy=9
tracked=0

Run this in Node.js or in a browser console. If `PointerEvent` is unavailable, the snippet dispatches plain `Event` objects carrying the three fields to exercise the same listeners. The synthetic events have `isTrusted === false` and do not move a physical pointer. The code logs displacement from the start of each interaction. The move for pointer `8` after cancellation is ignored; `tracked=0` means no entries remain.

Related lessons

  • JavaScript · Lesson 35 · Advanced · Version: 4

Sources