textWidth() seems to only return the correct value after calling text().
function setup() {
background(0);
fill(255);
noStroke();
textSize(20);
s = "String.";
// Returns 28.9013671875
console.log(textWidth(s));
text(s, 0, 50);
// Returns 57.802734375
console.log(textWidth(s));
}
I think it's just a matter of setting this.drawingContext.font before calling this.drawingContext.measureText(), but I'm not sure what would be the preferred approach.
I'm thinking the best plan is to take this out of text(), which is inefficient since there's no reason to set the styling for every call if it hasn't changed, and instead set this.drawingContext.font anytime a relavant property (size, style, font) is updated. This way everything is always up to date for text(), textWidth() and textHeight();
textWidth()seems to only return the correct value after callingtext().I think it's just a matter of setting
this.drawingContext.fontbefore callingthis.drawingContext.measureText(), but I'm not sure what would be the preferred approach.I'm thinking the best plan is to take this out of
text(), which is inefficient since there's no reason to set the styling for every call if it hasn't changed, and instead setthis.drawingContext.fontanytime a relavant property (size, style, font) is updated. This way everything is always up to date fortext(),textWidth()andtextHeight();