# The Babylon GUI

> Learn all about the Babylon.js 2D GUI system.

Source: https://doc.babylonjs.com/features/featuresDeepDive/gui/gui

## Introduction

The Babylon.js GUI library is an extension you can use to generate interactive user interfaces.

Babylon.GUI uses a DynamicTexture to generate a fully functional user interface which is flexible and GPU accelerated.

See the demo: https://www.babylonjs.com/Demos/GUI/

> [!NOTE]
> **3D GUI**
>
> In addition to the Babylon 2D GUI system described below, with Babylon.js v3.3 and higher, you also have a <a href='https://doc.babylonjs.com/features/featuresDeepDive/gui/gui3D'>3D GUI system</a> available to leverage as well. Both systems can be used for different needs for your project.

## How To Use Babylon GUI

### CDN

The latest version can be found on our CDN at https://cdn.babylonjs.com/gui/babylon.gui.js.

> [!WARNING]
> **Warning**
>
> The CDN should not be used in production environments. The purpose of our CDN is to serve Babylon packages to users learning how to use the platform or running small experiments. Once you've built an application and are ready to share it with the world at large, you should serve all packages from your own CDN.

Here is the source code on the main Babylon.js repo: https://github.com/BabylonJS/Babylon.js/tree/master/packages/dev/gui.

### NPM

First, download the GUI package (see [npmjs.com](https://www.npmjs.com/package/@babylonjs/gui)):
```bash
npm install @babylonjs/gui
```
Then import it into your project:
```javascript
import * as GUI from '@babylonjs/gui'
```

## AdvancedDynamicTexture

To begin with Babylon.GUI, you first need an AdvancedDynamicTexture object.

Babylon.GUI has two modes:

### Fullscreen mode

In this mode, Babylon.GUI will cover the entire screen and will rescale to always adapt to your rendering resolution. It will also intercept clicks (including touches). To create an AdvancedDynamicTexture in fullscreen mode, just run this code:

```javascript
const advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("myUI");
```

Here is an example of a simple fullscreen mode GUI: [Playground: Fullscreen GUI Example](https://playground.babylonjs.com/#XCPP9Y#1) - Simple example of adding a fullscreen BabylonGUI to your scene.

By default the ratio between rendering resolution and texture size is 1. But you can force it to different values with `advancedTexture.renderScale`. This could be useful if you want crisper texts for instance.

Foreground & background:
The fullscreen mode can be rendered in either the foreground or the background of a scene.
It can be set like this:

```javascript
// true == foreground (default)
// false == background
const advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("myUI", foreground? : Boolean );
// it can also be changed on the go:
    advancedTexture.isForeground = false;
```

**Please note that only one fullscreen mode GUI is allowed per scene.**

The fullscreen mode is not intended to be used with WebVR, as it is pure 2D rendering. For WebVR scenarios, you will have to use the texture mode below.

### Texture mode

In this mode, BABYLON.GUI will be used as a texture for a given mesh. You will have to define the resolution of your texture. To create an AdvancedDynamicTexture in texture mode, just run this code:

```javascript
const advancedTexture2 = BABYLON.GUI.AdvancedDynamicTexture.CreateForMesh(myPlane, 1024, 1024);
```

Here is an example of a simple texture mode GUI: [Playground: Texture Mode GUI Example](https://playground.babylonjs.com/#ZI9AK7#1) - Simple example of adding a texture mode BabylonGUI to your scene.

Here is the same example but now using “billboardMode = all” where the GUI will always face the camera: [Playground: Texture Mode With Camera Facing GUI Example](https://playground.babylonjs.com/#ZI9AK7#1214) - Simple example of adding a texture mode BabylonGUI to your scene with camera facing.

Please note that handling pointer move events could be costly on complex meshes, so you can turn off supporting pointer move events with a fourth parameter:

```javascript
const advancedTexture2 = BABYLON.GUI.AdvancedDynamicTexture.CreateForMesh(myPlane, 1024, 1024, false);
```

Once you have an AdvancedDynamicTexture object, you can start adding controls.

## Loading from Snippet Server

Here is an example of loading an AdvancedDynamicTexture from the Snippet Server: [Playground: Loading GUI from Snippet Server](https://playground.babylonjs.com/#AJA7KA#50) - Simple demo of loading a texture from the Snippet Server and accessing the controls.

## Debugging

Starting with Babylon.js v4.0, the new inspector can help you debug your GUI by displaying bounding info and letting you dynamically change properties: [Inspector Docs](https://doc.babylonjs.com/toolsAndResources/inspectorv2)

## General properties

### Events

**Please note that controls need to have `control.isPointerBlocker = true` to correctly handle all the pointer events. This property is set by default on obvious controls like buttons for instance but if you want to have it on controls like images you must turn it on.**

All controls have the following observables:

| Observables              | Comments                                                                         |
| ------------------------ | -------------------------------------------------------------------------------- |
| onPointerMoveObservable  | Raised when the cursor moves over the control. Only available on fullscreen mode |
| onPointerEnterObservable | Raised when the cursor enters the control. Only available on fullscreen mode     |
| onPointerOutObservable   | Raised when the cursor leaves the control. Only available on fullscreen mode     |
| onPointerDownObservable  | Raised when pointer is down on the control.                                      |
| onPointerUpObservable    | Raised when pointer is up on the control.                                        |
| onPointerClickObservable | Raised when a control is clicked on.                                             |
| onClipboardObservable    | Raised when a clipboard event is triggered.                                      |

To use the clipboard events, they first need to be enabled by calling `registerClipboardEvents` on the AdvancedDynamicTexture instance, which registers the `cut`, `copy`, and `paste` events on the canvas. Once enabled, they can be triggered via `ctrl/cmd + c` for copy, `ctrl/cmd + v` for paste, and `ctrl/cmd + x` for cut, and they will always listen to the canvas. If you have any other actions that use the same key bindings, you can prevent the default triggering of these events by calling `unRegisterClipboardEvents`, which unregisters them from the canvas.

Here is an example on how to use clipboard observables:

- To create new meshes: [Playground: Clipboard Observable Mesh Creation Example](https://playground.babylonjs.com/#S0IW99#1) - Simple example of creating a mesh using the clipboard observable.
- To create new textblocks from clipboard data: [Playground: Clipboard Observable Textblock Example](https://playground.babylonjs.com/#AY28VL#4) - Simple example of creating new textblocks from clipboard data.

You can also define that a control is invisible to events (so you can click through it for instance). To do so, just call `control.isHitTestVisible`.

Please note that `onPointerMoveObservable`, `onPointerDownObservable`, `onPointerUpObservable`, `onPointerClickObservable` will receive a Vector2 parameter containing the pointer coordinates. If you want to get the pointer coordinates in local control space, you have to call `control.getLocalCoordinates(coordinates)`.

Here is an example of how to use observables: [Playground: Observables Example](https://playground.babylonjs.com/#XCPP9Y#121) - Simple example demonstrating observables.
Here is an example of how to use the onPointerClickObservable: [Playground: onPointerClickObservable Example](https://playground.babylonjs.com/#7RH606) - Simple example demonstrating the onPointerClickObservable.

### Alignments

You can define the alignments used by your control with the following properties:

| Property            | Default | Comments                              |
| ------------------- | ------- | ------------------------------------- |
| horizontalAlignment | 2       | Can be set to left, right or center.  |
| verticalAlignment   | 2       | Can be set to top, bottom and center. |

Values can be taken from `BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_*` and `BABYLON.GUI.Control.VERTICAL_ALIGNMENT_*`.

Here is an example of how to use alignments: [Playground: Alignments Example](https://playground.babylonjs.com/#XCPP9Y#13) - Simple example demonstrating how to use gui alignments.

### Position and size

You can set controls' position with the following properties:

| Property | Type         | Default | Default unit |
| -------- | ------------ | ------- | ------------ |
| left     | valueAndUnit | 0       | Pixel        |
| top      | valueAndUnit | 0       | Pixel        |

Size can be set with:

| Property | Type         | Default | Default unit |
| -------- | ------------ | ------- | ------------ |
| width    | valueAndUnit | 100%    | Percentage   |
| height   | valueAndUnit | 100%    | Percentage   |

Paddings can be set with:

| Property      | Type         | Default | Default unit |
| ------------- | ------------ | ------- | ------------ |
| paddingTop    | valueAndUnit | 0px     | Pixel        |
| paddingBottom | valueAndUnit | 0px     | Pixel        |
| paddingLeft   | valueAndUnit | 0px     | Pixel        |
| paddingRight  | valueAndUnit | 0px     | Pixel        |

Padding is the space around the control (on the outside) between it and its parent or sibling controls (like CSS margin when `box-sizing` is set to `border-box`). This means that `usableWidth = width - paddingLeft - paddingRight`. The same applies to `usableHeight = height - paddingTop - paddingBottom`.

All these properties can be defined using pixels or percentages as units.
To set a value in pixels, use this construct: `control.left = "50px"`.
To set a value as a percentage, use this construct: `control.left = "50%"`.

You can also omit the unit (in this case, the default unit will be used): `control.width = 0.5` (which is equivalent to `control.width = "50%"`).

Here is an example of how to use and update positions and sizes: [Playground: Positions and Sizes Example](https://playground.babylonjs.com/#KKA6L4) - Simple example demonstrating how to set and update GUI positions and sizes.

### Tracking positions

All controls can be moved to track the position of a mesh.
To do this, just call `control.linkWithMesh(mesh)`. You can then offset the position with `control.linkOffsetX` and `control.linkOffsetY`.

Here is an example of a trackable label: [Playground: Trackable Label Example](https://playground.babylonjs.com/#XCPP9Y#16) - Simple example of a trackable gui label.

Please note that controls that want to track position of a mesh must be at root level (at AdvancedDynamicTexture level).

You can also move a control to specific coordinates in your scene with `control.moveToVector3(position)`. Please note that the control will not stay with the vector if you change it afterward.

For Line control, you can also attach the second point to a control with `line.connectedControl = control`. In this case the `x2` and `y2` properties are used to offset the second point from the connected control.

With these 2 options, you can create a complete trackable label: [Playground: Complete Trackable Label Example](https://playground.babylonjs.com/#XCPP9Y#20) - Complete example of a trackable gui label.

**Tracking positions features only work when the AdvancedDynamicTexture is in fullscreen mode**

### Adaptive scaling

When using fullscreen UI, you can choose to define your UI with a fixed resolution.
To define this resolution, just set `myAdvancedDynamicTexture.idealWidth = 600` **or** `myAdvancedDynamicTexture.idealHeight = 400`.

If both are set, the idealWidth will be used.

If neither is set, you will be responsible for resizing the UI controls yourself.

When an ideal resolution is set, all values expressed **in pixels** are considered relative to this resolution and scaled accordingly to match the current resolution.

Even when an ideal size is set, the fullscreen UI is rendered at the same resolution as your canvas, but you can choose (mostly for performance reasons) to force the texture to use the ideal size for its resolution as well. To do so, call `myAdvancedDynamicTexture.renderAtIdealSize = true`.

In order to use both idealWidth and idealHeight, set both of them, and set `myAdvancedDynamicTexture.useSmallestIdeal = true`.
When the window width is smaller than the window height, `idealWidth` will be used; otherwise, `idealHeight` will be used.
This is a good solution for when your canvas can be resized to varying width : height ratios.

Here is an example of how to use horizontal adaptive scaling: [Playground: Horizontal Adaptive Scaling Example](https://playground.babylonjs.com/#XCPP9Y#39) - Simple example of horizontal adaptive scaling.

### Rotation and Scaling

Controls can be transformed with the following properties:

| Property         | Type   | Default | Comments                                                                |
| ---------------- | ------ | ------- | ----------------------------------------------------------------------- |
| rotation         | number | 0       | Value is in radians                                                     |
| scaleX           | number | 1       |
| scaleY           | number | 1       |
| transformCenterX | number | 0.5     | Define the center of transformation on X axis. Value is between 0 and 1 |
| transformCenterY | number | 0.5     | Define the center of transformation on Y axis. Value is between 0 and 1 |

**Please be aware that transformations are done at the rendering level, after all computations.** This means that alignment or positioning is done first without taking the transform into account.

Here is an example of how to use rotation and scaling: [Playground: Rotation and Scaling Example](https://playground.babylonjs.com/#XCPP9Y#22) - Simple example of rotation and scaling.

### Optimization

For complex controls (like the ColorPicker, for instance), you can turn on rendering cache by using `control.useBitmapCache = true`. This stores a cached version of the control image so it can be reused when the GUI is updated.

Starting with Babylon.js v4.0, the GUI system uses the Invalidate Rect optimization, which allows the renderer to update only portions of the texture. If you want to turn it off, you can call `adtTexture.useInvalidateRectOptimization = false`.

- [Playground: GUI Optimization Example](https://playground.babylonjs.com/#GBNTXK) - Simple example of GUI optimization.

## Controls

A control is an abstraction of a piece of UI. There are two kinds of controls:

- Pure controls: A pure control defines an action or an information useful for the user. It could be a TextBlock or a Button.
- Containers: Containers are used to organize your UI. They can contain other controls or containers.

All controls share the following properties:

| Property (Default)       | Type    | Comments                                                                                                                                                                                                                                                                        |
| ------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| alpha (1)                | number  | Between 0 and 1. 0 means completely transparent. 1 means fully opaque                                                                                                                                                                                                           |
| color (Black)            | string  | Foreground color                                                                                                                                                                                                                                                                |
| fontFamily (Arial)       | string  | Font family can be inherited. This means that if you set it on a container, it will be transmitted to all children of the container                                                                                                                                             |
| fontSize (18)            | number  | Can be inherited                                                                                                                                                                                                                                                                |
| fontStyle (unset)        | string  | Can be inherited                                                                                                                                                                                                                                                                |
| fontWeight (unset)       | string  | Can be inherited                                                                                                                                                                                                                                                                |
| zIndex (0)               | number  | the zIndex can be used to reorder controls on the z axis                                                                                                                                                                                                                        |
| shadowBlur (0)           | number  | the amount of blur that is applied to the drop shadow                                                                                                                                                                                                                           |
| shadowOffsetX (0)        | number  | the offset of the shadow on the x axis                                                                                                                                                                                                                                          |
| shadowOffsetY (0)        | number  | the offset of the shadow on the y axis                                                                                                                                                                                                                                          |
| shadowColor (#000)       | string  | the color of the shadow                                                                                                                                                                                                                                                         |
| isPointerBlocker (false) | boolean | make sure gui events are triggered before the scene events                                                                                                                                                                                                                      |
| hoverCursor ("")         | string  | the cursor to use when the mouse is over the control; `isPointerBlocker` needs to be set to `true` [Playground: HoverCursor Example](https://playground.babylonjs.com/#XCPP9Y#888) - Simple example using the hoverCursor control. |
| overlapGroup             | number  | The overlapGroup to which the control belongs, or `undefined` to exclude it from moving the control to a non-overlapping position.[Playground: OverlapGroup example](https://playground.babylonjs.com/#BMW0VQ#3) - Simple example using the overlapGroup property of the control.            |
| overlapDeltaMultiplier   | number  | The speed of the movement of the control when used with `AdvancedDynamicTexture`'s `moveToNonOverlappedPosition` method. See the playground above.                                                                                                                              |

Controls can be added directly to the AdvancedDynamicTexture or to a container with:

```javascript
container.addControl(control);
```

They can be removed with:

```javascript
container.removeControl(control);
```

You can also control the control visibility with `control.isVisible = false`. When `isVisible` is false, all children will also be invisible. If you just want to hide the current control but keep its children visible then you can use `control.notRenderable = true`.

### TextBlock

The TextBlock is a simple control used to display text: [Playground: Simple TextBlock Example](https://playground.babylonjs.com/#XCPP9Y#2) - Simple example of adding a textBlock to your scene.

Here are the properties you can define:

| Property                    | Type         | Comments                                                    |
| --------------------------- | ------------ | ----------------------------------------------------------- |
| lineSpacing (0px)           | valueAndUnit | Can be set to configure vertical spacing between text lines |
| text (null)                 | string       | Text to display                                             |
| textWrapping (false)        | boolean      | Can be set to true to enable text wrapping.                 |
| resizeToFit (false)         | boolean      | Can be set to true to enable resize to fit.                 |
| textHorizontalAlignment (2) | number       | Can be set to left, right or center                         |
| textVerticalAlignment (2)   | number       | Can be set to top, bottom or center                         |
| outlineWidth (0)            | number       | Text outline size, in pixels.                               |
| outlineColor ("white")      | string       | Text outline color.                                         |
| wordSplittingFunction       | string       | Function to use to split the text into words                |

The control currently provides 1 observable:

| Observables             | Comments                         |
| ----------------------- | -------------------------------- |
| onTextChangedObservable | Raised when the text has changed |

Please note that to get crisp texts you have to ensure that your rendering resolution is aligned with the screen: [Playground: Crisp Text Resolution Example](https://playground.babylonjs.com/#2ARI2W#10) - Simple example showing how to get crisp text by matching the screen resolution.

In this example, you can see on line #3 that we force the engine to use the same DPI as the screen.
Then on lines #10 and #11 we scale the GUI to align with the screen resolution.

When in wrapping mode, the text is split into words before being displayed, and words are supposed to be separated by at least one space character. In some languages, however, breaking a text into words should follow some other rules.

For those languages, you can use the `wordSplittingFunction` property to provide your own splitting function: this function takes a string as input and must return an array of strings (the input string broken into words).

Here's an example for Japanese: https://jsfiddle.net/3ph9m0cx/

### Line spacing

You can configure vertical line spacing between lines in pixels or percentage values.

**lineSpacing should be used with textWrapping set to true.**

You can try it here: [Playground: Simple Line Spacing Example](https://playground.babylonjs.com/#44KYLP) - Simple example of line spacing.

### Resize to Fit

When resizeToFit is set to true, the width and height of the rendered text will be automatically measured and applied to the TextBlock.

This property allows you to change the text and font of a TextBlock without having to worry about manually setting the estimated rendered width and height.

**Warning** When `resizeToFit` and `textWrapping` are both set to `true`, the width of the block will not be resized to fit the text; however, the height will be. This means that, depending on the font size, parts of the text on each line may be missing, and the width of the block may need to be changed manually.

## InputText

The InputText is a control used to let users insert text in a single line: [Playground: InputText Example](https://playground.babylonjs.com/#UWS0TS) - Simple example of InputText.

Here are the properties you can define:

| Property (default)           | Type         | Comments                                                                                                                 |
| ---------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------ |
| text (null)                  | string       | Text to display                                                                                                          |
| color (white)                | string       | Foreground color                                                                                                         |
| background (black)           | string       | Background color                                                                                                         |
| focusedBackground (black)    | string       | Background color to use when the control is focused                                                                      |
| autoStretchWidth (true)      | boolean      | The control will resize horizontally to adapt to text size                                                               |
| maxWidth (100%)              | valueAndUnit | The maximum width allowed if autoStretchWidth is set to true                                                             |
| margin (10px)                | valueAndUnit | Margin to use on left and right inside the control itself. This margin is used to determine where the text will be drawn |
| thickness (1)                | number       | Thickness of the border                                                                                                  |
| highligherOpacity (0.4)      | number       | Defines the transparency of highlighted text's background                                                                |
| textHighlightColor (#D5E0FF) | string       | Background color of highlighted text                                                                                     |
| onFocusSelectAll (false)     | boolean      | Allows complete selection of text by default when the input is focused.                                                  |

The InputText is a focusable control. This means you can click / touch it in order to give it the focus and control over the keyboard events. You can remove the focus from the control by hitting enter or clicking outside of the control.

The control provides several observables to track its state:

| Observables                        | Comments                                                |
| ---------------------------------- | ------------------------------------------------------- |
| onTextChangedObservable            | Raised when the text has changed                        |
| onBeforeKeyAddObservable           | Raised just before the entered key is added to the text |
| onFocusObservable                  | Raised when the control gets the focus                  |
| onBlurObservable                   | Raised when the control loses the focus                 |
| onTextHighlightObservable          | Raised when the text is highlighted                     |
| onTextCopyObservable               | Raised when the copy event is triggered                 |
| onTextCutObservable                | Raised when the cut event is triggered                  |
| onTextPasteObservable              | Raised when the paste event is triggered                |
| onKeyboardEventProcessedObservable | Raised when a key event was processed                   |

Please note that the InputText has fairly limited editing support. Here are the supported keys:

- Delete
- Backspace
- Home
- End
- Enter
- Left / Right (used to move the cursor)

Furthermore, please note that due to JavaScript platform limitation, the InputText cannot invoke the onscreen keyboard. On mobile, the InputText will use the `prompt()` command to get user input. You can define the title of the prompt by setting `control.promptMessage`.

### Using onBeforeKeyAddObservable for extended keyboard layouts and input masks

The onBeforeKeyAddObservable observable can be used to extend or change how the InputText control accepts text. For example, it's possible to implement support for different keyboard layouts using this feature where some keys act as modifiers for the next entered key or you can implement an input mask which only accepts numerical keys.

The observable is triggered just before a printable key will be added to the text in the control. The attached handler can then use the following methods to get information on the keyboard state and to modify how the key is handled within the control:

| Method     | Description                                                                            |
| ---------- | -------------------------------------------------------------------------------------- |
| currentKey | The key that will be appended to the text                                              |
| addKey     | If true, the key in currentKey will be added to the text, otherwise it will be skipped |
| deadKey    | Set to true if the user hit the dead key on the keyboard. Handler must reset to false  |

For example, if the handler wants to limit the control to only accept numerical keys, it can set `addKey` to `false` if the value of `currentKey` is not numerical. The key will then not be added to the text. Similarly, dead key support can be implemented by checking the `deadKey` flag and setting `currentKey` to the appropriate character for the dead key + key combination.

Please note that the observable is only triggered by printable keys, that is, keys that can be added to the text, and not by control keys like backspace and enter.

Here's an example showing two inputs, one which only accepts numerical keys and one which has simple dead key support: [Playground: Restricted Input Example](https://playground.babylonjs.com/#I1Y5YT#1) - Simple example of restricting input for specific character types.

InputText also supports clipboardObservables, here's an example: [Playground: InputText With ClipboardObservable](https://playground.babylonjs.com/#UWS0TS#20) - Simple example showing InputText with a clipboardObservable.

## InputTextArea

The InputTextArea is a control used to display editable text across multiple lines:

```javascript
const inputTextArea = new BABYLON.GUI.InputTextArea("input", "Some initial text");
```

You can try it here: [Playground: InputTextArea Example](https://playground.babylonjs.com/#NVAEWD#7) - Simple example of InputTextArea.

InputTextArea behaves the same as the InputText control but it also has these additional properties:

| Property (default)       | Type         | Comments                                                      |
| ------------------------ | ------------ | ------------------------------------------------------------- |
| autoStretchHeight (true) | boolean      | The control will resize vertically to adapt to text size      |
| maxHeight (100%)         | valueAndUnit | The maximum height allowed if autoStretchWidth is set to true |

Here's an example of an InputTextArea using the additional properties: [Playground: InputTextArea AutoStretchHeight Example](https://playground.babylonjs.com/#NVAEWD#8) - Example of InputTextArea with autoStretchHeight enabled

## InputPassword

The InputPassword is a control that shows the entered characters as bullets and is thus suited for entering passwords: [Playground: InputPassword Example](https://playground.babylonjs.com/#UB58DY) - Simple example showing how to add an InputPassword control to your scene.

Otherwise it behaves the same as the InputText control and has the same properties as shown above.

There are no configuration options available that are specific to this control. For example, it is not possible to show the entered plain text.

## Button

A button can be used to interact with the user.
Please see the events chapter to see how to connect your events with your buttons.
The `onPointerClickObservable` is raised when a button is clicked, meaning both the down and up events happen while the cursor is over the control.

There are three kinds of buttons available out of the box:

- ImageButton: An image button is a button made with an image and text. You can create one with:

```javascript
const button = BABYLON.GUI.Button.CreateImageButton("but", "Click Me", "textures/grass.png");
```

You can try it here: [Playground: Button Example](https://playground.babylonjs.com/#XCPP9Y#3) - Simple example showing how to add a Button control to your scene.

- ImageWithCenterTextButton: An image button made with an image background and a centered text overlay.

```javascript
const button = BABYLON.GUI.Button.CreateImageWithCenterTextButton("but", "Click Me", "textures/grass.png");
```

You can try it here: [Playground: ImageWithCenterTextButton Example](https://playground.babylonjs.com/#PLTRBV) - Simple example showing how to add an ImageWithCenterTextButton control to your scene.

- SimpleButton: A simple button with text only.

```javascript
const button = BABYLON.GUI.Button.CreateSimpleButton("but", "Click Me");
```

You can try it here: [Playground: SimpleButton Example](https://playground.babylonjs.com/#XCPP9Y#4) - Simple example showing how to add a SimpleButton control to your scene.

- ImageOnlyButton:

```javascript
const button = BABYLON.GUI.Button.CreateImageOnlyButton("but", "textures/grass.png");
```

You can try it here: [Playground: ImageOnlyButton Example](https://playground.babylonjs.com/#XCPP9Y#28) - Simple example showing how to add an ImageOnlyButton control to your scene.

Please also note that by default, buttons handle hit testing based on their bounding info. If you want embedded controls to handle the picking, you can call `button.delegatePickingToChildren = true`.

### Accessing parts

You can use the following properties to get a button's parts (if available):

- image: Returns the image part of the button (if any)
- textBlock: Returns text related properties of the button

### Visual animations

By default, a button changes its opacity on pointerOver and changes its scale when clicked.
You can define your own animations with the following callbacks:

- pointerEnterAnimation
- pointerOutAnimation
- pointerDownAnimation
- pointerUpAnimation

### Custom button

You can also create a complete custom button by manually adding children to the button. Here is how the ImageButton is built:

```javascript
BABYLON.GUI.Button.CreateMyCustomButton = function (name, text, imageUrl) {
  const result = new BABYLON.GUI.Button(name);

  // Adding text
  const textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
  textBlock.textWrapping = true;
  textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  textBlock.paddingLeft = "20%";
  result.addControl(textBlock);

  // Adding image
  const iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
  iconImage.width = "20%";
  iconImage.stretch = BABYLON.GUI.Image.STRETCH_UNIFORM;
  iconImage.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  result.addControl(iconImage);

  return result;
};
```

## Checkbox

The checkbox is used to control a boolean value.
You can specify the value with `checkbox.isChecked`.

Changing the `isChecked` property will raise an observable called `checkbox.onIsCheckedChangedObservable`.

The control is rendered using the following properties:

| Property       | Type   | Default | Comments                                                                                                                                                 |
| -------------- | ------ | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| color          | string | white   | Foreground color                                                                                                                                         |
| background     | string | black   | Background color                                                                                                                                         |
| checkSizeRatio | number | 0.8     | Define the ratio used to compute the size of the inner checkbox (0.8 by default, which means the inner check size is equal to 80% of the control itself) |

Here is an example of a checkbox: [Playground: Checkbox Example](https://playground.babylonjs.com/#U9AC0N#2) - Simple example showing how to add a Checkbox control to your scene.

## RadioButton

The radio button is used to define a value among a list by using a group of radio buttons where only one can be true.
You can specify the selected value with `radiobutton.isChecked`.

Changing the `isChecked` property will raise an observable called `checkbox.onIsCheckedChangedObservable`. Furthermore, if you select a radio button, all other radio buttons within the same group will turn to false.

The control is rendered using the following properties:

| Property       | Type   | Default      | Comments                                                                                                                                                 |
| -------------- | ------ | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| color          | string | white        | Foreground color                                                                                                                                         |
| background     | string | black        | Background color                                                                                                                                         |
| checkSizeRatio | number | 0.8          | Define the ratio used to compute the size of the inner checkbox (0.8 by default, which means the inner check size is equal to 80% of the control itself) |
| group          | string | empty string | Use the group property to gather radio buttons working on the same value set                                                                             |

Here is an example of a radiobutton: [Playground: RadioButton Example](https://playground.babylonjs.com/#U9AC0N#13) - Simple example showing how to add a RadioButton control to your scene.

## Slider

The slider is used to control a value within a range.
You can specify the range with `slider.minimum` and `slider.maximum`.

The value itself is specified with `slider.value` and raises an observable every time it is changed (`slider.onValueChangedObservable`).

The control is rendered using the following properties:

| Property       | Type         | Default | Comments                                                                                                                  |
| -------------- | ------------ | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| borderColor    | string       | white   | Color used to render the border of the thumb                                                                              |
| color          | string       | white   | Foreground color                                                                                                          |
| background     | string       | black   | Background color                                                                                                          |
| barOffset      | valueAndUnit | 5px     | Offset used vertically to draw the background bar                                                                         |
| thumbWidth     | valueAndUnit | 30px    | Width of the thumb                                                                                                        |
| displayThumb   | boolean      | true    | Indicates if the thumb must be rendered (useful to simulate progress bar)                                                 |
| isThumbCircle  | boolean      | false   | Indicates if the thumb should be a circle (square if false)                                                               |
| isThumbClamped | boolean      | false   | Indicates if the thumb should be clamped                                                                                  |
| isVertical     | boolean      | false   | Indicates that the slider will be rendered vertically instead of horizontally                                             |
| step           | number       | 0       | Indicates the degree of precision required for slider values (0 means full precision, whereas 0.01 means 2 digits of precision) |

When using a vertical slider, you have to make sure that the height is greater than the width. The opposite has to be true when using `isVertical = false`.

Here is an example of a slider: [Playground: Slider Example](https://playground.babylonjs.com/#U9AC0N#1) - Simple example showing how to add a Slider control to your scene.

## ImageBasedSlider

You can use an ImageBasedSlider to customize a slider using pictures. This control can be configured like the Slider.

It is rendered using the following properties:

| Property        | Type         | Default | Comments                                                                      |
| --------------- | ------------ | ------- | ----------------------------------------------------------------------------- |
| backgroundImage | string       | null    | Path to the image to use for the background                                   |
| valueBarImage   | string       | null    | Path to the image to use for the value bar                                    |
| thumbImage      | string       | null    | Path to the image to use for the thumb                                        |
| barOffset       | valueAndUnit | 5px     | Offset used vertically to draw the background bar                             |
| thumbWidth      | valueAndUnit | 30px    | Width of the thumb                                                            |
| displayThumb    | boolean      | true    | Indicates if the thumb must be rendered (useful to simulate progress bar)     |
| isThumbClamped  | boolean      | false   | Indicates if the thumb should be clamped                                      |
| isVertical      | boolean      | false   | Indicates that the slider will be rendered vertically instead of horizontally |

Here is an example of sliders and image-based sliders: [Playground: ImageBasedSlider Example](https://playground.babylonjs.com/#HATGQZ) - Simple example showing how to add a ImageBasedSlider control to your scene.

## Line

The line will draw a line (!!) between two points.

Here are the properties you can define:

| Property  | Type             | Default     | Comments                         |
| --------- | ---------------- | ----------- | -------------------------------- |
| x1        | number           | 0           | X coordinate of the first point  |
| y1        | number           | 0           | Y coordinate of the first point  |
| x2        | number           | 0           | X coordinate of the second point |
| y2        | number           | 0           | Y coordinate of the second point |
| dash      | array of numbers | Empty array | Defines the size of the dashes   |
| lineWidth | number           | 1           | Width in pixel                   |

Here is an example of a line: [Playground: Line Example](https://playground.babylonjs.com/#XCPP9Y#6) - Simple example showing how to add a Line control to your scene.

## MultiLine

MultiLine will draw lines between any number of meshes, controls and points.

Each item in MultiLine is called MultiLinePoint, and has the following properties:

| Property | Type         | Default | Comments                                  |
| -------- | ------------ | ------- | ----------------------------------------- |
| mesh     | AbstractMesh | null    | Tracks a Mesh                             |
| control  | Control      | null    | Tracks a Control                          |
| x        | number       | null    | x of a point, can be specified in px or % |
| y        | number       | null    | y of a point, can be specified in px or % |

Here are the functions you can use:

- add(): Receives any number of arguments and adds them, each argument can be a mesh, a control, or a point. Returns an array of MultiLinePoint
- push(): Receives 1 argument and adds it, each argument can be a mesh, a control, or a point. Returns a MultiLinePoint
- remove(): Receives an index or an instance of a MultiLinePoint and removes it
- getAt(): Receives an index of a MultiLinePoint and returns its instance. If no MultiLinePoint exists at that index, a new one is created

Here are the properties you can define in MultiLine:

| Property  | Type             | Default     | Comments                       |
| --------- | ---------------- | ----------- | ------------------------------ |
| dash      | array of numbers | Empty array | Defines the size of the dashes |
| lineWidth | number           | 1           | Width in pixel                 |

Here is an example of a MultiLine combining meshes, a control and a point: [Playground: MultiLine Example](https://playground.babylonjs.com/#H03KNW#2) - Simple example showing how to add a MultiLine control to your scene.

## Image

Use the image control to display an image in your UI.

### Stretch Modes and Scaling

You can control the stretch used by the image with `image.stretch` property. You can set it to one of these values:

- BABYLON.GUI.Image.STRETCH_NONE: Use original size
- BABYLON.GUI.Image.STRETCH_FILL: Scale the image to fill the container (This is the default value)
- BABYLON.GUI.Image.STRETCH_UNIFORM: Scale the image to fill the container but maintain aspect ratio
- BABYLON.GUI.Image.STRETCH_EXTEND: Scale the container to adapt to the image size.
- BABYLON.GUI.Image.STRETCH_NINE_PATCH: Scale the image using a [nine patch technique](http://wiresareobsolete.com/2010/06/9-patches/). You have to either define the `sliceLeft`, `sliceRight`, `sliceTop` and `sliceBottom` properties or store data into your image (in the first and last rows and columns) and call `image.populateNinePatchSlicesFromImage = true` to read that data. Demo: [Playground: Stretch_Nine_Patch Example](https://playground.babylonjs.com/#G5H9IN#2) - Simple example showing how to add an Image control with Stretch_Nine_Patch to your scene.

You may want to have the Image control adapt its size to the source image. To do so just call `image.autoScale = true`.

### Image Source

You can change image source at any time with `image.source="myimage.jpg"`.

You can also define which part of the source image you want to use with the following properties:

- sourceLeft: x coordinate in
  the source image (in pixel)
- sourceTop: y coordinate in the source image (in pixel)
- sourceWidth: width of the source image you want to use (in pixel)
- sourceHeight: height of the source image you want to use (in pixel)

Here is an example of an image: [Playground: Image Example](https://playground.babylonjs.com/#XCPP9Y#7) - Simple example showing how to add an Image control to your scene.

You can use an animation sheet in the image by using the properties `image.cellId`, `image.cellWidth`, and `image.cellHeight`. [Playground: Image With Sprite Sheet Example](https://playground.babylonjs.com/#K60448#10) - Simple example showing how to add an Image with a sprite sheet to your scene.

You can also apply stretch to animation sheet using `image.stretch` property.

- [Playground: Image With Stretched Sprite Sheet Example 1](https://playground.babylonjs.com/#K60448#1) - Simple example showing how to add an Image with a stretched sprite sheet to your scene.

- [Playground: Image With Stretched Sprite Sheet Example 2](https://playground.babylonjs.com/#K60448#2) - Simple example showing how to add an Image with a stretched sprite sheet to your scene.

### Pointer events

If you want the non-visible parts of a transparent image to not trigger pointer events, you can set `img.detectPointerOnOpaqueOnly = true` to indicate that pointer events should only be fired on pixels with alpha > 0.

- [Playground: Detect pointer on opaque only](https://playground.babylonjs.com/#E3F1B8) - Using detectPointerOnOpaqueOnly to only fire pointer events in the visible parts of an Image

### Batch loading of SVG images from icon sheet

You can now load multiple SVG icons from a single SVG icon sheet without manually defining multiple sourceLeft, sourceTop, sourceWidth, sourceHeight attributes for each image.

Pre-requisite: a valid single layer SVG document with width, height, viewbox defined and icons grouped via ids. The layer should not have any transform attributes.

`onSVGAttributesComputedObservable` will trigger when the sourceLeft, sourceTop, sourceWidth, sourceHeight attributes are automatically computed. You can create custom SVG buttons that are built from multiple SVG assets (glow, text, images etc) for even cleaner code.

Here is an example that uses SVG assets for images and buttons: [Playground: SVG Asset GUI Example](https://playground.babylonjs.com/#E5CARD) - Simple example using SVG assets for gui elements.

Known issue: The batch loading process requires the entire SVG icon sheet to be loaded as an HTMLObjectElement in the DOM. In certain browsers, you may notice a quick flash of the icon sheet on the canvas as the assets load. To alleviate this, you may use [a loading screen](https://doc.babylonjs.com/features/featuresDeepDive/scene/customLoadingScreen).

## ColorPicker

The color picker control allows users to set colors in your scene.

Whenever a user interacts with the color picker an observable is triggered (`colorPicker.onValueChangedObservable`) which returns the current value (Color3) of the color picker.

The control is rendered using the following properties:

| Property | Type             | Default | Comments                                                                                                        |
| -------- | ---------------- | ------- | --------------------------------------------------------------------------------------------------------------- |
| size     | string or number | "200px" | The size, width, and height property will always be the same value since the color picker can only be a square. |

Here is an example of a color picker: [Playground: ColorPicker Example](https://playground.babylonjs.com/#91I2RE#1) - Simple example showing how to add a ColorPicker control to your scene.

## DisplayGrid

The display grid control is a simple control used to display grids inside your GUI.

The control is rendered using the following properties:

| Property           | Type   | Default    | Comments                                 |
| ------------------ | ------ | ---------- | ---------------------------------------- |
| background         | string | "Black"    | Defines the color of the grid background |
| cellWidth          | number | 20         | Defines the width of each cell           |
| cellHeight         | number | 20         | Defines the height of each cell          |
| minorLineTickness  | number | 1          | Defines the tickness of minor lines      |
| minorLineColor     | string | "DarkGray" | Defines the color of the minor lines     |
| majorLineTickness  | number | 2          | Defines the tickness of major lines      |
| majorLineColor     | string | "White"    | Defines the color of the major lines     |
| majorLineFrequency | number | 5          | Defines the frequency of major lines     |

Here is an example of a display grid: [Playground: DisplayGrid Example](https://playground.babylonjs.com/#747U9T) - Simple example showing how to add a DisplayGrid control to your scene.

## VirtualKeyboard

The VirtualKeyboard is a control used to display a simple onscreen keyboard. This is mostly useful for WebVR scenarios where the user cannot easily use a keyboard.

### Keys

You can define the keys provided by the keyboard with the following code:

```javascript
const keyboard = new BABYLON.GUI.VirtualKeyboard();
keyboard.addKeysRow(["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "\u2190"]);
```

Every key will be created using default values specified by the following properties:

| Property                   | Default |
| -------------------------- | ------- |
| defaultButtonWidth         | 40px    |
| defaultButtonHeight        | 40px    |
| defaultButtonPaddingLeft   | 2px     |
| defaultButtonPaddingRight  | 2px     |
| defaultButtonPaddingTop    | 2px     |
| defaultButtonPaddingBottom | 2px     |
| defaultButtonColor         | #DDD    |
| defaultButtonBackground    | #070707 |

You can also override each property by providing an array containing properties for keys (or null):

```javascript
addKeysRow(["a", "b"], [null, { width: "200px" }]);
```

You can define each default property set based on the following class:

```javascript
class KeyPropertySet {
      width?: string;
      height?: string;
      paddingLeft?: string;
      paddingRight?: string;
      paddingTop?: string;
      paddingBottom?: string;
      color?: string;
      background?: string;
  }
```

### Layouts

The VirtualKeyboard provides a static method to create a default layout:

```javascript
const keyboard = BABYLON.GUI.VirtualKeyboard.CreateDefaultLayout();
```

The default layout is equivalent to:

```javascript
addKeysRow(["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "\u2190"]);
addKeysRow(["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]);
addKeysRow(["a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "\u21B5"]);
addKeysRow(["\u21E7", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/"]);
addKeysRow([" "], [{ width: "200px" }]);
```

### VirtualKeyboard Events

Every time a key is pressed, the `onKeyPressObservable` observable is triggered. But you can also rely on `keyboard.connect(inputText)` to automatically connect a VirtualKeyboard to an InputText. In this case, the keyboard will appear only when the InputText is focused, and all key-press events will be sent to the InputText.

You can find a complete demo here: [Playground: VirtualKeyboard Events Example](https://playground.babylonjs.com/#S7L7FE) - Simple example showing how to add VirtualKeyboard Events to your scene.

## Containers

The containers are controls used to host other controls. Use them to organize your UI.
Containers have one specific property: `container.background`. Use it to define the background color of your container.

By default containers do not block pointer events (ie. the underlying scene will receive the pointer event even if the pointer is over a container). You can prevent this behavior by calling `container.isPointerBlocker = true`.

Containers are responsible for managing their children's layout. To prevent layout cycles, the system will not let the layout being updated during a cycle more than 3 times. This value can be changed with `container.maxLayoutCycle`. You can also turn on console warnings when layout cycles are detected with `container.logLayoutCycleErrors = true`.

### Adaptive size

You can have your containers adapt their size to their children by using one of these properties:

- adaptWidthToChildren (false by default)
- adaptHeightToChildren (false by default)

If you set one of these properties to `true`, the associated dimension (width, height, or both) is computed based on the direct children's size as long as it is defined in pixels (size cannot be defined as a percentage because this would generate an infinite loop: the child would need the parent size, and the parent would need the child size).
You can find a demo here: [Playground: Adaptative size Example](https://playground.babylonjs.com/#GL5SIM) - Simple example showing how to use adaptative sizing in your scene.

### Make your controls non-overlapping

If you link your control with a mesh, the control will follow the mesh and its position will be changed automatically relative to the position of the linked mesh. Sometimes your controls will overlap each other.

To solve this problem, you can use the `moveToNonOverlappedPosition` method of the `AdvancedDynamicTexture` class.
First, you need to set a value for the `overlapGroup` property on your control. If you leave it `undefined`, the control will not be touched during the execution of the `moveToNonOverlappedPosition` method. Second, you need to call the `moveToNonOverlappedPosition` method of the `AdvancedDynamicTexture` class manually each frame, for example in the render loop or by using an observer. The `onBeginRenderObservable` of the `AdvancedDynamicTexture` is a good place for this.

Please refer to this playground with detailed comments in the code on how to use this functionality: [Playground: Non-overlapping controls Example](https://playground.babylonjs.com/#BMW0VQ#3) - Simple example showing how to make your mesh linked controls non-overlapping.

Forum: https://forum.babylonjs.com/t/non-overlapping-gui-linked-controls/24610

### Clipping

By default containers will clip their children to their bounds. If you want to clip a control's contents and its children, you should set

```javascript
container.clipChildren = false;
container.clipContent = true;
```

Please note that not clipping children may generate issues with `adt.useInvalidateRectOptimization` so it is recommended to turn this optimization off if you want to use unclipped children.

You can find a demo here: [Playground: Clipping Example](https://playground.babylonjs.com/#LBF8S2#38) - Simple example showing how to use clipping in your scene.

### Rectangle

The Rectangle is a rectangular container with the following properties:

| Property     | Type   | Default | Comments                                                        |
| ------------ | ------ | ------- | --------------------------------------------------------------- |
| thickness    | number | 1       | Thickness of the border                                         |
| cornerRadius | number | 0       | Size in pixel of each corner. Used to create rounded rectangles |

Here is an example of a rectangle control: [Playground: Rectangle Example](https://playground.babylonjs.com/#XCPP9Y#8) - Simple example showing how to add a Rectangle control to your scene.

### Ellipse

The Ellipse is an ellipsoidal container with the following properties:

| Property  | Type   | Default | Comments                                                            |
| --------- | ------ | ------- | ------------------------------------------------------------------- |
| thickness | number | 1       | Thickness of the border                                             |
| arc       | number | 1       | Used to create arcs. Ranges between 0 and 1 (CW), or 0 and -1 (CCW) |

Here is an example of an ellipse control: [Playground: Ellipse Example](https://playground.babylonjs.com/#XCPP9Y#10) - Simple example showing how to add an Ellipse control to your scene.

### StackPanel

The StackPanel is a control which stacks its children based on its orientation (can be horizontal or vertical).
All children must have a defined width or height (depending on the orientation) in **pixels** (A warning will be written to the console if this is not true. This warning can be turned off with `panel.ignoreLayoutWarnings = true`).

If the panel is vertical, its height will depend on the children, while its width will be 100% of the parent, unless specified. If the panel is horizontal, its width will depend on the children, while its height will be 100% of the parent, unless specified.

| Property   | Type    | Default | Comments                 |
| ---------- | ------- | ------- | ------------------------ |
| isVertical | boolean | true    | Orientation of the panel |

Here is an example of a StackPanel: [Playground: StackPanel Example](https://playground.babylonjs.com/#XCPP9Y#11) - Simple example showing how to add a StackPanel control to your scene.

### ScrollViewer

Due to its rich feature set, the ScrollViewer has its own dedicated page right [here](https://doc.babylonjs.com/features/featuresDeepDive/gui/scrollViewer).

### Grid

The Grid is a control which defines a set of rows and columns and allows children to specify which cell they want to belong to:

```javascript
const grid = new BABYLON.GUI.Grid();
grid.addColumnDefinition(100, true);
grid.addColumnDefinition(0.5);
grid.addColumnDefinition(0.5);
grid.addColumnDefinition(100, true);
grid.addRowDefinition(0.5);
grid.addRowDefinition(0.5);

// This rect will be on first row and second column
const rect = new BABYLON.GUI.Rectangle();
rect.background = "green";
rect.thickness = 0;
grid.addControl(rect, 0, 1);

// This rect will be on second row and third column
rect = new BABYLON.GUI.Rectangle();
rect.background = "red";
rect.thickness = 0;
grid.addControl(rect, 1, 2);
```

You can define rows and columns with the following functions:

- addColumnDefinition(width, isPixel): Use this function to create a new column. Width can be between 0 and 1 if isPixel is false (percentage mode then) or contains an actual width if isPixel is true
- addRowDefinition(height, isPixel): Use this function to create a new row. Height can be between 0 and 1 if isPixel is false (percentage mode then) or contains an actual width if isPixel is true

Here is an example of a grid made of 4 columns where the first and the last will have a width of 50px and the second and third will each have 50% of the remaining space:

```javascript
grid.addColumnDefinition(100, true);
grid.addColumnDefinition(0.5);
grid.addColumnDefinition(0.5);
grid.addColumnDefinition(100, true);
```

You can update or delete columns and rows with the following functions:

- setRowDefinition(index, height, isPixel): Update a row definition
- setColumnDefinition(index, width, isPixel): Update a column definition
- removeRowDefinition(index): Remove a row definition at specified index
- removeColumnDefinition(index): Remove a column definition at specified index

Two properties can also help you get the row and column counts:

- rowCount: Will give you the number of rows
- columnCount: Will give you the number of columns

To add a control in a grid, you have to specify the row and column indexes:

```javascript
grid.addControl(control, 1, 2); // 2nd row, thrid column
```

You can get the list of controls in a specific cell by calling:

```javascript
const controls = grid.getChildrenAt(2, 3);
```

Here is an example of a Grid: [Playground: Grid Example](https://playground.babylonjs.com/#KX33X8#1) - Simple example showing how to add a Grid control to your scene.

## Styles

Starting with Babylon.js v3.3, you can create a style object that will be used to share configuration across controls. To do so, you can use this code:

```javascript
const style = advancedTexture.createStyle();
style.fontSize = 24;
style.fontStyle = "italic";
style.fontFamily = "Verdana";
```

Then apply the style to a control:

```javascript
textControl.style = style;
```

Here is the list of properties supported by styles so far:

- fontSize
- fontStyle
- fontFamily
- fontWeight

Please note that if a control has a style, then the style values are used instead of values directly defined on the control itself.

You can find a demo here: [Playground: Styles Example](https://playground.babylonjs.com/#5N4JIS) - Simple example showing how to add a Styles control to your scene.

## Helpers

To reduce the amount of code required to achieve frequent tasks you can use the following helpers:

- `BABYLON.GUI.Control.AddHeader(control, text, size, options { isHorizontal, controlFirst })`: This function creates a StackPanel (horizontal or vertical based on options) and adds your control plus a TextBlock to it. Options can also be used to specify whether the control is inserted first or after the header. Depending on the orientation, `size` specifies either the width or the height used for the TextBlock.

- `BABYLON.GUI.Checkbox.AddCheckBoxWithHeader(title, onValueChanged)`: This function will create a horizontal StackPanel and will add a checkbox alongside a text block displaying the `title` property. `onValueChanged` defines the callback to call when checkbox state changes.

- `BABYLON.GUI.RadioButton.AddRadioButtonWithHeader(title, group, isChecked, onValueChanged)`: This function will create a horizontal StackPanel and will add a radio button (set with specified group and isChecked parameters) alongside a text block displaying the `title` property. `onValueChanged` defines the callback to call when radio button state changes.

## Cloning

You can clone individual controls by using the `clone` method. Its argument is an optional AdvancedDynamicTexture that will host the cloned control. This is so you can clone the control to a different ADT. After cloning the control, you still have to add it to the ADT, like this:

```javascript
const cloned = control.clone();
adt.addControl(cloned);
```

An example can be seen here: [Playground: Cloning a control](https://playground.babylonjs.com/#1W1JS5) - Playground showing how to clone a control

You can also clone the entire ADT by using the same method: [Playground: Cloning the entire ADT](https://playground.babylonjs.com/#XCPP9Y#18083) - Playground showing how to clone an ADT

## GUI and postprocesses

### LayerMask

In order to not apply postprocesses to your GUI, you will have to use a multi-cameras approach: one for your main scene and one for your GUI.

You can find an implementation example here: [Playground: LayerMask Example](https://playground.babylonjs.com/#U9AC0N#58) - Simple example showing how to add a LayerMask control to your scene.

The key point is to use the camera.layerMask property to isolate your GUI:

```javascript
const camera2 = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
camera2.layerMask = 2;

// GUI - simply set advancedTexture layerMask to 2
const advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
advancedTexture.layer.layerMask = 2;
```

Then all meshes of your main scene will have a different layerMask attached to main camera:

```javascript
const camera1 = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
camera1.layerMask = 1;

myMesh.layerMask = 1;
```

### Multi-scenes

The other option is to use a multi-scene approach with a render loop defined like this:

```javascript
guiScene.autoClear = false;
engine.runRenderLoop(function () {
  mainScene.render();
  guiScene.render();
});
```

In this case the `guiScene` will host your GUI and the `mainScene` will host your scene with your postprocesses.

## GUI and HighDPI Displays

If you are viewing the scene on a high-DPI (or "retina") device (such as many mobile devices or some laptops), you may notice that text on the UI appears "blurry" or "pixelated". This is because, starting in Babylon.js v2.6, the engine no longer defaults to adapting to the device pixel ratio. This was done for performance reasons on mobile devices; turning it on can have a large impact on performance. To improve text rendering (at the cost of performance), you need to enable the `adaptToDeviceRatio` option when constructing your engine.

Please see [Turning AdaptToDeviceRatio Off/On](https://doc.babylonjs.com/features/featuresDeepDive/scene/optimize_your_scene#turning-adapttodeviceratio-offon) for more information on the trade-offs.

## GUI Examples
This [example showing contributors to the Babylon repo](https://babylonjs.tricotools.com/) - created by [Tricotou](https://forum.babylonjs.com/u/tricotou/summary) and posted to our [forum](https://forum.babylonjs.com/t/babylonjs-repository-viewer/50974) - is a great example of dynamically creating Babylon GUI from a data set.

The following playground shows Tricotou's process of creating a complex GUI comprised of buttons, images, and text in a scroll viewer generated from a JSON string.
- [Playground: Creating GUI from JSON Data](https://playground.babylonjs.com/#4U4QH9#35)

## Further reading

- [How To Use the Selection Panel Helper](https://doc.babylonjs.com/features/featuresDeepDive/gui/selector)
- [How To Use Babylon GUI Scroll Viewer](https://doc.babylonjs.com/features/featuresDeepDive/gui/scrollViewer)
- [How To Use Babylon GUI Xml Loader](https://doc.babylonjs.com/features/featuresDeepDive/gui/xmlLoader)
- [How To Use Babylon GUI3D](https://doc.babylonjs.com/features/featuresDeepDive/gui/gui3D)
