From cd276169d7ab59e4a50ad56de64c83ca7524f072 Mon Sep 17 00:00:00 2001 From: Matthieu Ravey Date: Thu, 10 May 2012 11:15:29 +0200 Subject: [PATCH] Adding maxWidth support for fillText and strokeText --- src/shapes/Text.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/shapes/Text.js b/src/shapes/Text.js index 58128ed6..73ddd3e2 100644 --- a/src/shapes/Text.js +++ b/src/shapes/Text.js @@ -18,7 +18,8 @@ Kinetic.Text = function(config) { align: 'left', verticalAlign: 'top', padding: 0, - fontStyle: 'normal' + fontStyle: 'normal', + maxWidth: undefined }); this.shapeType = "Text"; @@ -65,7 +66,13 @@ Kinetic.Text = function(config) { // draw text if(this.attrs.textFill !== undefined) { 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) { // defaults @@ -77,7 +84,13 @@ Kinetic.Text = function(config) { } context.lineWidth = this.attrs.textStrokeWidth; 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 @@ -242,6 +255,19 @@ Kinetic.Text.prototype = { width: metrics.width, 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