xxxxxxxxxx
29
let txt = 'The quick brown fox jumps over the lazy dog.';
function setup() {
createCanvas(400, 400);
noStroke();
}
function draw() {
background(220);
text(txt, 100, 100);
push();
stroke('red');
line(100, 100, 100 + textWidth(txt), 100);
stroke('green');
line(100, 100 - textAscent(), 100 + textWidth(txt), 100 - textAscent());
line(100, 100 + textDescent(), 100 + textWidth(txt), 100 + textDescent());
pop();
let ix = txt.indexOf('brown');
if (ix >= 0) {
let xOffset = textWidth(txt.substr(0, ix));
push();
stroke('blue');
noFill();
rect(100 + xOffset, 100 - textAscent(), textWidth('brown'), textAscent() + textDescent());
pop();
}
}