Dragging Cards With Pointer Events: Touch, Mouse and Keys
Play5s moves a solitaire card with Pointer Events: one set of handlers serves a finger, a pen and a mouse, and a press that travels less than 6 pixels counts as a tap, not a drag. The pressed card keeps the pointer through capture, follows it with a CSS transform, and on release lands in the nearest column or foundation within 1.3 card widths when the move is legal. Otherwise it slides back. A keyboard path built on the same rules covers players who cannot drag.
This note follows the Klondike table; most Play5s card tables use the same pattern.
Why use Pointer Events instead of touch and mouse handlers?
A card game that listens for mouse events and touch events separately has to guess which one produced a press, and browsers add emulated mouse events after a touch. MDN describes Pointer Events as one model for input from a mouse, a pen or a finger, with a pointerType property for the few places where the difference matters. The Klondike table never needs to ask which device it is: the same four handlers run for all three.
The handlers sit on the board element, not on each card. There are 52 cards and four listeners: pointerdown, pointermove, pointerup and pointercancel. A press is traced back to its card with closest('.kl-card'), and the card only takes part if it carries a data-where attribute, which the renderer sets on face-up cards that can move. The engine then answers the real question, which cards would travel with this one: pickUp returns the pressed card and every card below it in its column, and an empty list for a face-down card.
board.addEventListener('pointerdown', (e) => {
const el = (e.target as HTMLElement).closest<HTMLElement>('.kl-card');
if (!el || !el.dataset.where) return;
const w = JSON.parse(el.dataset.where) as Where;
const cards = pickUp(state, w);
if (!cards.length) return;
e.preventDefault();
drag = { w, cards, x0: e.clientX, y0: e.clientY, moved: false, el, base: cards.map(posOf) };
el.setPointerCapture(e.pointerId);
});
Nothing is stored on the card elements except the position they already have.
What do pointer capture and touch-action do on a phone?
The last line of the handler is the one that makes touch play work. setPointerCapture routes every later event of that pointer to the element that called it, even after the finger leaves the element's box. A card follows the finger one frame late, so a quick flick would otherwise end with the finger over a neighbour, and the release would arrive at the wrong target.
Capture does not stop the browser from scrolling the page, and a scroll takes the pointer away. The cards that can move therefore carry one CSS rule:
.kl-card.is-up[role="button"] { cursor: grab; touch-action: none; }
MDN's touch-action page explains that the property tells the browser which touch gestures it may handle itself. With none, a press on a movable card never starts a pan or a zoom, so the page does not scroll under the player's thumb. The felt and the face-down cards keep the default, so a swipe that starts there still scrolls the page on a short phone. The board also sets user-select: none and a transparent tap highlight, so a long press neither selects text nor flashes a grey box.
If the browser takes the gesture anyway, as it can for a system swipe, it fires pointercancel. The handler drops the drag state and calls render(), which lays every card out again from the game state. No half-moved transform survives, because the layout never trusted it.
How does a tap tell itself apart from a drag?
A finger is never perfectly still between touching and lifting, so a tap needs some slack. The drag object carries a moved flag, and the move handler ignores everything until the pointer is 6 pixels from where it started:
const dx = e.clientX - drag.x0, dy = e.clientY - drag.y0;
if (!drag.moved && Math.hypot(dx, dy) < 6) return;
if (!drag.moved) sfx('lift');
drag.moved = true;
The first real movement plays the soft lift sound described in the note on game sound. On release the flag decides what happened. A press that never moved is a tap, and the card is sent to its best place, which the next note, One tap, best place, covers. A press that moved is a drag, and the drop logic below takes over.
Committing the move on release, not on press, matches the idea behind WCAG's pointer cancellation criterion: a wrong press costs nothing as long as the action only happens when the pointer comes up, and the player can still abort by letting go over nothing. WCAG 2.2 also asks that anything done by dragging can be done without dragging, and the tap and the keyboard path give that. The WAI explanation of dragging movements has the wording.
How does the card follow the finger?
Every card is absolutely positioned at the top left of the board, and its place is a transform, translate(x px, y px), written into the inline style. A CSS transition on transform animates deals and undone moves in about 0.2 seconds. During a drag the is-drag class sets transition: none, so the card tracks the finger with no lag.
The handler does not read positions from a layout table. posOf parses the current translate out of each card's style with a regular expression, and pointerdown stores those positions as base. While the pointer moves, each card in the run is set to its base position plus the pointer's offset, and its z-index is 500 plus its index in the run, so the whole run travels above the table in its original order. Only transform changes. The browser can move a transformed element without laying the page out again, so dragging a run of a dozen cards stays cheap on an old phone.
The same trick makes the cancel path short. Rendering from state means the base position is always recoverable, so a dropped card needs no animation code of its own: render() sets the original transform, the transition is back on, and the card slides home.
Where does the card land when the finger lifts?
There is no hit test under the finger. A fingertip covers the card it moves, and what the player lines up is the card, not the fingertip. So the drop logic takes the centre of the lead card in board coordinates, its base position plus the pointer offset plus half a card, and measures the distance to eleven candidates: the four foundation slots and the seven columns, each taken at the vertical middle of its last card, or of its empty slot.
let best = null, bestD = Infinity;
// four foundations, then seven columns: centre of the slot or of the column's last card
const consider = (d, cx, cy) => {
const dd = Math.hypot(px - cx, py - cy);
if (dd < bestD) { bestD = dd; best = d; }
};
return bestD < L.cw * 1.3 ? best : null;
The nearest candidate wins, and only if it is closer than 1.3 card widths. Then the engine's canDrop has the last word. A foundation takes a single card of its own suit with the next rank. A column takes a card of the opposite colour with a rank one lower, and an empty column takes only a king. If the move is legal, move() returns the new state and the game commits it. If not, render() sends the card home.
A drop on bare felt, with no candidate in range, is silent. A drop on a column that refuses the card plays the soft refusal sound from the sound note. Letting go in the wrong place is a normal part of a drag, and the game only complains when the player aimed at something.
What does a player without a pointer do?
The same engine rules drive a keyboard path, so it never disagrees with the mouse. Enter or Space on a face-up card picks it up with the run below it and sets aria-pressed. The arrow keys then step through validTargets, the list of every foundation and column where canDrop is true, skipping the card's own column and a king that already sits at the base of a column. Each target is highlighted in turn, and a live region reads out where the card would go, in a line that ends "Enter to confirm". Enter puts the card down, Escape cancels, and a second Enter without choosing a place sends the card to its best place, just as a tap does.
A pick-up that has no legal target does not fail silently: the card shakes and the message says that card cannot move anywhere. MDN's guide to ARIA live regions covers the mechanism that lets a screen reader announce those lines without moving focus. A capture-phase pointerdown on the board clears any keyboard selection, so a player who switches to the mouse mid-move never ends up with two cards picked up.
The keys themselves are listed in the guide's page on keyboard shortcuts. The quickest place to feel all of this is any of the ten solitaire tables open on 27 November 2026: try a drag that ends on bare felt, then one that ends a little too far from any column, and listen for the difference.