Speech bubble at the pointer

A bubble that follows the pointer, jumps to the other side at an edge and points its tail at the pointer.

Demo

Try this: Move the pointer over the dashed box, or touch it with a finger: a speech bubble follows. Go towards the right or bottom edge: the bubble jumps to the other side and its tail changes the corner. The Calculated values show the placement (itemAt).

How it works

  1. The bubble is the item, the dashed box is the boundary, and the target is not an element: it is the pointer event. The library takes the position of the pointer from it.
  2. The example sets no itemAt and targetAt, so the top left corner of the bubble goes to the pointer. targetOffset with x: 16 and y: 16 moves it away from the pointer, so the bubble does not cover the pointer.
  3. flip: "both" puts the bubble on the other side of the pointer when it would leave the box at the right or bottom edge. mirror: true turns the offset around with it, so the gap stays 16 pixels. stick: "all" pulls the bubble back inside when neither side fits completely.
  4. itemAt in the result is the corner of the bubble that sits at the pointer after the flip. The example writes it into data-at, and the CSS draws the tail at that corner.
  5. The pointer event is an option of the constructor, so the example creates a new calculator for every event. This also reads the current size of the bubble each time.

Touch screens

The example listens to pointerdown and pointermove and passes the pointer event, as the limits recommend. A TouchEvent would give NaN. A tap shows the bubble at the finger, and a moving finger takes it along. The bubble disappears when the finger is lifted.

There is one more thing for a finger: the browser scrolls the page on a swipe, and then it cancels the pointer after the first move. The example has touch-action: none on the box, so the finger moves the bubble instead, and a swipe that starts inside the box does not scroll the page. In Chrome with touch emulation, a tap in the example at 100/60 and at 300/170 gave the same positions as the mouse (116/76 and 144/122), and a swipe moved the bubble. With touch-action: auto the same swipe scrolled the page and cancelled the pointer after the first move, and the bubble disappeared. Other browsers and real devices are not tested.

Variations

See also