Professional UI components: buttons, panels, text rendering, and layout helpers
The UI System provides high-level components for building game interfaces. It includes interactive buttons with callbacks, decorative panels, advanced text rendering with shadows/outlines, progress bars, and layout utilities.
Creates an interactive button with hover effects and click callback.
const startBtn = createButton(220, 150, 200, 40, "START GAME", () => {
gameState = 'playing';
});
const quitBtn = createButton(220, 200, 200, 40, "QUIT", () => {
// Quit game
}, {
normalColor: rgba8(200, 50, 50),
hoverColor: rgba8(255, 80, 80)
});
Updates all button states (hover, click detection). Call in update() function.
function update() {
updateAllButtons(); // Check button interactions
}
Renders all created buttons. Call in draw() function.
function draw() {
cls(rgba8(0, 0, 20));
drawAllButtons(); // Render buttons
}
Removes all buttons (useful for screen transitions).
Removes a specific button.
Creates a decorative panel/window with borders, gradients, and optional title bar.
const infoPanel = createPanel(50, 50, 300, 200, {
title: "Game Info",
bgColor: rgba8(0, 0, 50, 200),
borderColor: uiColors.primary,
borderWidth: 3,
shadow: true,
gradient: true
});
Renders all panels. Call before drawing panel contents.
Removes all panels.
Draws text with current alignment and font settings.
setTextAlign('center');
setTextBaseline('middle');
drawText("MY GAME", 320, 50, uiColors.primary, 3);
Draws text with drop shadow effect.
drawTextShadow("LEVEL 1", 320, 100,
uiColors.white, uiColors.black, 3, 2);
Draws text with outline (8-direction) for visibility on any background.
drawTextOutline("SCORE: " + score, 10, 10,
uiColors.white, uiColors.black, 1);
Sets text horizontal alignment: "left", "center", or "right".
Sets text vertical alignment: "top", "middle", or "bottom".
Sets current font: "tiny", "small", "normal", "large", or "huge".
Calculates text dimensions.
Draws a progress bar (health, loading, etc.).
drawProgressBar(10, 10, 200, 20, player.hp, player.maxHp, {
fillColor: uiColors.success,
bgColor: rgba8(50, 0, 0),
showText: true
});
Calculates X coordinate to center an element horizontally.
const btnX = centerX(200); // Center 200px wide button
createButton(btnX, 150, 200, 40, "START", startGame);
Calculates Y coordinate to center an element vertically.
Creates a grid layout array for positioning elements.
const cells = grid(3, 2, 100, 50, 10, 10);
cells.forEach((cell, i) => {
createButton(cell.x + 50, cell.y + 100,
cell.width, cell.height, `BTN ${i}`, () => {});
});
The UI system provides a built-in color palette accessible via uiColors:
| Color Name | Value (RGB) | Usage |
|---|---|---|
| uiColors.primary | rgba8(0, 120, 255) | Primary buttons, borders |
| uiColors.secondary | rgba8(100, 100, 255) | Secondary elements |
| uiColors.success | rgba8(0, 255, 100) | Health bars, positive |
| uiColors.warning | rgba8(255, 200, 0) | Warnings, caution |
| uiColors.danger | rgba8(255, 50, 50) | Errors, critical |
| uiColors.white | rgba8(255, 255, 255) | Text, highlights |
| uiColors.black | rgba8(0, 0, 0) | Shadows, backgrounds |
let gameState = 'menu';
let menuButtons = [];
function init() {
// Create menu panel
createPanel(170, 80, 300, 200, {
title: "MAIN MENU",
bgColor: rgba8(0, 0, 50, 220),
borderColor: uiColors.primary,
shadow: true,
gradient: true
});
// Create buttons
const btnX = centerX(200);
createButton(btnX, 120, 200, 40, "START GAME", () => {
gameState = 'playing';
clearButtons();
clearPanels();
});
createButton(btnX, 170, 200, 40, "OPTIONS", () => {
gameState = 'options';
});
createButton(btnX, 220, 200, 40, "QUIT", () => {
// Quit
}, { normalColor: uiColors.danger });
}
function update() {
if (gameState === 'menu') {
updateAllButtons();
}
}
function draw() {
cls(rgba8(0, 0, 20));
if (gameState === 'menu') {
// Draw title
setTextAlign('center');
setFont('large');
drawTextShadow("MY AWESOME GAME", 320, 40,
uiColors.primary, uiColors.black, 3, 2);
// Draw UI
drawAllPanels();
drawAllButtons();
}
}