# GUI Tooltip - Linked to mouse

**URL:** <https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922>\
**Category:** Questions\
**Tags:** gui, tooltip\
**Created:** [April 16, 2023, 2:23am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922 "2023-04-16T02:23:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 16, 2023, 2:23am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/1 "2023-04-16T02:23:53Z")

</div>

Hi everyone,

Another tooltip thread, as I dont seem to have found any suitable answers.

I’m trying to have a global tooltip that shows upon hovering certain gui elements (abilities, items in inventory, etc)

I’d like to have the tooltip to the left, right, top bottom but never under the mouse cursor.

Any idea how I could achieve, I have a small tooltip class if you want to see where I’m up to

Everything is working except the position of the tooltip who is all over the place, I cannot make it follow the cursor.

> <https://github.com/oriongunning/t5c/blob/main/src/client/Controllers/UI/UI_Tooltip.ts>

[https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/d/6d3107494fc3baadfc003aa219547c611277e80a.mp4](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/d/6d3107494fc3baadfc003aa219547c611277e80a.mp4)

Hopefully, this is not too much of a dumb question 🙂

Cheers,  
Orion

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 17, 2023, 3:24pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/3 "2023-04-17T15:24:37Z")

</div>

Adding @carolhmj our GUI guru

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 17, 2023, 5:26pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/4 "2023-04-17T17:26:26Z")

</div>

It would be easier to know what’s going on with a playground reproduction, but I saw that you add the tooltipContainer in something called \_playerUI, and if that’s not the top level ADT, your tooltip’s coordinates will be offset by whatever \_playerUI is offset. If that’s the case, you’ll have to apply the inverse of this offset to your tooltip.

---

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 17, 2023, 11:43pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/5 "2023-04-17T23:43:33Z")

</div>

Hi,

Thanks Carol, to simplify this, I’ve did a quick playground to show what I mean,

> **[Babylon.js Playground](https://playground.babylonjs.com/#NEF5HL%2322)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

I would like the tooltip to follow my mouse as I hover over any of the black boxes, or alternatively use the gui element provided in the showTooltip function to position the tooltip to any side of that provided gui element.

Make sense?

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 18, 2023, 11:52am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/6 "2023-04-18T11:52:42Z")

</div>

I have a mouse following example here: [GUI Tooltip example | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#8WXN3S#2)

---

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 19, 2023, 1:57am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/7 "2023-04-19T01:57:31Z")

</div>

Thanks Carol,

I feel so dumb!

All the issues I was having with ui & placement that’s been plaguing me since I started with Babylon is to do with this line…

`this._playerUI.idealHeight = 720;`

As soon as I removed it, everyhting is working perfectly and positionning itself as you would expect.

Can you explain?

Updated playground with what I was trying to achieve: [https://playground.babylonjs.com/#NEF5HL#73](https://playground.babylonjs.com/#NEF5HL#73)

Not quite sure why the adaptHeightToChildren is not working on PG?

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 19, 2023, 8:39am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/8 "2023-04-19T08:39:13Z")

</div>

> [@oriongu](#):
>
> Not quite sure why the adaptHeightToChildren is not working on PG

I think it’s because it should read ‘adaptHeightToChild’ in case of a textBlock. It doesn’t seem to be able to recalculate from ‘stacked’ tb. Usually what I do is that either I use a grid OR if the container needs to dynamically change size (like if sometimes there is no description and it needs to adapt), I use a stackpanel with no height as the parent container. It will change height from the children containers (other stackpanels or rectangles). Then, I would constrain each textBlock I want to stack inside a container.  
But then, if you have only tiltle and description, you could also just make a string, like:

> var tbtext = “Item Title\n” +  
> “Item Description”;

If you have only one textBlock then you will not need the container to stack it.

Edit: Something like that:

> **[Babylon.js Playground](https://playground.babylonjs.com/#NEF5HL%2374)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Edit1: Sorry rushed it a little. I had to go for some shopping 😅 Now I’m back and quickly made a more suitable version I believe. Here it will resize no matter if there is a description or not and no matter the length of the description.

> **[Babylon.js Playground](https://playground.babylonjs.com/#NEF5HL%2375)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Hope this helps,

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 19, 2023, 11:14am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/9 "2023-04-19T11:14:08Z")

</div>

> [@oriongu](#):
>
> `this._playerUI.idealHeight = 720;`
> 
> As soon as I removed it, everyhting is working perfectly and positionning itself as you would expect.
> 
> Can you explain?

[There’s a good explanation here on the docs](https://doc.babylonjs.com/features/featuresDeepDive/gui/gui#adaptive-scaling)

---

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 19, 2023, 11:17am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/10 "2023-04-19T11:17:09Z")

</div>

Ok, thanks Mawa, make sense,

I’ve updated the playground to reflect a more detailled tooltip for anyone stumbling onto this topic: [https://playground.babylonjs.com/#NEF5HL#101](https://playground.babylonjs.com/#NEF5HL#101)

@carolhmj thanks, i’ll have a read on that now

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 19, 2023, 11:21am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/11 "2023-04-19T11:21:42Z")

</div>

Cool. Thanks a lot for this effort. 🤗 I know I sometimes don’t take enough time for the polishing 🤭 I’m sure this will come handy for others. Have a great day 🕶

---

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 20, 2023, 4:48am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/12 "2023-04-20T04:48:00Z")

</div>

Hey @carolhmj

To answer original post correctly, I need a little more help,

I’ve read the docs mutliple times, and it seems to me like I need to add a multiplier or something to my tooltip position to cater for the adaptive scaling that is done automatically by babylon. (if I understand correctly, that is why everything is out of whack)

If I added a ideal width and height of 1024x768 to the PG, any idea how I would correct the position of the tooltip?

> **[Babylon.js Playground](https://playground.babylonjs.com/#NEF5HL%23109)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Many thanks,

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 20, 2023, 10:54am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/13 "2023-04-20T10:54:27Z")

</div>

I have a question/issue similar to this. I also still do not fully understand how ideal width and height works just exactly. It seems to be desync from the resize. I’m not trying to place tooltips but I’m using ‘delegatePickingToChildren’ and ‘detectPointerOnOpaqueOnly’ which obviously calls on the image/pixels coordinates. And although visually, my image shows correctly, depending on how the canvas is scaled the pointer is detected or not. It also seems to work ‘eratically’, don’t know how to explain it. There’s something I either don’t understand or happening under the hood that might just not be always what one would expect (or as I said I don’t understand it - despite from all the hours I already spent on it an then despite from reading the docs I don’t know how many times).

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 20, 2023, 11:44am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/14 "2023-04-20T11:44:09Z")

</div>

Hmm I experimented a bit on my example, and it seems you can correct the mouse position by the ratio between the current width/height and ideal width/height: [GUI Tooltip example | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#8WXN3S#3)

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 20, 2023, 12:13pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/15 "2023-04-20T12:13:16Z")

</div>

Huh, sorry but that’s not what I see (SS below). But then I don’t know about @oriongu but on my side, I was pretty much willing not to bother the Team today and let you focus on the launch 🚀  
My popcorn 🍿 🍿 is ready and so am I 🤓 🧒

 ![Capture d’écran 2023-04-20 à 14.11.14](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/a/6af89b8a1457060f2d07fd69730df6d8362082e6.png)  
 ![Capture d’écran 2023-04-20 à 14.01.37](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/d/6d04def2e58370acaaf0e662ec9e8aad2e6b35b0.png)  
 ![Capture d’écran 2023-04-20 à 14.01.26](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/5/95cfc220a8421d2c7c37709e03164b8cdee24cde.png)

---

<div class="post-metadata">

**Author:** ![oriongu](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/oriongu/32/40402_2.png) [@oriongu](https://forum.babylonjs.com/u/oriongu)\
**Post date:** [April 21, 2023, 3:18am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/16 "2023-04-21T03:18:30Z")

</div>

My two cents: I’m curious, because I would have expected any positions to be updated automatically when using adaptive scaling. What’s the point of having position values thats does not reflect whats on the screen? If I understand correctly, everytime I would like to find/use a position of a GUI element, I would need to apply some sort of ratio fix to get the “real” position. I’m probably missing something completely obvious, and apologize if that’s the case. 😃

In the sake of reducing complexity on my project, I will avoid adaptive scaling for the moment.

… and focus on this very exciting new version! GO BABYLON V6! 🙂 please share the popcorn @mawa🍿 🍿 🍿 🍿 🍿 🍿 🍿 🍿

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 21, 2023, 10:21am UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/17 "2023-04-21T10:21:51Z")

</div>

> [@oriongu](#):
>
> What’s the point of having position values thats does not reflect whats on the screen?

Yes, same here. I just can’t figure it. 😬  
I don’t want to ‘pollute’ your topic but I sort of feel this must be related (sry, if I’m wrong).

Here’s a link to my sketchy wheely type GUI case scenario.  
Click on the top icon to the bottom right to show the wheel menu. Click on the right ‘walk’ or ‘run’ icons to expand. These all use picking from the image and detect pointer on opaque. Next, enlarge the window or click on the full-screen button (bottom right) and eventually be able or not to click on the ‘walk’ or ‘run’ expanded buttons. I tried n versions of setting or not an ideal size, coupled with scaleTo and whatever I know options do exist but no, I just can’t get this one setting that would fit all.

> **[Babylon.js Playground](https://playground.babylonjs.com/#UAD9TI%23100)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 24, 2023, 12:26pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/18 "2023-04-24T12:26:26Z")

</div>

Ooh, thanks for checking, I think this is related to the screen size :o Does this version work for you? [GUI Tooltip example | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#8WXN3S#6)

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 24, 2023, 12:35pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/19 "2023-04-24T12:35:33Z")

</div>

Not really, sry. But then I’m not sure what is the expected output. May be to make this easier to check, let’s pretend the tooltip should always show in the center of the hovered square.

 ![Capture d’écran 2023-04-24 à 14.31.41](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/4/54f7731a7e1f82c095e3d1fea816a653b00053f3.png)  
 ![Capture d’écran 2023-04-24 à 14.31.25](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/8/f8c11621013aac802abf4c537aed67f4712be359.png)

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 24, 2023, 12:43pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/20 "2023-04-24T12:43:10Z")

</div>

Where is your mouse pointer in these images?

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [April 24, 2023, 1:13pm UTC](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922/21 "2023-04-24T13:13:55Z")

</div>

LOL. good question. It doesn’t capture the pointer 😁  
Basically, I’m hovering towards the center of the squares. Reason why I suggested to make the tooltip appear either on the center of the hovered square… but now that I think of it, it should may be for a start simply appear in the center of the pointer. Currently, it’s offseted from the pointer either above or below AND at various position from the pointer depending on window size/ratio.

Edit: Actually (sry again @oriongu for polluting your post), Actually… may be you could start by explaining to me (to us) why we need this in the before render function to detect whether the pointer is hovering a texture or not:

> const widthRatio = gui.idealWidth / engine.getRenderWidth();  
> const heightRatio = gui.idealHeight / engine.getRenderHeight();

[Next page](https://forum.babylonjs.com/t/gui-tooltip-linked-to-mouse/39922.md?page=2)
