mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Create a hover effect for labels

Open Roxyi opened this issue 9 years ago • 6 comments

Hi, I am trying to create a hover effect for labels. My idea is that we can create two layers using the same label data and add them to the map. One layer will be the labels displayed on the map and the other one will be the hover effect layer. When hovering over a label, we can get the properties of that label and use properties like "id" or "name" to filter the hover effect layer. Here is what I have got so far: hover effect test

There are three problems:

  1. The hover effect does not always work.
  2. The respond of the hover effect is slow in low zoom levels. It is faster when you zoom in.
  3. The hover effect will be easily gone because I used "mouseenter" and "mouseleave" on the label layer. You may notice that if you move your mouse to the space between letters, the hover effect will be gone. Only when you move your mouse exactly on the letters, you will see the hover effect.

I wonder if there is a better way to create a hover effect for labels. Also, is there any way to create polygons like the collision boxes for the text labels.

Any help will be appreciated.

Roxyi avatar Sep 14 '17 22:09 Roxyi

That's probably the best way for now. See #200 for a request for a built-in feature for this.

jfirebaugh avatar Sep 14 '17 22:09 jfirebaugh

@jfirebaugh Thank you! The pseudo-class is already a built-in feature but not released to the public now? Where can I send a request for that built-in feature?

Roxyi avatar Sep 14 '17 22:09 Roxyi

@Roxyi It's not (yet) a built-in feature -- issue #200 is the request to add it; that would be the place to watch for further discussion/progress.

anandthakker avatar Sep 15 '17 12:09 anandthakker

Using a debounce function with layer filters can help a bit for your hover use case in the meantime @roti https://bl.ocks.org/ryanbaumann/0d72890cea4f97e0dbd10ea3cf7189b2

ryanbaumann avatar Sep 15 '17 12:09 ryanbaumann

@anandthakker Thank you! Got it.

Roxyi avatar Sep 15 '17 18:09 Roxyi

@ryanbaumann That's amazing. The debounce function helps improve the performance a lot. Thank you!

Roxyi avatar Sep 15 '17 18:09 Roxyi