p5.js | textAlign() Function

The textAlign() function in p5.js is used to set the alignment for the drawing the text. This function accepts two parameter horizAlign and vertAlign. The horizAlign parameter sets the alignment on x-axis and the vertAlign parameter sets the alignment on y-axis.

Syntax:

textAlign( horizAlign, vertAlign)

or

textAlign()

Parameters: This function accepts two parameters as mentioned above and described below:

  • horizAlign: This parameter stores the horizontal alignment of the text as (LEFT, CENTER, or RIGHT).
  • vertAlign: This parameter stores the vertical alignment of the text as (TOP, BOTTOM, CENTER, or BASELINE).

Below programs illustrate the textAlign() function in p5.js:
Example 1: This example uses textAlign() function to align the content horizontally.



filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of given size
    createCanvas(380, 150);
}
  
function draw() {
      
    // Set the background color
    background(220);
      
    let c = 0.5;
      
    // Use atan() function to calculate
    // arc tangent value
    let ac = atan(c);
      
    // Set font size
    textSize(26);
      
    // Set font color
    fill(color('green'));
      
    // Set the text alignment to
    // CENTER, RIGHT, LEFT
    textAlign(LEFT);
    text('GEEKS', 100, 30);
    textAlign(CENTER);
    text('for', 100, 60);
    textAlign(RIGHT);
    text('GEEKS', 100, 90);
}

chevron_right


Output:

Example 2: This example uses textAlign() function to align the content vertically.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of given size
    createCanvas(380, 150);
}
  
function draw() {
      
    // Set the background color
    background(220);
      
    // Set the text size
    textSize(26);
      
    // Set the stroke width
    strokeWeight(0.5);
      
    // Set the co-ordinates for line
    line(0, height/2, width, height/2);
      
    // set the alignment for the text
    textAlign(CENTER, TOP);
      
    // Set the text 
    text('CENTER', 0, height/2, width);
}

chevron_right


Output:

Reference: https://p5js.org/reference/#/p5/textAlign

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.