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 top 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

Changing color and size

Use additional text blocks to change the appearance of text on the screen. You can change: size, color, alignment, and other properties.

textSize(60);
textAlign(CENTER, TOP);
textFont("Impact");
fill("yellow");
stroke("red");
strokeWeight(5);
text("Ada Lovelace was one of the first computer programmers!", 0, 0, 400, 400);

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

No line wrap vs. line wrap

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

// No text 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);

// Use parameters four and five to create a text box with line wrapping.
// This will wrap text in a 100 by 100 box
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, 110, 300);

Syntax

text(str, x, y, width, height)

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).
width Number The width of the rectangular area to display the text. Should be a number from 0 to 400. When the text reaches the end of this width, it will wrap to a new line.
height Number The height of the rectangular area to display the text. Should be a number from 0 to 400. When the text reaches the end of this width, it will wrap to a new line.

Returns

No return value. Outputs to the display only.

Tips

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