textAlign()

Set how text is aligned

This function controls the alignment of the text you create with the text() function. It basically tell the computer how to understand the x, y coordinates the text function takes as parameter, since the text is not a point anyway. When you say textAlign(RIGHT) then call text("Hello", x, y) the right edge of the word "Hello" becomes the x value you give in the text function. Same thing with the vertical alignment, If you use textAlign(RIGHT, TOP), the top right corner becomes the x, y coordinates you give in the text function. You need to use on the predefined constants specified below.

textAlign(horizAlign, [vertAlign])
InputDescription

horizAlign

Specifies the horizontal alignment, takes values CENTER, RIGHT, LEFT

vertAlign

Specifies the vertical alignment, takes values CENTER, TOP, BOTTOM, BASELINE

Examples

    textAlign(LEFT)
    text("ABCD", 50, 25)

    textAlign(RIGHT)
    text("ABCD", 50, 50)

    textAlign(CENTER)
    text("ABCD", 50, 75)

    stroke("red")
    strokeWeight(5)
    point(50, 25)
    point(50, 50)
    point(50, 75)
    textAlign(CENTER, TOP)
    text("ABCD", 50, 15)

    textAlign(CENTER, CENTER)
    text("ABCD", 50, 40)

    textAlign(CENTER, BOTTOM)
    text("ABCD", 50, 65)

    textAlign(CENTER, BASELINE)
    text("ABCD", 50, 90)

    stroke("red")
    strokeWeight(5)
    point(50, 15)
    point(50, 40)
    point(50, 65)
    point(50, 90)

Find these documents helpful? Let the people who made them help your child learn to code at Strivemath!

We'd love to hear your Feedback/Comments here.

Last updated