label offset is now adjusted whenever the text is updated

This commit is contained in:
Eric Rowell 2013-03-17 22:01:52 -07:00
parent e43c2fbeb4
commit 4036aa5fc7
2 changed files with 26 additions and 8 deletions

View File

@ -1,10 +1,15 @@
(function() {
// constants
var NONE = 'none',
var ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'padding', 'lineHeight', 'text'],
CHANGE_KINETIC = 'Change.kinetic',
NONE = 'none',
UP = 'up',
RIGHT = 'right',
DOWN = 'down',
LEFT = 'left';
LEFT = 'left',
// cached variables
attrChangeListLen = ATTR_CHANGE_LIST.length;
/**
* Label constructor.  Blobs are defined by an array of points and
@ -32,16 +37,27 @@
Kinetic.Plugins.Label.prototype = {
_initLabel: function(config) {
var that = this,
text = null;
this.innerGroup = new Kinetic.Group();
this.createAttrs();
Kinetic.Group.call(this, config);
this.setText(new Kinetic.Text(config.text));
text = new Kinetic.Text(config.text);
this.setText(text);
this.setRect(new Kinetic.Plugins.LabelRect(config.rect));
this.innerGroup.add(this.getRect());
this.innerGroup.add(this.getText());
this.innerGroup.add(text);
this.add(this.innerGroup);
this._setGroupOffset();
this._setGroupOffset();
// update text data for certain attr changes
for(var n = 0; n < attrChangeListLen; n++) {
text.on(ATTR_CHANGE_LIST[n] + CHANGE_KINETIC, function() {
that._setGroupOffset();
});
}
},
getWidth: function() {
return this.getText().getWidth();

View File

@ -12,7 +12,7 @@ Test.Modules.LABEL = {
y: 100,
draggable: true,
text: {
text: 'Hello World!',
text: '',
fontSize: 50,
//fontFamily: 'Calibri',
//fontStyle: 'normal',
@ -28,7 +28,7 @@ Test.Modules.LABEL = {
shadowOffset: [10, 10],
shadowOpacity: 0.2,
lineJoin: 'round',
//pointerDirection: 'down',
pointerDirection: 'up',
pointerWidth: 20,
pointerHeight: 20,
cornerRadius: 5
@ -44,9 +44,11 @@ Test.Modules.LABEL = {
var afterTextWidth = label.getText().getWidth();
test(afterTextWidth > beforeTextWidth, 'label text width should have grown');
//test(afterTextWidth > beforeTextWidth, 'label text width should have grown');
label.getText().setFontSize(50);
label.getText().setText('Hello big world');
layer.draw();
}