$ cat tips/setpointercapture-touch-lostpointercapture.md

setPointerCapture on touch: lostpointercapture ends the drag at once

On a touch screen Chrome captures the pointer by itself for the element under the finger. When you call setPointerCapture on its parent, that element loses the capture and its lostpointercapture reaches the parent too. In the handler for that event, check that event.target is your element.

handle.addEventListener("lostpointercapture", (ev) => {
  if (ev.target !== handle) return;
  endDrag();
});

I had a button that can be picked up and carried to another place on the page, with a canvas inside it. With a mouse it worked. With a finger the drag ended in the same moment it began.

On touch, Chrome captures the pointer already at pointerdown, for the element the finger landed on. For me that was the canvas. My handle.setPointerCapture(ev.pointerId) on the button took that capture away from it, so the canvas got lostpointercapture.

That event travels up the tree, through the button. And on the button I listened for it in order to end the drag when the button itself loses the pointer.

A mouse has no such automatic capture, which is why nothing showed on the desktop.

The handle also has touch-action: none in CSS, so that a moving finger does not scroll the page.

You can connect to the phone without a cable, over Wi-Fi adb.

checked: Pixel 9 Pro, Chrome on Android

$ ls ./blog/tips
all tips →
↑