Created
February 15, 2016 14:46
-
-
Save swanson/270dafa59af3f2491c1b to your computer and use it in GitHub Desktop.
pixi wordbreak
This file contains hidden or 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
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