From the example of textAscent() in reference I built a littel test. Actually i was going to ask a different question, but then I find a wierd behavior that I can not understand:
There is a jump in values of textAscent and textDescent when textSize changes from 193.13 to 193.15.
Could not understand why.
the code, heavily based on the example:
//move mouse to set font size
let base;
let scalar;
let asc;
let dsc;
function setup() {
createCanvas(400, 400);
base = 350;
scalar = 0.8; // Different for each font
}
function draw() {
background(220);
//alter font size - i narrowed dowm the values, but you can change this
textSize(map(mouseY, 0, height, 180, 200));
// asc and desc calc
asc = textAscent() * scalar;
dsc = textDescent() * scalar;
//baseline
stroke(20, 240, 240);
line(0, base, width, base);
//draw asc desc
stroke(240, 240, 10);
line(0, base - asc, width, base - asc);
line(0, base + dsc, width, base + dsc);
//the text
text("dp", 10, base); // Draw text on baseline
}
function mouseMoved() {
console.log(
`text size= ${nf(textSize(), 3, 2)} asc= ${nf(asc, 3, 2)} dsc = ${nf(
dsc,
3,
2
)}`
);
}
Im done for the night will came back to investigate, weird enough if you change the size of the window, the behavior comes and go… So now there is to many variables to isolate, later I’ll try.
Thanks @quark, I haven’t been able to return to this yet. But I’ve noticed a diferent behaviour across browsers. In p5js online editor if you move de division in the page (between the code and the canvas) the lines jumps, even with scalar = 1. But I think the editor is not using the last version of p5js… so I’ll need to do some more tests.
Anyway, how the window’s size relate to textAscent() and textDescent() is a mistery to me.