Button

The Button component creates an interactive area that can be clicked or pressed. It supports rectangular or circular shapes and reports its state through event handlers. It uses the entity’s Transform for position.

Options

OptionTypeDefaultDescription
shapeButtonShapeButtonShape.Rectangle or ButtonShape.Circumference.
widthnumber0Width in pixels. Rectangle shape only.
heightnumber0Height in pixels. Rectangle shape only.
radiusnumber0Radius in pixels. Circumference shape only.
touchEnabledbooleantrueEnables interaction with touch screens.
offsetVector2(0, 0)Offset from the entity’s position.
onClick() => voidCalled when the button is clicked.
onPressed() => voidCalled while the button is pressed.

Read-only properties

PropertyTypeDescription
pressedbooleantrue while the button is pressed.
mouseOverbooleantrue while the cursor is over the button.

Example

import { Transform, Button, ButtonShape } from "angry-pixel";

this.entityManager.createEntity([
    new Transform(),
    new Button({
        shape: ButtonShape.Rectangle,
        width: 100,
        height: 50,
        onClick: () => console.log("clicked"),
    }),
]);