Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

PointerEvent — pointer input and capture

PointerEvent unifies mouse, touch, and pen input. Learn how pointer identifiers, viewport coordinates, interaction lifecycles, and pointer capture work together when implementing dragging.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new PointerEvent(type, options)
element.addEventListener('pointerdown', handler)
element.setPointerCapture(event.pointerId)
element.releasePointerCapture(event.pointerId)

PointerEvent is a browser Web API interface extending MouseEvent, shared by mouse, touch, and pen input. It is not a feature of a particular ECMAScript edition. Check availability with 'PointerEvent' in window; Node 24 in a repository does not establish browser support.

PointerEvent.pointerId identifies an active pointer; identifiers can later be reused and are not permanent device identities. PointerEvent.isPrimary identifies the primary pointer of its type, not a single global pointer. Mouse and touch can therefore each have a primary pointer simultaneously. PointerEvent.clientX and PointerEvent.clientY, inherited from MouseEvent, provide viewport-relative coordinates in CSS pixels, not element-local or document-origin coordinates.

Pointer lifecycle and capture events: the interaction includes pointerdown, pointermove, and termination through pointerup or pointercancel. Clear interaction state on cancellation too, for example when the browser takes over a gesture. pointerover/pointerout and pointerenter/pointerleave describe element boundaries; they do not replace interaction termination.

Calling element.setPointerCapture(event.pointerId) during a real pointerdown directs subsequent events for that pointer to the element even outside its bounds. gotpointercapture and lostpointercapture report capture acquisition and loss; element.releasePointerCapture(event.pointerId) releases it explicitly. Release also follows pointerup or pointercancel. Touch input may receive capture implicitly. Handle capture loss to avoid leaving a drag active.

For custom gestures, declare appropriate CSS touch-action before interaction starts; preventDefault() alone does not block viewport panning and zooming. The example deliberately dispatches synthetic events: it checks fields and state cleanup but does not trigger real hardware input or pointer capture.

Examples

const target = document.createElement('div');
const active = new Map();
const lines = [];

for (const type of ['pointerdown', 'pointermove', 'pointerup', 'pointercancel']) {
  target.addEventListener(type, (event) => {
    const { pointerId, isPrimary, clientX, clientY } = event;
    if (type === 'pointerdown') active.set(pointerId, true);
    if (!active.has(pointerId)) return;
    lines.push(`${type} ${pointerId} ${isPrimary} ${clientX},${clientY}`);
    if (type === 'pointerup' || type === 'pointercancel') {
      active.delete(pointerId);
    }
  });
}

for (const [type, clientX, clientY] of [
  ['pointerdown', 10, 20],
  ['pointermove', 15, 25],
  ['pointerup', 15, 25]
]) {
  target.dispatchEvent(new PointerEvent(type, {
    pointerId: 7, pointerType: 'touch', isPrimary: true,
    clientX, clientY
  }));
}
console.log(lines.join('\n'));
console.log(`active=${active.size}`);

Result

pointerdown 7 true 10,20
pointermove 7 true 15,25
pointerup 7 true 15,25
active=0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources