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

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);

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);

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