Make button's position to be fixed?


I am creating a simple button with Babylon.js. Everything works well from clicking to color etc. But when I zoom in/out the page using Control+Mouse wheel key, the button changes position. As in it does not stay fixed. Also when I view it on mobile, the button is not visible at all. So how do I make the button stay fixed to its position if you are viewing from PC, mobile or any other device. It looks as if it changes position but i think its because of the camera’s near and far field of view.

            var gui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("myUI");

            var button = BABYLON.GUI.Button.CreateSimpleButton("button_1", "Overlay");
   = "420px";
            button.left = "850px";
            button.width = "150px";
            button.height = "50px";
            button.cornerRadius = 20;
            button.thickness = 4;
            button.children[0].color = "#DFF9FB";
            button.children[0].fontSize = 24;
            button.color = "#FF7979";
            button.background = "#EB4D4B";


Pinging @msDestiny14 for help:)

On it! Will get back soon. :smiley:

Part 1:
So here is a playground I have using a similar parameters to your button. When I zoom in with the mouse it does not change the size or position.

1 Like

MsDestiny… new BJS helper… cooooool… welcome.

Probably, line 1 of that playground should not have engine in it.

More correct method… var createScene = function () {

Removing the engine from line #1… allows us to use the engine variable name… later in the code… like line 108.

Also note… there were TWO background, color, and cornerRadius settings in the button for playground #150. All these mistakes were OLD mistakes, and NOT created by @msDestiny14.

Now, let’s look MORE at playground #152 (myself and @msDestiny14 are going to have a canvas resizing pow-wow, here). #152 is preparing for canvas resizing… both vertical and horizontal. (I added another button on the left side, vertically-centered.)

There is also a canvas resize observer… active in lines 108-110. Not important at this time.

@msDestiny14 - notice that I changed the value of (I made it a negative number - line 78) and I also added button1.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM; at line 79.

Now let’s open our F12 dev tools (in desktop browser)… which causes a VERTICAL canvas resize (and you can drag the divider line up/down to do MORE vertical resizings). Notice that the bottom button moves up/down… when the JS console is opened/height-dragged? No blocked buttons. In PG version 152… opening the dev tools (F12) made the lower button disappear. (the button did not survive canvas vertical resize). But now, in PG #152, the button stays in view. This is because we used button1.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;.

Same for horizontal resize of canvas, and button 2. Line 95… button2.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;. We can drag the divider bar between the playground canvas area and editor area (resize the canvas horizontally)… and the left button automatically moves DURING the resizing. We can drag the playground’s vertical divider left/right all we wish, and the button TRIES to stay in view.

PG #152 also seems to act ok during control-mousewheel actions.

I don’t know if @saifshk17 will be asking about canvas resizing later, or not. Maybe. But if a GUI scene can survive control-mousewheel font-sizing… AND canvas resizes… then you have a super-gui… where the gui is sometimes called “bulletproof”. (bulletproof generally means… you know… difficult to mess-up). :slight_smile:

Not very exciting reading… but… now we can see some issues with canvas resizing and GUI controls (buttons, labels, containers, etc).

Canvas resizing is a troublesome situation for GUI and sometimes for “click zones”. I once had a BIG canvas resize challenge that superhero @sebavan helped me with… somewhat. It was painful but full of learning. I was trying to use BJS Gui 2d as a diagram-making tool. Limited success. :slight_smile:

PG #12 - My diagram - not surviving vert or horz canvas resize well at all.
PG #14 - Sebavan does MUCH MUCH work on it, using engine/canvas resize observers (thx Sebavan!). Not bad. The controls and connecting lines survive vert/horz canvas-resize… fairly well.
PG #16 - Here, Wingnut tries to “slew” the entire diagram across the screen as if it were a simple PICTURE/IMAGE diagram. The gui controls operate fine, but the connecting lines did not survive well. Maybe someday, I will try to advance “the GUI2d diagram” to be more-functional. But I must say… I was really torturing the diagram… and hoping it would be perfect.

Sorry for the long post. Canvas resizing is a big subject - possibly apropos to this thread, possibly not. Admin… delete/clean this post, as wanted. In many ways, it’s “too much”.