Created
October 16, 2014 16:31
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://editor.p5js.org/ThatsTrue/sketches/QmHcXTd_o