Phaser Wind Button Component

Example of button using PhaserWind tokens with theming and design system integration.

Phaser Wind Button

Example of button using PhaserWind tokens with theming and design system integration. Demonstrates how to create consistent, themed UI components using PhaserWind design tokens.

Features

  • 🎨 Design Tokens: Uses PhaserWind color and spacing tokens
  • đŸ–ąī¸ Interactive: Hover and click states with smooth transitions
  • đŸŽ¯ Consistent: Follows design system principles
  • ⚡ Performance: Optimized for 60fps gameplay

Live Example

Loading game...

Usage Example

import {
  Color,
  type ColorToken,
  createTheme,
  PHASER_WIND_KEY,
  PhaserWindPlugin,
  SceneWithPhaserWind,
} from 'phaser-wind';

const theme = createTheme({});
type Theme = typeof theme;

class ButtonScene extends SceneWithPhaserWind<Theme> {
  create(): void {
    const { pw } = this;
    this.cameras.main.setBackgroundColor(Color.slate(900));

    const createButton = (
      x: number,
      y: number,
      label: string,
      bgToken: ColorToken
    ): Phaser.GameObjects.Container => {
      const text = this.add.text(0, 0, label, {
        fontSize: pw.fontSize.css('lg'),
        color: pw.color.rgb('white'),
      });

      const padX = pw.spacing.px('6');
      const padY = pw.spacing.px('3');
      const width = text.width + padX * 2;
      const height = text.height + padY * 2;

      const bg = this.add.rectangle(0, 0, width, height, pw.color.hex(bgToken));
      bg.setOrigin(0.5);

      const container = this.add.container(x, y, [bg, text]);
      text.setPosition(-text.width / 2, -text.height / 2);
      container.setSize(width, height);

      bg.setInteractive();

      // Hover and click states
      const highColor = bgToken.replace('-500', '-600') as ColorToken;
      const lowColor = bgToken.replace('-500', '-700') as ColorToken;

      bg.on('pointerover', () => (bg.fillColor = pw.color.hex(highColor)));
      bg.on('pointerout', () => (bg.fillColor = pw.color.hex(bgToken)));
      bg.on('pointerdown', () => (bg.fillColor = pw.color.hex(lowColor)));
      bg.on('pointerup', () => (bg.fillColor = pw.color.hex(highColor)));

      return container;
    };

    createButton(300, 120, 'Primary', 'green-500');
    createButton(300, 200, 'Secondary', 'blue-500');
    createButton(300, 280, 'Danger', 'red-500');
  }
}

Design Tokens

Color Tokens

  • â€ĸ green-500 - Primary button color
  • â€ĸ blue-500 - Secondary button color
  • â€ĸ red-500 - Danger button color
  • â€ĸ white - Text color

Spacing Tokens

  • â€ĸ px('6') - Horizontal padding
  • â€ĸ px('3') - Vertical padding
  • â€ĸ css('lg') - Font size
  • â€ĸ slate(900) - Background color

Interactive States

Default

Base button state

Hover

Mouse over state

Pressed

Click/press state