Text

Events

Change
when the text is changed

Component to draw text inside the body of an entity.

Back to top

.text

public this .text(String text)

public this .text(Function textgenerator)

text
String of text that will be inserted into the DOM or Canvas element.

This method will update the text inside the entity. If you use DOM, to modify the font, use the .css method inherited from the DOM component.

If you need to reference attributes on the entity itself you can pass a function instead of a string.

Example

Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 }).text("Look at me!!");

Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 })
    .text(function () { return "My position is " + this._x });

Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 }).text("Look at me!!");

Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 })
    .text(function () { return "My position is " + this._x });
Back to top

.textColor

public this .textColor(String color, Number strength)

color
The color in hexadecimal
strength
Level of opacity

Modify the text color and level of opacity.

Example

Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 }).text("Look at me!!")
  .textColor('#FF0000');

Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 }).text('Look at me!!')
  .textColor('#FF0000', 0.6);

See Also

Back to top

.textFont

public this .textFont(String key, * value)

key
Property of the entity to modify
value
Value to set the property to

public this .textFont(Object map)

map
Object where the key is the property to modify and the value as the property value

Use this method to set font property of the text entity.

Example

Crafty.e("2D, DOM, Text").textFont({ type: 'italic', family: 'Arial' });
Crafty.e("2D, Canvas, Text").textFont({ size: '20px', weight: 'bold' });

Crafty.e("2D, Canvas, Text").textFont("type", "italic");
Crafty.e("2D, Canvas, Text").textFont("type"); // italic