fixed bug with getTextSize() which was throwing a JS error if used before adding text to the stage

This commit is contained in:
Eric Rowell 2012-05-12 15:32:27 -07:00
parent 1146919d3d
commit 51258531bf
5 changed files with 39 additions and 4 deletions

18
dist/kinetic-core.js vendored
View File

@ -2475,7 +2475,12 @@ Kinetic.Shape.prototype = {
* .getContext() returns the context of the invisible path layer. * .getContext() returns the context of the invisible path layer.
*/ */
getContext: function() { getContext: function() {
return this.tempLayer.getContext(); if(this.tempLayer === undefined) {
return null;
}
else {
return this.tempLayer.getContext();
}
}, },
/** /**
* get shape temp layer canvas * get shape temp layer canvas
@ -3598,6 +3603,17 @@ Kinetic.Text.prototype = {
*/ */
getTextSize: function() { getTextSize: function() {
var context = this.getContext(); var context = this.getContext();
/**
* if the text hasn't been added a layer yet there
* will be no associated context. Will have to create
* a dummy context
*/
if (!context) {
var dummyCanvas = document.createElement('canvas');
context = dummyCanvas.getContext('2d');
}
context.save(); context.save();
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily; context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
var metrics = context.measureText(this.attrs.text); var metrics = context.measureText(this.attrs.text);

File diff suppressed because one or more lines are too long

View File

@ -48,7 +48,12 @@ Kinetic.Shape.prototype = {
* .getContext() returns the context of the invisible path layer. * .getContext() returns the context of the invisible path layer.
*/ */
getContext: function() { getContext: function() {
return this.tempLayer.getContext(); if(this.tempLayer === undefined) {
return null;
}
else {
return this.tempLayer.getContext();
}
}, },
/** /**
* get shape temp layer canvas * get shape temp layer canvas

View File

@ -234,6 +234,17 @@ Kinetic.Text.prototype = {
*/ */
getTextSize: function() { getTextSize: function() {
var context = this.getContext(); var context = this.getContext();
/**
* if the text hasn't been added a layer yet there
* will be no associated context. Will have to create
* a dummy context
*/
if (!context) {
var dummyCanvas = document.createElement('canvas');
context = dummyCanvas.getContext('2d');
}
context.save(); context.save();
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily; context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
var metrics = context.measureText(this.attrs.text); var metrics = context.measureText(this.attrs.text);

View File

@ -1936,6 +1936,9 @@ Test.prototype.tests = {
verticalAlign: 'middle' verticalAlign: 'middle'
}); });
// test text width before adding it to stage
test(text.getTextWidth() > 0, 'text width should have a value');
layer.add(text); layer.add(text);
stage.add(layer); stage.add(layer);