textAlign()

Category:Drawing

Sets the alignment for drawing text.

There are three horizontal (LEFT, CENTER, or RIGHT) and four vertical (TOP, BOTTOM, CENTER, or BASELINE) text alignment options to change where the text() is displayed relative to the (x,y) position specified. The default is (x,y) is the bottom left corner of the text (LEFT, BASELINE).

Text that does not fit completely within the display area will not be drawn or seen. Use the fourth and fifth parameters to create a text box to display the text in with automatic line wrapping.

Examples

Vertical Alignments

Display various text vertical alignments.

// Display various text vertical alignments.
textSize(18);
text("Dog "+textSize(), 0, 18);
textAlign("left", "baseline");  // Same as the default.
text("Dog "+textSize(), 75, 18);
textAlign("Left", "Bottom");    // Bottom of the letters that go below the baseline on the y.
text("Dog "+textSize(), 150, 18);
textAlign("Left", "Center");    // Letters centered on the y.
text("Dog "+textSize(), 225, 18);
textAlign("Left", "Top");       // Top of the Letters on the y.
text("Dog "+textSize(), 300, 18);

Horizontal Alignments

Display various text horizontal alignments.

// Display various text horizontal alignments.
textSize(18);
text("Dog "+textSize(), 200, 75);
textAlign(LEFT);  // Same as the default.
text("Dog "+textSize(), 200, 150);
textAlign("Center");    // Text centered on the x.
text("Dog "+textSize(), 200, 225);
textAlign("Right");    // Text right justified on the x.
text("Dog "+textSize(), 200, 300);

Syntax

textAlign(horiz, vert)

Parameters

NameTypeRequired?Description
horiz String Where to align the text horizontally relative to the x position specified in text(). Possible values "Left", "Center", or "Right".
vert String Where to align the text vertically relative to the y position specified in text(). Possible values "Top", "Bottom", "Center", or "Baseline".

Returns

No return value. Changes future text output to the display only.

Found a bug in the documentation? Let us know at documentation@code.org