đź§± UI Components

KAPLAY‑UI provides a small set of lightweight, game‑ready UI components.

All components behave like native KAPLAY objects, integrating naturally with scenes, layers, positions, and events.


🔤 TextButton

A clickable button with centered text.

const btn = k.addTextButton("Play");

Signature

The addTextButton method takes a required string and an optional object.

k.addTextButton(txt: string, opts?: {});

Options

All fields are optional. Defaults are shown below.

Option Type Default
width number 150
height number 60
posX number 0
posY number 0
radius number 10
outline number 3
txtSize number 22
btnColor [number, number, nuber] [200, 200, 200]
outlineColor string #5c5b5b
txtColor [number, number, nuber] [0, 0, 0]

Default Styling

These are the built‑in visual defaults applied automatically.

The button object is created with:

The button text is created with:


Text Button Instance Methods

The returned ButtonComponent exposes the following mutator methods, allowing the button to be updated after creation:

Layout & Geometry

Text

Colors


Examples

// Basic usage
const btn1 = addTextButton("Play");
btn1.onClick(() => k.go("game"));

// Runtime mutation
const btn2 = addTextButton("Start");
btn2.setSize(300, 120);
btn2.setButtonColor([255, 100, 100]);
btn2.setButtonText("Go!");

// Visual-only updates
const btn3 = addTextButton("Options", {
  radius: 2,
});
btn3.setButtonOutlineColor([0, 0, 0]);
btn3.setButtonTextSize(24);

🏷️ Label

A small, fast text element ideal for HUDs, overlays, and lightweight UI.

const score = k.addLabel("Score: 0");

Signature

The addLabel method takes a required string and an optional object.

k.addLabel(txt: string, opts?: {});

Options

All fields are optional. Defaults are shown below.

Option Type Default
widht number 160
height number 96
posX number 0
posY number 0
opacity number 0.7
txtSize number 22
lblColor [number, number, number] [0, 0, 0]
txtColor [number, number, number] [255, 255, 255]

Default Styling

Style Value
label anchor “topleft”
label text anchor “center”

Label Instance Methods

The returned LabelComponent exposes helper methods that allow the label to be updated dynamically at runtime.

Layout & Geometry

Appearance

Text


Example

// Basic usage
const lbl = addLabel("Score: 0");
lbl.setPosition(20, 20);

// Dynamic text updates (e.g. score counter)
let score = 0;
const scoreLbl = addLabel(`Score: ${score}`);

k.wait(2, () => {
  score++;
  scoreLbl.setLabelText(`Score: ${score}`);
});

// Runtime appearance updates
const title = addLabel("Game Over", {
  txtSize: 36,
});
title.setLabelColor([40, 40, 40]);
title.setLabelTextColor([255, 80, 80]);
title.setOpacity(0.9);

// Layout adjustment after creation
const hudLabel = addLabel("Paused");
hudLabel.setSize(200, 60);
hudLabel.setLabelAnchor("center");

📦 More Components Coming Soon

Planned UI components include:

These will be added here as the library grows.