Skip to content

Commit 828f0de

Browse files
authored
Merge pull request #7606 from perminder-17/fontAscent
Added fontAscent Docs
2 parents 55db905 + e5baa86 commit 828f0de

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

src/type/textCore.js

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,76 @@ function textCore(p5, fn) {
338338
};
339339

340340
/**
341-
* @returns - returns the descent for the current font
342-
* @private
341+
* Returns the typographic ascent of the currently active font in pixels.
342+
*
343+
* @return {number} returns the descent for the current font
344+
*
345+
* This function automatically detects the active font and calculates its
346+
* ascent, which is the maximum vertical distance between the baseline and
347+
* the highest glyph in the font's design metrics. This measurement is
348+
* intrinsic to the font itself and remains consistent regardless of specific
349+
* character combinations.
350+
*
351+
* @method fontDescent
352+
*
353+
* @example
354+
* <div modernizr='canvas'>
355+
* <code>
356+
* function setup() {
357+
* createCanvas(300, 200);
358+
* background(200);
359+
* textFont('Courier New');
360+
* textSize(20);
361+
* text('Hello World!', 35, 55);
362+
* const ascent = fontAscent();
363+
* text(`Ascent-value: ${ascent.toFixed(1)}px`, 20, 120);
364+
* }
365+
* </code>
366+
* </div>
367+
*
368+
* In the example below, you can see how to visualize the font ascent:
369+
* - A red line represents the text baseline.
370+
* - A green line is drawn at a distance equal to the ascent above the baseline.
371+
* - The text is rendered using the specified font, and the calculated ascent value is displayed.
372+
*
373+
* <div modernizr='canvas'>
374+
* <code>
375+
* function setup() {
376+
* createCanvas(300, 200);
377+
* textSize(20);
378+
* textFont('Courier New');
379+
* noLoop();
380+
* }
381+
*
382+
* function draw() {
383+
* background(255);
384+
* const yBase = 50; // Baseline Y position
385+
*
386+
* // Get font metrics
387+
* const ascent = fontAscent();
388+
* const txt = 'Hello p5.js';
389+
*
390+
* // Draw baseline (red line)
391+
* stroke(255, 0, 0);
392+
* line(0, yBase, width, yBase);
393+
*
394+
* // Draw ascent line (green line)
395+
* stroke(0, 255, 0);
396+
* line(0, yBase - ascent, width, yBase - ascent);
397+
*
398+
* // Draw text
399+
* noStroke();
400+
* fill(0);
401+
* textAlign(LEFT, BASELINE);
402+
* text(txt, 50, yBase);
403+
*
404+
* // Display the ascent metric
405+
* fill(0);
406+
* text(`Ascent-value: ${ascent.toFixed(1)}px`, 20, 120);
407+
* }
408+
* </code>
409+
* </div>
410+
*
343411
*/
344412
Renderer.prototype.fontDescent = function () {
345413
return this.textDrawingContext().measureText('_').fontBoundingBoxDescent;

0 commit comments

Comments
 (0)