p5.js | textAscent() Function

The textAscent() function in p5.js is used to find out the ascent of the current font at its current size. The ascent can be defined as the distance of the tallest descender character above the baseline, in pixels.

Syntax:

textAscent()

Parameters: This function has no parameters.

Return Value: It returns a number that denotes the ascent in pixels.

The example below illustrates the textAscent() function in p5.js:



Example 1: This example shows the text ascent using the default font.

filter_none

edit
close

play_arrow

link
brightness_4
code

let inputElem;
let currfontSize = 24;
let fontBaseline = 150;
  
function setup() {
  createCanvas(600, 300);
  
  // Create button to increase font size
  let fontBtn = createButton("Increase Font Size");
  fontBtn.mouseClicked(() => {
    currfontSize += 1;
  });
  fontBtn.position(20, 70);
  
  // Create input box
  inputElem = createInput("");
  inputElem.position(20, 40);
}
  
function draw() {
  clear();
  textSize(18);
  text("Write in input to change the text and observe text ascent", 10, 20);
  textSize(currfontSize);
  
  // This value depends on the font used
  let fontScalar = 0.8;
  
  // Display text and line if input not empty
  let enteredText = inputElem.value();
  if (enteredText != "") {
    text(enteredText, 20, fontBaseline);
  
    // Draw the Base line
    stroke("black");
    line(0, fontBaseline, width, fontBaseline);
  
    // Draw the Ascent Line
    stroke("green");
    ascVal = textAscent() * fontScalar;
    line(0, fontBaseline - ascVal, width, fontBaseline - ascVal);
    noStroke();
  
    textSize(18);
    text("Text Ascent Value: " + ascVal, 20, 275);
  }
}

chevron_right


Output:

textAscent-defaultFont

Example 2: This example shows the text ascent using a custom font.

filter_none

edit
close

play_arrow

link
brightness_4
code

let inputElem;
let currfontSize = 24;
let fontBaseline = 150;
let newFont;
  
function preload() {
  newFont = loadFont("fonts/Montserrat.otf");
}
  
function setup() {
  createCanvas(600, 300);
  
  textFont(newFont);
  
  // Create button to increase font size
  let fontBtn = createButton("Increase Font Size");
  fontBtn.mouseClicked(() => {
    currfontSize += 1;
  });
  fontBtn.position(20, 70);
  
  // Create input box
  inputElem = createInput("");
  inputElem.position(20, 40);
}
  
function draw() {
  clear();
  textSize(18);
  text("Write in input to change the text and observe text ascent", 10, 20);
  textSize(currfontSize);
  
  // This value depends on the font used
  let fontScalar = 0.8;
  
  // Display text and line if input not empty
  let enteredText = inputElem.value();
  if (enteredText != "") {
    text(enteredText, 20, fontBaseline);
  
    // Draw the Base line
    stroke("black");
    line(0, fontBaseline, width, fontBaseline);
  
    // Draw the Ascent Line
    stroke("green");
    ascVal = textAscent() * fontScalar;
    line(0, fontBaseline - ascVal, width, fontBaseline - ascVal);
    noStroke();
      
    textSize(18);
    text("Text Ascent Value: " + ascVal, 20, 275);
  }
}

chevron_right


Output:

textAscent-loadedFont

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

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

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.