Skip to content

Instantly share code, notes, and snippets.

@studioijeoma
Created October 16, 2014 16:31
Show Gist options
  • Save studioijeoma/942ced6a9c24a4739199 to your computer and use it in GitHub Desktop.
Save studioijeoma/942ced6a9c24a4739199 to your computer and use it in GitHub Desktop.
p5.js textHeight() gets height of text box – text(words,x,y,w,h)
function textHeight(text, maxWidth) {
var words = text.split(' ');
var line = '';
var h = this._textLeading;
for (var i = 0; i < words.length; i++) {
var testLine = line + words[i] + ' ';
var testWidth = drawingContext.measureText(testLine).width;
if (testWidth > maxWidth && i > 0) {
line = words[i] + ' ';
h += this._textLeading;
} else {
line = testLine;
}
}
return h;
}
@GodWoold
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment