Adding maxWidth support for fillText and strokeText

This commit is contained in:
Matthieu Ravey 2012-05-10 11:15:29 +02:00
parent 28f7fc246d
commit cd276169d7

View File

@ -18,7 +18,8 @@ Kinetic.Text = function(config) {
align: 'left', align: 'left',
verticalAlign: 'top', verticalAlign: 'top',
padding: 0, padding: 0,
fontStyle: 'normal' fontStyle: 'normal',
maxWidth: undefined
}); });
this.shapeType = "Text"; this.shapeType = "Text";
@ -65,7 +66,13 @@ Kinetic.Text = function(config) {
// draw text // draw text
if(this.attrs.textFill !== undefined) { if(this.attrs.textFill !== undefined) {
context.fillStyle = this.attrs.textFill; context.fillStyle = this.attrs.textFill;
context.fillText(this.attrs.text, tx, ty);
if (this.attrs.maxWidth !== undefined) {
context.fillText(this.attrs.text, tx, ty, this.attrs.maxWidth);
}
else {
context.fillText(this.attrs.text, tx, ty);
}
} }
if(this.attrs.textStroke !== undefined || this.attrs.textStrokeWidth !== undefined) { if(this.attrs.textStroke !== undefined || this.attrs.textStrokeWidth !== undefined) {
// defaults // defaults
@ -77,7 +84,13 @@ Kinetic.Text = function(config) {
} }
context.lineWidth = this.attrs.textStrokeWidth; context.lineWidth = this.attrs.textStrokeWidth;
context.strokeStyle = this.attrs.textStroke; context.strokeStyle = this.attrs.textStroke;
context.strokeText(this.attrs.text, tx, ty);
if (this.attrs.maxWidth !== undefined) {
context.strokeText(this.attrs.text, tx, ty, this.attrs.maxWidth);
}
else {
context.strokeText(this.attrs.text, tx, ty);
}
} }
}; };
// call super constructor // call super constructor
@ -242,6 +255,19 @@ Kinetic.Text.prototype = {
width: metrics.width, width: metrics.width,
height: parseInt(this.attrs.fontSize, 10) height: parseInt(this.attrs.fontSize, 10)
}; };
},
/**
* get max width in pixels
*/
getMaxWidth: function() {
return this.attrs.maxWidth;
},
/**
* set max width
* @param {float} max width
*/
setMaxWidth: function(maxWidth) {
this.attrs.maxWidth = maxWidth;
} }
}; };
// extend Shape // extend Shape