text()

Category:Drawing

Draws text onto the display positioned at x and y.

You are not limited to just drawing shapes and lines in the display, you can also write text. Use textFont() and textSize() to change the default font, Arial, and the default size, 12 pixels. Use textAlign() to change where the text is displayed relative to the (x,y) position specified. The default is that (x,y) is the bottom left corner of the text.

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

No line wrap vs. line wrap

Use parameters four and five to create a text box with line wrapping.

// Use parameters four and five to create a text box with line wrapping.
text("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.", 10, 100);

text("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.", 10, 200, 210, 300);

// Display Hello World in top left corner.
text("Hello world.", 10, 10);

Syntax

text(str, x, y, x2, y2)

Parameters

NameTypeRequired?Description
str String The text to be displayed.
x Number The x-location in pixels of the text from left to right on the display. Should be a number from 0 to 400, but negative numbers will start the text to the left of the display and numbers greater than 400 will start the text to the right of the display (possibly unseen).
y Number The y-location in pixels of the text from top to bottom on the display. Should be a number from 0 to 400, but negative numbers will start the text above the display and numbers greater than 400 will start the text below the display (possibly unseen).
x2 Number The x-location in pixels of the lower right corner of a rectangular area to display the text. Should be a number from 0 to 400, but negative numbers will start the text to the left of the display and numbers greater than 400 will start the text to the right of the display (possibly unseen).
y2 Number The y-location in pixels of the lower right corner of a rectangular area to display the text. Should be a number from 0 to 400, but negative numbers will start the text to the above of the display and numbers greater than 400 will start the text to the below of the display (possibly unseen).

Returns

No return value. Outputs to the display only.

Tips

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