Skip to content

Text Component

Use Text for labels, HUD values, menu items, dialogue, debug output, and any canvas-rendered text.

Minimal example

html
<Text text="Hello World" size={20} color="#ffffff" />

Properties

You can use all properties from Display Object.

style

Style object from PixiJS Text.

text

text?: string

Text to display.

color

color?: string

Text color. This is a shortcut for style.fill.

size

size?: string

Font size. This is a shortcut for style.fontSize.

fontFamily

fontFamily?: string

Font family. This is a shortcut for style.fontFamily.

Typewriter

html
<Text text="Hello World" typewriter={{ speed: 1 }} />

typewriter?: { speed?: number; onComplete?: () => void; skip?: Trigger; sound?: { src: string; volume?: number; rate?: number; }; }

Object to configure typewriter effect:

  • speed: Animation speed of the typewriter effect
  • onComplete: Callback function when the animation completes
  • skip: Trigger to skip the current animation
  • sound: Sound configuration for typewriter effect
    • src: Path to the audio file to play for each character
    • volume: Volume level from 0.0 to 1.0 (default: 0.5)
    • rate: Playback rate/speed of the sound (default: 1.0)

Example with skip trigger

html
<Text text="Hello World" typewriter={ { skip } } />

<script>
import { trigger } from 'canvasengine'

const skip = trigger()

// Skip the typewriter effect.
skip.start()
</script>

Example with sound effect

html
<Text 
  text="Hello World! This is a typewriter effect with sound." 
  typewriter={{ 
    speed: 1,
    sound: {
      src: "/assets/typewriter.mp3",
      volume: 0.3,
      rate: 1.2
    }
  }} 
/>

This will play a typewriter sound effect for each character as the text appears. The sound system automatically calculates the duration of the audio file and uses it to prevent overlapping sounds, ensuring a clean typewriter effect even at high speeds.

Common Properties

PropertyTypeDescription
xnumberX-coordinate position of the display object.
ynumberY-coordinate position of the display object.
widthnumberWidth of the display object.
heightnumberHeight of the display object.
scaleobjectScale of the display object.
anchorobjectAnchor point of the display object.
skewobjectSkew of the display object.
tintnumberTint color of the display object.
rotationnumberRotation of the display object in radians.
anglenumberRotation of the display object in degrees.
zIndexnumberZ-index of the display object.
roundPixelsbooleanWhether to round pixel values.
cursorstringCursor style when hovering over the display object.
visiblebooleanVisibility of the display object.
alphanumberAlpha transparency of the display object.
pivotobjectPivot point of the display object.
filtersarrayFilters applied to the display object.
maskOfElementElement that this display object masks.
blendModestringBlend mode for rendering.
filterAreaobjectFilter area for rendering.
outlineobjectOutline effect following the object's alpha contour.
clipobjectRectangular mask used to keep or hide part of the object.
occlusionobjectLow-alpha redraw of the covered part when this object passes behind obstacles.

Layout Properties

Pour obtenir la documentation complète et détaillée sur toutes les propriétés de mise en page, consultez la documentation officielle de PixiJS Layout.

Sizing and Dimensions

PropertyTypeDescription
widthnumber/stringWidth of the display object. Accepts pixels or percentage (e.g. '50%').
heightnumber/stringHeight of the display object. Accepts pixels or percentage (e.g. '50%').
minWidthnumber/stringMinimum width the object can shrink to.
minHeightnumber/stringMinimum height the object can shrink to.
maxWidthnumber/stringMaximum width the object can expand to.
maxHeightnumber/stringMaximum height the object can expand to.
aspectRationumberRatio between width and height (e.g. 1.5 for 3:2 ratio).

Flex Layout

PropertyTypeDescription
flexDirectionstringDirection of flex items. Values: 'row', 'column', 'row-reverse', 'column-reverse'.
flexWrapstringWhether items wrap. Values: 'wrap', 'nowrap', 'wrap-reverse'.
justifyContentstringAlignment along main axis. Values: 'flex-start', 'flex-end', 'center', 'space-between', 'space-around'.
alignItemsstringAlignment along cross axis. Values: 'flex-start', 'flex-end', 'center', 'stretch', 'baseline'.
alignContentstringAlignment of lines with multiple items. Values: 'flex-start', 'flex-end', 'center', 'stretch', 'space-between', 'space-around'.
alignSelfstringOverride of parent's alignItems for specific item.
flexGrownumberGrow factor of item relative to other items.
flexShrinknumberShrink factor of item relative to other items.
flexBasisnumber/stringInitial size of item before flex growing/shrinking.
gapnumber/objectGap between items.
rowGapnumberGap between rows.
columnGapnumberGap between columns.

Positioning

PropertyTypeDescription
positionTypestringType of positioning. Values: 'relative', 'absolute', 'static'.
topnumber/stringDistance from the top edge.
rightnumber/stringDistance from the right edge.
bottomnumber/stringDistance from the bottom edge.
leftnumber/stringDistance from the left edge.

Spacing, Margins and Borders

PropertyTypeDescription
marginnumber/arraySpace outside border box. Can be single value or array for different sides.
paddingnumber/arraySpace inside border box. Can be single value or array for different sides.
bordernumber/arrayBorder width. Can be single value or array for different sides.

Object Fitting and Alignment

PropertyTypeDescription
objectFitstringHow object is resized to fit layout box. Values: 'contain', 'cover', 'fill', 'none', 'scale-down'.
objectPositionstringAnchor point of object inside layout box. E.g. 'center', 'top left'.
transformOriginstringPivot point for rotation and scaling of layout box.

Shadow

PropertyTypeDescription
blurnumberBlur strength.
colornumberColor of the shadow.
offsetobjectOffset of the shadow.
qualitynumberQuality of the shadow.

Hook before destroy

html
<script>
  import {
    signal,
    animatedSignal,
    effect,
    animatedSequence,
  } from "canvasengine";
  import MyViewport from "./viewport.ce";
  
  let bool = signal(true)
  const opacity = animatedSignal(1, { duration: 500 });

  const click = async () => {
    bool.set(!bool())
  }

  const beforeDestroy = async () => {
    await animatedSequence([
      () => opacity.set(0),
    ])
    console.log("before destroy")
  }
</script>


<Canvas antialias={true}>
     <Container onBeforeDestroy={beforeDestroy}>
        @if (bool) {
            <Rect width={300} height={300} color="red" alpha={opacity} click />
        }
    </Container>
</Canvas>