pixi.js
    Preparing search index...

    Class HTMLTextStyle

    A TextStyle object rendered by the HTMLTextSystem.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    tagStyles: Record<string, HTMLTextStyleOptions>

    Custom styles to apply to specific HTML tags. Allows for consistent styling of custom elements without CSS overrides.

    new HTMLText({
    text:'<red>Red</red>,<blue>Blue</blue>,<green>Green</green>',
    style:{
    fontFamily: 'DM Sans',
    fill: 'white',
    fontSize:100,
    tagStyles:{
    red:{
    fill:'red',
    },
    blue:{
    fill:'blue',
    },
    green:{
    fill:'green',
    }
    }
    }
    );
    defaultDropShadow: TextDropShadow = ...

    Default drop shadow settings used when enabling drop shadows on text. These values are used as the base configuration when drop shadows are enabled without specific settings.

    // Customize default settings globally
    TextStyle.defaultDropShadow.alpha = 0.5; // 50% opacity for all shadows
    TextStyle.defaultDropShadow.blur = 2; // 2px blur for all shadows
    TextStyle.defaultDropShadow.color = 'blue'; // Blue shadows by default
    defaultTextStyle: TextStyleOptions = ...

    Default text style settings used when creating new text objects. These values serve as the base configuration and can be customized globally.

    // Customize default text style globally
    TextStyle.defaultTextStyle.fontSize = 16;
    TextStyle.defaultTextStyle.fill = 0x333333;
    TextStyle.defaultTextStyle.fontFamily = ['Arial', 'Helvetica', 'sans-serif'];

    Accessors

    • get breakWords(): boolean

      Indicates if lines can be wrapped within words, it needs wordWrap to be set to true.

      Returns boolean

    • set breakWords(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get cssOverrides(): string[]
      Advanced

      Returns string[]

    • set cssOverrides(value: string | string[]): void
      Advanced

      List of CSS style overrides to apply to the HTML text. These styles are added after the built-in styles and can override any default styling.

      Parameters

      • value: string | string[]

      Returns void

    • get cssStyle(): string
      Advanced

      The CSS style string that will be applied to the HTML text.

      Returns string

    • set fill(value: FillInput): void

      Sets the fill style for the text. HTML text only supports color fills (string or number values). Texture fills are not supported and will trigger a warning in debug mode.

      Parameters

      • value: FillInput

        The fill color to use. Must be a string or number.

      Returns void

      // Using hex colors
      const text = new HTMLText({
      text: 'Colored Text',
      style: {
      fill: 0xff0000 // Red color
      }
      });

      // Using CSS color strings
      text.style.fill = '#00ff00'; // Hex string (Green)
      text.style.fill = 'blue'; // Named color
      text.style.fill = 'rgb(255,0,0)' // RGB
      text.style.fill = '#f0f'; // Short hex

      // Invalid usage (will trigger warning in debug)
      text.style.fill = {
      type: 'pattern',
      texture: Texture.from('pattern.png')
      }; // Not supported, falls back to default

      In debug mode when attempting to use unsupported fill types

      TextStyle#fill For full fill options in canvas text

    • get filters(): Filter[]

      An optional filter or array of filters to apply to the text, allowing for advanced visual effects. These filters will be applied to the text as it is created, resulting in faster rendering for static text compared to applying the filter directly to the text object (which would be applied at run time).

      Returns Filter[]

      null
      
    • set filters(value: Filter[]): void

      Parameters

      Returns void

    • get fontFamily(): string | string[]

      The font family, can be a single font name, or a list of names where the first is the preferred font.

      Returns string | string[]

    • set fontFamily(value: string | string[]): void

      Parameters

      • value: string | string[]

      Returns void

    • get fontSize(): number

      The font size (as a number it converts to px, but as a string, equivalents are '26px','20pt','160%' or '1.6em')

      Returns number

    • set fontSize(value: string | number): void

      Parameters

      • value: string | number

      Returns void

    • get leading(): number

      The space between lines.

      Returns number

    • set leading(value: number): void

      Parameters

      • value: number

      Returns void

    • get letterSpacing(): number

      The amount of spacing between letters, default is 0.

      Returns number

    • set letterSpacing(value: number): void

      Parameters

      • value: number

      Returns void

    • get lineHeight(): number

      The line height, a number that represents the vertical space that a letter uses.

      Returns number

    • set lineHeight(value: number): void

      Parameters

      • value: number

      Returns void

    • get padding(): number

      Occasionally some fonts are cropped. Adding some padding will prevent this from happening by adding padding to all sides of the text.

      Note

      This will NOT affect the positioning or bounds of the text.

      Returns number

    • set padding(value: number): void

      Parameters

      • value: number

      Returns void

    • set stroke(value: StrokeInput): void

      Sets the stroke style for the text. HTML text only supports color strokes (string or number values). Texture strokes are not supported and will trigger a warning in debug mode.

      Parameters

      • value: StrokeInput

        The stroke style to use. Must be a string, number, or stroke configuration object

      Returns void

      // Using hex colors
      const text = new HTMLText({
      text: 'Outlined Text',
      style: {
      stroke: 0xff0000 // Red outline
      }
      });

      // Using CSS color strings
      text.style.stroke = '#00ff00'; // Hex string (Green)
      text.style.stroke = 'blue'; // Named color
      text.style.stroke = 'rgb(255,0,0)' // RGB
      text.style.stroke = '#f0f'; // Short hex

      // Using stroke width
      text.style = {
      stroke: {
      color: '#ff0000',
      width: 2
      }
      };

      // Remove stroke
      text.style.stroke = null;

      // Invalid usage (will trigger warning in debug)
      text.style.stroke = {
      type: 'pattern',
      texture: Texture.from('pattern.png')
      }; // Not supported, falls back to default

      In debug mode when attempting to use unsupported stroke types

      TextStyle#stroke For full stroke options in canvas text

    • get trim(): boolean

      Trim transparent borders from the text texture.

      Important

      PERFORMANCE WARNING: This is a costly operation as it requires scanning pixel alpha values. Avoid using trim: true for dynamic text, as it could significantly impact performance.

      Returns boolean

    • set trim(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get whiteSpace(): TextStyleWhiteSpace

      How newlines and spaces should be handled. Default is 'pre' (preserve, preserve).

      value New lines Spaces
      'normal' Collapse Collapse
      'pre' Preserve Preserve
      'pre-line' Preserve Collapse

      Returns TextStyleWhiteSpace

    • set whiteSpace(value: TextStyleWhiteSpace): void

      Parameters

      Returns void

    • get wordWrap(): boolean

      Indicates if word wrap should be used.

      Returns boolean

    • set wordWrap(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get wordWrapWidth(): number

      The width at which text will wrap, it needs wordWrap to be set to true.

      Returns number

    • set wordWrapWidth(value: number): void

      Parameters

      • value: number

      Returns void

    Methods

    • Advanced

      Add a style override, this can be any CSS property it will override any built-in style. This is the property and the value as a string (e.g., color: red). This will override any other internal style.

      Parameters

      • ...value: string[]

        CSS style(s) to add.

      Returns void

      style.addOverride('background-color: red');
      @advanced
    • Creates a new HTMLTextStyle object with the same values as this one. This creates a deep copy of all style properties, including dropShadow and tag styles.

      Returns HTMLTextStyle

      A new HTMLTextStyle instance with the same properties

      // Create original style
      const originalStyle = new HTMLTextStyle({
      fontSize: 24,
      fill: '#ff0000',
      tagStyles: {
      header: { fontSize: 32, fill: '#00ff00' }
      }
      });

      // Clone the style
      const clonedStyle = originalStyle.clone();

      // Modify cloned style independently
      clonedStyle.fontSize = 36;
      clonedStyle.fill = '#0000ff';

      // Original style remains unchanged
      console.log(originalStyle.fontSize); // Still 24
      console.log(originalStyle.fill); // Still '#ff0000'

      Properties that are cloned:

      • Basic text properties (fontSize, fontFamily, etc.)
      • Fill and stroke styles
      • Drop shadow configuration
      • CSS overrides
      • Tag styles (deep copied)
      • Word wrap settings
      • Alignment and spacing
    • Destroys this text style.

      Parameters

      Returns void

      // Destroy the text style and its textures
      textStyle.destroy({ texture: true, textureSource: true });
      textStyle.destroy(true);
    • Advanced

      Remove any overrides that match the value.

      Parameters

      • ...value: string[]

        CSS style to remove.

      Returns void

      style.removeOverride('background-color: red');
      @advanced
    • Resets all properties to the default values

      Returns void

    • Advanced

      Updates the text style and triggers a refresh of the CSS style cache. This method is called automatically when style properties are changed.

      Returns void

      // Update after multiple changes
      const text = new HTMLText({
      text: 'Hello World',
      style
      });

      style.fontSize = 32;
      style.fill = '#00ff00';
      style.fontFamily = 'Arial';
      style.update(); // Apply all changes at once