# Udim2

## Overview

UDim2 is a data type that handles values in both horizontal (X) and vertical (Y) directions. For each direction, it includes both the scale value proportional to the entire size and fixed offset correction value in pixel. It is mostly used when flexibly specifying the size or position of UI elements on the screen.

## Constructors

### new

Creates a single UDim2 object based on the passed scale value for X axis and Y axis, and returns it. The created UDim2 can be used to manually specify the position or size of UI.

#### Parameters

| `number` xScale  | The scale value applied along the X axis relative to the size of parent. |
| ---------------- | ------------------------------------------------------------------------ |
| `number` xOffset | The absolute value applied along the X axis in pixel.                    |
| `number` yScale  | The scale value applied along the Y axis relative to the size of parent. |
| `number` yOffset | The absolute value applied along the Y axis in pixel.                    |

#### Return

| `UDim2` | The UDim2 consisted of the specified scale and offset values. |
| ------- | ------------------------------------------------------------- |

#### Code Samples

```lua
local Position = UDim2.new(0.5, 10, 0.5, 20)

TextLabel.Position = Position
```

## Properties

### X

It represents both the relative scale value and offset value in pixel applied along the X axis.

#### Code Samples

```lua
local Position = UDim2.new(0.5, 10, 0.5, 20)

print(Position.X.Scale, Position.X.Offset)
```

### Y

It represents both the relative scale value and offset value in pixel applied along the Y axis.

#### Code Samples

```lua
local Position = UDim2.new(0.5, 10, 0.5, 20)

print(Position.Y.Scale, Position.Y.Offset)
```

## Methods

### Lerp

Returns a new Dim2 calculated by linearly interpolating between the current Dim2 and the target Dim2 according to the alpha ratio.

#### Parameters

| `UDim2` goal   | The target UDim2 to be interpolated.    |
| -------------- | --------------------------------------- |
| `number` alpha | A value indicating interpolation ratio. |

#### Return

| `UDim2` | The created UDim2. |
| ------- | ------------------ |

#### Code Samples

```lua
local a = UDim2.new(0.0, 0, 0.1, 0)
local b = UDim2.new(0.1, 100, 0.1, 100)
print(a:Lerp(b, 0.3)) --> Output: {0.03, 30}, {0.1, 30}
```

## See also

{% content-ref url="../../../manual/studio-manual/get-started/coordinate-system" %}
[coordinate-system](https://docs.overdare.com/manual/studio-manual/get-started/coordinate-system)
{% endcontent-ref %}
