Skip to content

Instantly share code, notes, and snippets.

@swanson
Created February 15, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swanson/270dafa59af3f2491c1b to your computer and use it in GitHub Desktop.
Save swanson/270dafa59af3f2491c1b to your computer and use it in GitHub Desktop.
pixi wordbreak
Text.prototype.wordWrapWordBreak = function(text) {
var result = '';
var lines = text.split('\n');
var wordWrapWidth = this._style.wordWrapWidth;
for (var i = 0; i < lines.length; i++) {
var spaceLeft = wordWrapWidth;
var characters = lines[i].split('');
for (var j = 0; j < characters.length; j++) {
var characterWidth = this.context.measureText(characters[j]).width;
if (j === 0 || characterWidth > spaceLeft) {
// Skip printing the newline if it's the first word of the line that is
// greater than the word wrap width.
if (j > 0) {
result += '\n';
}
result += characters[j];
spaceLeft = wordWrapWidth - characterWidth;
}
else {
spaceLeft -= characterWidth;
result += characters[j];
}
}
if (i < lines.length - 1) {
result += '\n';
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment