mirror of
https://github.com/konvajs/konva.git
synced 2025-04-28 22:25:22 +08:00
fixed bug with getTextSize() which was throwing a JS error if used before adding text to the stage
This commit is contained in:
parent
1146919d3d
commit
51258531bf
18
dist/kinetic-core.js
vendored
18
dist/kinetic-core.js
vendored
@ -2475,7 +2475,12 @@ Kinetic.Shape.prototype = {
|
||||
* .getContext() returns the context of the invisible path layer.
|
||||
*/
|
||||
getContext: function() {
|
||||
return this.tempLayer.getContext();
|
||||
if(this.tempLayer === undefined) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return this.tempLayer.getContext();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* get shape temp layer canvas
|
||||
@ -3598,6 +3603,17 @@ Kinetic.Text.prototype = {
|
||||
*/
|
||||
getTextSize: function() {
|
||||
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.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
||||
var metrics = context.measureText(this.attrs.text);
|
||||
|
4
dist/kinetic-core.min.js
vendored
4
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@ -48,7 +48,12 @@ Kinetic.Shape.prototype = {
|
||||
* .getContext() returns the context of the invisible path layer.
|
||||
*/
|
||||
getContext: function() {
|
||||
return this.tempLayer.getContext();
|
||||
if(this.tempLayer === undefined) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return this.tempLayer.getContext();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* get shape temp layer canvas
|
||||
|
@ -234,6 +234,17 @@ Kinetic.Text.prototype = {
|
||||
*/
|
||||
getTextSize: function() {
|
||||
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.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
||||
var metrics = context.measureText(this.attrs.text);
|
||||
|
@ -1936,6 +1936,9 @@ Test.prototype.tests = {
|
||||
verticalAlign: 'middle'
|
||||
});
|
||||
|
||||
// test text width before adding it to stage
|
||||
test(text.getTextWidth() > 0, 'text width should have a value');
|
||||
|
||||
layer.add(text);
|
||||
stage.add(layer);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user