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).
Calculated values
How it works
- 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.
-
The example sets no
itemAtandtargetAt, so the top left corner of the bubble goes to the pointer.targetOffsetwithx: 16andy: 16moves it away from the pointer, so the bubble does not cover the pointer. -
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: trueturns the offset around with it, so the gap stays 16 pixels.stick: "all"pulls the bubble back inside when neither side fits completely. -
itemAtin the result is the corner of the bubble that sits at the pointer after the flip. The example writes it intodata-at, and the CSS draws the tail at that corner. - 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
-
In the playground, remove
flipandstickand move to the right edge: the bubble leaves the box.distance.overflowin the result says which edge. -
Set
itemAtto"bottom center"andtargetOffsetto{ x: 0, y: -24, mirror: true }for a bubble that sits above the pointer and goes below it at the top edge.