# Rect

`com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect`

**Since:** 1.0.5

A filled rectangle defined by two corner positions. Corners may be given in either
order; width and height are their absolute coordinate differences. Colors use
packed ARGB, and a zero-alpha color with nonzero RGB is treated as opaque.  
```js
const draw = Hud.createDraw2D();  
// Corners may be specified in either order.  
const rect = draw.addRect(110, 60, 10, 10, Color.argb(0x80, 0, 0, 0));  
draw.register();  
Chat.log(`${rect.getWidth()} by ${rect.getHeight()}, from ${rect.getX1()}, ${rect.getY1()}`);  
```

Available in scripts as `RenderElements.Rect`.

## Nested types

- [Builder](Rect.Builder.md)

## Constructors

### new Rect {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_constructor-int-int-int-int-int-float-int-}

```js
new Rect(int x1, int y1, int x2, int y2, int color, float rotation, int zIndex)
```

Constructs a rectangle from two corners, applying [setColor(int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setColor-int-)'s alpha
fix-up and normalizing the rotation with [Mth.wrapDegrees(float)](https://mcsrc.dev/2/26.1.2/net/minecraft/util/Mth).

**Parameters:**
- `x1`: the first x position of this rectangle
- `y1`: the first y position of this rectangle
- `x2`: the second x position of this rectangle
- `y2`: the second y position of this rectangle
- `color`: the colour, with the alpha in the top byte
- `rotation`: the rotation in degrees
- `zIndex`: the z-index against the other elements on the overlay

### new Rect {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_constructor-int-int-int-int-int-int-float-int-}

```js
new Rect(int x1, int y1, int x2, int y2, int color, int alpha, float rotation, int zIndex)
```

Constructs a rectangle from two corners, replacing the color's top byte with the
separately supplied alpha and normalizing rotation with [Mth.wrapDegrees(float)](https://mcsrc.dev/2/26.1.2/net/minecraft/util/Mth).

**Parameters:**
- `x1`: the first x position of this rectangle
- `y1`: the first y position of this rectangle
- `x2`: the second x position of this rectangle
- `y2`: the second y position of this rectangle
- `color`: the colour, whose top byte is replaced by the alpha
- `alpha`: the value shifted into the top byte
- `rotation`: the rotation in degrees
- `zIndex`: the z-index against the other elements on the overlay

## Fields

### color {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_color}

```js
color: int
```

The packed ARGB color. A zero-alpha value with nonzero RGB is made opaque by the
one-argument color setter; pure black remains transparent.

### parent {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_parent}

```js
parent: IDraw2D<?>
```

The overlay this rectangle belongs to, or `null` to use the window dimensions
for parent-relative alignment.

### rotateCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_rotateCenter}

```js
rotateCenter: boolean
```

Whether rotation is about the rectangle's center rather than its first corner.  
Directly constructed rectangles default to `false`; builders default to `true`.

### rotation {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_rotation}

```js
rotation: float
```

Rotation in degrees, normalized by the constructor and [setRotation(double)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setRotation-double-).

### x1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_x1}

```js
x1: int
```

The x coordinate of the first corner; it is not necessarily the left edge.

### x2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_x2}

```js
x2: int
```

The x coordinate of the second corner.

### y1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_y1}

```js
y1: int
```

The y coordinate of the first corner; it is not necessarily the top edge.

### y2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_y2}

```js
y2: int
```

The y coordinate of the second corner.

### zIndex {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_zIndex}

```js
zIndex: int
```

Draw-order value relative to other elements on the overlay; lower values draw first.

## Methods

### getAlpha {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getAlpha-}

```js
getAlpha(): int
```

Returns the alpha byte of this rectangle's packed color, from 0 (transparent) to
255 (opaque).

**Returns:** the alpha value of this rectangle.

**Since:** 1.8.4

### getColor {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getColor-}

```js
getColor(): int
```

Returns this rectangle's packed ARGB color. Colors set without an alpha byte are
made opaque by the one-argument color setter, except pure black.

**Returns:** the color value of this rectangle.

**Since:** 1.8.4

### getHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getHeight-}

```js
getHeight(): int
```

returns the absolute distance between the two y positions.

**Returns:** the height of this rectangle.

**Since:** 1.8.4

### getParentHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getParentHeight-}

```js
getParentHeight(): int
```

Returns the assigned overlay's height, or the window's GUI-scaled height when no
overlay is assigned.

**Returns:** the height of the parent

### getParentWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getParentWidth-}

```js
getParentWidth(): int
```

Returns the assigned overlay's width, or the window's GUI-scaled width when no
overlay is assigned.

**Returns:** the width of the parent

### getRotation {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getRotation-}

```js
getRotation(): float
```

Returns this rectangle's rotation in degrees, normalized by its constructor and
[setRotation(double)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setRotation-double-).

**Returns:** the rotation of this rectangle.

**Since:** 1.8.4

### getScaledHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getScaledHeight-}

```js
getScaledHeight(): int
```

Returns this rectangle's height; rectangles have no additional scale.

**Returns:** the scaled height of this element

### getScaledLeft {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getScaledLeft-}

```js
getScaledLeft(): int
```

Returns the smaller of the two x coordinates, independent of corner order.

**Returns:** the position of the scaled element's left side

### getScaledTop {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getScaledTop-}

```js
getScaledTop(): int
```

Returns the smaller of the two y coordinates, independent of corner order.

**Returns:** the position of the scaled element's top side

### getScaledWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getScaledWidth-}

```js
getScaledWidth(): int
```

Returns this rectangle's width; rectangles have no additional scale.

**Returns:** the scaled width of this element

### getWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getWidth-}

```js
getWidth(): int
```

returns the absolute distance between the two x positions.

**Returns:** the width of this rectangle.

**Since:** 1.8.4

### getX1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getX1-}

```js
getX1(): int
```

Returns the x coordinate of the first corner; it is not necessarily the left edge.

**Returns:** the first x position of this rectangle.

**Since:** 1.8.4

### getX2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getX2-}

```js
getX2(): int
```

Returns the x coordinate of the second corner; it is not necessarily the right edge.

**Returns:** the second x position of this rectangle.

**Since:** 1.8.4

### getY1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getY1-}

```js
getY1(): int
```

Returns the y coordinate of the first corner; it is not necessarily the top edge.

**Returns:** the first y position of this rectangle.

**Since:** 1.8.4

### getY2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getY2-}

```js
getY2(): int
```

the second y position of this rectangle, its other corner rather than an edge.

**Returns:** the second y position of the rectangle.

**Since:** 1.8.4

### getZIndex {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getZIndex-}

```js
getZIndex(): int
```

Returns this rectangle's overlay draw-order value; lower values draw first.

**Returns:** the z-index of this element

### isRotatingCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_isRotatingCenter-}

```js
isRotatingCenter(): boolean
```

Returns whether rotation is about the rectangle's center.

**Returns:** `true` if this rectangle should be rotated around its center, `false`
otherwise.

**Since:** 1.8.4

### moveTo {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_moveTo-int-int-}

```js
moveTo(int x, int y): Rect
```

Moves the rectangle so `(x, y)` is its first corner, preserving its current
width and height.

**Parameters:**
- `x`: the new x position
- `y`: the new y position

**Returns:** self for chaining.

### render {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_render-GuiGraphicsExtractor-int-int-float-}

```js
render(GuiGraphicsExtractor drawContext, int mouseX, int mouseY, float delta): void
```

Draws this rectangle through the overlay's graphics context. The mouse coordinates
and frame delta are unused.

**Parameters:**
- `drawContext`: the graphics context to draw into
- `mouseX`: the mouse x, not used by this element
- `mouseY`: the mouse y, not used by this element
- `delta`: the frame delta, not used by this element

### setAlpha {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setAlpha-int-}

```js
setAlpha(int alpha): Rect
```

Replaces the alpha byte of this rectangle's packed color; values outside 0–255 are
not clamped.

**Parameters:**
- `alpha`: the alpha to put in the top byte

**Returns:** self for chaining.

**Since:** 1.1.8

### setColor {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setColor-int-}

```js
setColor(int color): Rect
```

Sets the packed ARGB color, making zero-alpha colors with nonzero RGB opaque; zero-alpha black stays transparent, and explicit alpha is preserved.

**Parameters:**
- `color`: the packed colour, with the alpha in the top byte

**Returns:** self for chaining.

**Since:** 1.0.5

### setColor {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setColor-int-int-}

```js
setColor(int color, int alpha): Rect
```

sets the packed color and replaces its top byte with `alpha`; the value is
shifted directly and is not clamped to 0–255.

**Parameters:**
- `color`: the colour, whose top byte is replaced
- `alpha`: the alpha, in the top byte

**Returns:** self for chaining.

**Since:** 1.1.8

### setHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setHeight-int-}

```js
setHeight(int height): Rect
```

Resizes along the y axis, anchoring the currently smaller y coordinate. If
`y1 <= y2`, sets `y2 = y1 + height`; otherwise sets `y1 = y2 + height`.  
Negative heights are accepted and can reverse the corner order; [getHeight()](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getHeight-)
reports the absolute coordinate difference, subject to integer overflow.

**Parameters:**
- `height`: the signed y-coordinate offset in GUI-scaled pixels

**Returns:** self for chaining

**Since:** 1.8.4

### setParent {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setParent-IDraw2D-}

```js
setParent(IDraw2D<?> parent): Rect
```

Sets the overlay used for parent-relative dimensions and alignment; `null`
makes those dimensions fall back to the window. Builders assign their overlay when
creating the rectangle.

**Parameters:**
- `parent`: the overlay this element is on, or `null` for the window

**Returns:** self for chaining.

### setPos {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setPos-int-int-int-int-}

```js
setPos(int x1, int y1, int x2, int y2): Rect
```

sets both corners of this rectangle.

**Parameters:**
- `x1`: the first x position of this rectangle
- `y1`: the first y position of this rectangle
- `x2`: the second x position of this rectangle
- `y2`: the second y position of this rectangle

**Returns:** self for chaining.

**Since:** 1.1.8

### setPos1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setPos1-int-int-}

```js
setPos1(int x1, int y1): Rect
```

Sets both coordinates of the first corner, leaving the second corner unchanged.

**Parameters:**
- `x1`: the first x position of this rectangle
- `y1`: the first y position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setPos2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setPos2-int-int-}

```js
setPos2(int x2, int y2): Rect
```

sets the second corner, leaving the first corner unchanged.

**Parameters:**
- `x2`: the second x position of this rectangle
- `y2`: the second y position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setRotateCenter {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setRotateCenter-boolean-}

```js
setRotateCenter(boolean rotateCenter): Rect
```

Sets whether rotation is about the rectangle's center rather than its first corner.

**Parameters:**
- `rotateCenter`: whether this rectangle should be rotated around its center

**Returns:** self for chaining.

**Since:** 1.8.4

### setRotation {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setRotation-double-}

```js
setRotation(double rotation): Rect
```

Sets the rotation in degrees, normalized by [Mth.wrapDegrees(float)](https://mcsrc.dev/2/26.1.2/net/minecraft/util/Mth).

**Parameters:**
- `rotation`: the angle in degrees

**Returns:** self for chaining.

**Since:** 1.2.6

### setSize {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setSize-int-int-}

```js
setSize(int width, int height): Rect
```

Resizes both axes by applying [setWidth(int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setWidth-int-) then [setHeight(int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setHeight-int-).  
Each axis anchors its currently smaller coordinate; negative offsets are accepted
and can reverse the corresponding corner order.

**Parameters:**
- `width`: the signed x-coordinate offset in GUI-scaled pixels
- `height`: the signed y-coordinate offset in GUI-scaled pixels

**Returns:** self for chaining

**Since:** 1.8.4

### setWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setWidth-int-}

```js
setWidth(int width): Rect
```

Resizes along the x axis, anchoring the currently smaller x coordinate. If
`x1 <= x2`, sets `x2 = x1 + width`; otherwise sets `x1 = x2 + width`.  
Negative widths are accepted and can reverse the corner order; [getWidth()](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_getWidth-)
reports the absolute coordinate difference, subject to integer overflow.

**Parameters:**
- `width`: the signed x-coordinate offset in GUI-scaled pixels

**Returns:** self for chaining

**Since:** 1.8.4

### setX1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setX1-int-}

```js
setX1(int x1): Rect
```

Sets the first corner's x coordinate without changing any other coordinate.

**Parameters:**
- `x1`: the first x position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setX2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setX2-int-}

```js
setX2(int x2): Rect
```

Sets the second corner's x coordinate without changing any other coordinate.

**Parameters:**
- `x2`: the second x position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setY1 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setY1-int-}

```js
setY1(int y1): Rect
```

Sets the first corner's y coordinate without changing any other coordinate.

**Parameters:**
- `y1`: the first y position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setY2 {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setY2-int-}

```js
setY2(int y2): Rect
```

Sets the second corner's y coordinate without changing any other coordinate.

**Parameters:**
- `y2`: the second y position of this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4

### setZIndex {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Rect_setZIndex-int-}

```js
setZIndex(int zIndex): Rect
```

Sets this rectangle's overlay draw-order value; lower values draw first.

**Parameters:**
- `zIndex`: the new z-index for this rectangle

**Returns:** self for chaining.

**Since:** 1.8.4
