added new getTextSize(), getTextWidth(), and getTextHeight() methods

This commit is contained in:
Eric Rowell
2012-03-31 15:40:27 -07:00
parent 8398670a47
commit 8f104a6fad
4 changed files with 87 additions and 8 deletions

View File

@@ -36,9 +36,8 @@ Kinetic.Text = function(config) {
var context = this.getContext();
context.font = this.fontStyle + ' ' + this.fontSize + 'pt ' + this.fontFamily;
context.textBaseline = 'middle';
var metrics = context.measureText(this.text);
var textHeight = textHeight = parseInt(this.fontSize, 10);
var textWidth = metrics.width;
var textHeight = this.getTextHeight();
var textWidth = this.getTextWidth();
var p = this.padding;
var x = 0;
var y = 0;
@@ -227,6 +226,32 @@ Kinetic.Text.prototype = {
*/
getText: function() {
return this.text;
},
/**
* get text width in pixels
*/
getTextWidth: function() {
return this.getTextSize().width;
},
/**
* get text height in pixels
*/
getTextHeight: function() {
return this.getTextSize().height;
},
/**
* get text size in pixels
*/
getTextSize: function() {
var context = this.getContext();
context.save();
context.font = this.fontStyle + ' ' + this.fontSize + 'pt ' + this.fontFamily;
var metrics = context.measureText(this.text);
context.restore();
return {
width: metrics.width,
height: parseInt(this.fontSize, 10)
};
}
};
// extend Shape