Skip to content

Instantly share code, notes, and snippets.

@v12
Created January 23, 2014 02:15
Show Gist options
  • Save v12/8571640 to your computer and use it in GitHub Desktop.
Save v12/8571640 to your computer and use it in GitHub Desktop.
Some useful snippets for Canvas
Context2d.prototype.fillTextWrap = function (text, x, y, maxWidth, lineHeight) {
var words = text.split(' '),
line = '',
yOffset = 0,
context = this;
words.forEach(function (word, index) {
line += word + ' ';
if(context.measureText(line).width > maxWidth || index == words.length-1) {
context.fillText(line, x, y + yOffset);
line = '';
yOffset += lineHeight;
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment