Files
konva/src/plugins/Label.js

310 lines
9.6 KiB
JavaScript
Raw Normal View History

2013-03-15 23:35:40 -07:00
(function() {
// constants
var ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'padding', 'lineHeight', 'text'],
CHANGE_KINETIC = 'Change.kinetic',
NONE = 'none',
UP = 'up',
RIGHT = 'right',
DOWN = 'down',
LEFT = 'left',
LABEL = 'Label',
// cached variables
attrChangeListLen = ATTR_CHANGE_LIST.length;
2013-03-15 23:35:40 -07:00
/**
2013-05-18 22:30:57 -07:00
* Label constructor.  Labels are groups that contain a Text and Tag shape
2013-03-15 23:35:40 -07:00
* @constructor
2013-05-16 00:28:49 -07:00
* @memberof Kinetic
2013-03-15 23:35:40 -07:00
* @param {Object} config
2013-04-06 22:06:11 -07:00
* @param {Object} config.text Text config
* @param {String} [config.text.fontFamily] default is Calibri
* @param {Number} [config.text.fontSize] in pixels. Default is 12
* @param {String} [config.text.fontStyle] can be normal, bold, or italic. Default is normal
2013-04-06 22:06:11 -07:00
* @param {String} config.text.text
* @param {String} [config.text.align] can be left, center, or right
* @param {Number} [config.text.padding]
* @param {Number} [config.text.lineHeight] default is 1
2013-05-18 22:30:57 -07:00
* @param {Object} [config.tag] Tag config
* @param {String} [config.tag.pointerDirection] can be up, right, down, left, or none; the default
2013-04-06 22:06:11 -07:00
* is none. When a pointer is present, the positioning of the label is relative to the tip of the pointer.
2013-05-18 22:30:57 -07:00
* @param {Number} [config.tag.pointerWidth]
* @param {Number} [config.tag.pointerHeight]
* @param {Number} [config.tag.cornerRadius]
2013-03-15 23:35:40 -07:00
* {{NodeParams}}
2013-05-18 22:30:57 -07:00
* @example
* var label = new Kinetic.Label({<br>
* x: 350,<br>
* y: 50,<br>
* opacity: 0.75,<br>
* text: {<br>
* text: 'Simple label',<br>
* fontFamily: 'Calibri',<br>
* fontSize: 18,<br>
* padding: 5,<br>
* fill: 'black'<br>
* },<br>
* tag: {<br>
* fill: 'yellow',<br>
* }<br>
* });<br><br>
*
* var tooltip = new Kinetic.Label({<br>
* x: 170,<br>
* y: 75,<br>
* opacity: 0.75,<br>
* text: {<br>
* text: 'Tooltip pointing down',<br>
* fontFamily: 'Calibri',<br>
* fontSize: 18,<br>
* padding: 5,<br>
* fill: 'white'<br>
* },<br>
* tag: {<br>
* fill: 'black',<br>
* pointerDirection: 'down',<br>
* pointerWidth: 10,<br>
* pointerHeight: 10,<br>
* lineJoin: 'round',<br>
* shadowColor: 'black',<br>
* shadowBlur: 10,<br>
* shadowOffset: 10,<br>
* shadowOpacity: 0.5<br>
* }<br>
* });
2013-03-15 23:35:40 -07:00
*/
Kinetic.Label = function(config) {
2013-03-15 23:35:40 -07:00
this._initLabel(config);
};
Kinetic.Label.prototype = {
2013-03-15 23:35:40 -07:00
_initLabel: function(config) {
var that = this,
text = null;
this.innerGroup = new Kinetic.Group();
2013-03-15 23:35:40 -07:00
this.createAttrs();
this.className = LABEL;
2013-03-15 23:35:40 -07:00
Kinetic.Group.call(this, config);
text = new Kinetic.Text(config.text);
this.setText(text);
2013-05-18 22:30:57 -07:00
this.setTag(new Kinetic.Tag(config.tag));
this.innerGroup.add(this.getTag());
this.innerGroup.add(text);
this.add(this.innerGroup);
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();
},
getHeight: function() {
return this.getText().getHeight();
},
_setGroupOffset: function() {
var text = this.getText(),
width = text.getWidth(),
height = text.getHeight(),
2013-05-18 22:30:57 -07:00
tag = this.getTag(),
pointerDirection = tag.getPointerDirection(),
pointerWidth = tag.getPointerWidth(),
pointerHeight = tag.getPointerHeight(),
x = 0,
y = 0;
switch(pointerDirection) {
case UP:
x = width / 2;
y = -1 * pointerHeight;
break;
case RIGHT:
x = width + pointerWidth;
y = height / 2;
break;
case DOWN:
x = width / 2;
y = height + pointerHeight;
break;
case LEFT:
x = -1 * pointerWidth;
y = height / 2;
break;
}
this.setOffset({
x: x,
y: y
});
2013-03-15 23:35:40 -07:00
}
};
2013-05-16 00:28:49 -07:00
Kinetic.Util.extend(Kinetic.Label, Kinetic.Group);
2013-04-06 22:06:11 -07:00
2013-05-16 00:28:49 -07:00
Kinetic.Node.addGetterSetter(Kinetic.Label, 'text');
2013-04-06 22:06:11 -07:00
/**
* get Text shape for the label. You need to access the Text shape in order to update
* the text properties
* @name getText
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Label.prototype
2013-04-06 22:06:11 -07:00
*/
2013-05-16 00:28:49 -07:00
2013-05-18 22:30:57 -07:00
Kinetic.Node.addGetterSetter(Kinetic.Label, 'tag');
2013-04-06 22:06:11 -07:00
2013-05-16 00:28:49 -07:00
/**
2013-05-18 22:30:57 -07:00
* get Tag shape for the label. You need to access the Tag shape in order to update
2013-05-16 00:28:49 -07:00
* the pointer properties and the corner radius
2013-05-18 22:30:57 -07:00
* @name getTag
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Label.prototype
*/
2013-04-06 22:06:11 -07:00
/**
2013-05-18 22:30:57 -07:00
* Tag constructor.&nbsp; A Tag can be configured
2013-04-06 22:06:11 -07:00
* to have a pointer element that points up, right, down, or left
* @constructor
2013-05-16 00:28:49 -07:00
* @memberof Kinetic
2013-04-06 22:06:11 -07:00
* @param {Object} config
* @param {String} [config.pointerDirection] can be up, right, down, left, or none; the default
* is none. When a pointer is present, the positioning of the label is relative to the tip of the pointer.
* @param {Number} [config.pointerWidth]
* @param {Number} [config.pointerHeight]
* @param {Number} [config.cornerRadius]
*/
2013-05-18 22:30:57 -07:00
Kinetic.Tag = function(config) {
this._initTag(config);
};
2013-05-18 22:30:57 -07:00
Kinetic.Tag.prototype = {
_initTag: function(config) {
this.createAttrs();
Kinetic.Shape.call(this, config);
2013-05-18 22:30:57 -07:00
this.shapeType = 'Tag';
this._setDrawFuncs();
},
drawFunc: function(canvas) {
var label = this.getParent().getParent(),
context = canvas.getContext(),
width = label.getWidth(),
height = label.getHeight(),
pointerDirection = this.getPointerDirection(),
pointerWidth = this.getPointerWidth(),
pointerHeight = this.getPointerHeight(),
cornerRadius = this.getCornerRadius();
context.beginPath();
context.moveTo(0,0);
if (pointerDirection === UP) {
context.lineTo((width - pointerWidth)/2, 0);
context.lineTo(width/2, -1 * pointerHeight);
context.lineTo((width + pointerWidth)/2, 0);
}
context.lineTo(width, 0);
if (pointerDirection === RIGHT) {
context.lineTo(width, (height - pointerHeight)/2);
context.lineTo(width + pointerWidth, height/2);
context.lineTo(width, (height + pointerHeight)/2);
}
context.lineTo(width, height);
if (pointerDirection === DOWN) {
context.lineTo((width + pointerWidth)/2, height);
context.lineTo(width/2, height + pointerHeight);
context.lineTo((width - pointerWidth)/2, height);
}
context.lineTo(0, height);
if (pointerDirection === LEFT) {
context.lineTo(0, (height + pointerHeight)/2);
context.lineTo(-1 * pointerWidth, height/2);
context.lineTo(0, (height - pointerHeight)/2);
}
context.closePath();
canvas.fillStroke(this);
}
};
2013-05-18 22:30:57 -07:00
Kinetic.Util.extend(Kinetic.Tag, Kinetic.Shape);
Kinetic.Node.addGetterSetter(Kinetic.Tag, 'pointerDirection', NONE);
2013-04-06 22:06:11 -07:00
/**
* set pointer Direction
* @name setPointerDirection
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
2013-04-06 22:06:11 -07:00
* @param {String} pointerDirection can be up, right, down, left, or none. The
* default is none
*/
/**
* get pointer Direction
* @name getPointerDirection
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
*/
2013-05-18 22:30:57 -07:00
Kinetic.Node.addGetterSetter(Kinetic.Tag, 'pointerWidth', 0);
/**
2013-04-06 22:06:11 -07:00
* set pointer width
* @name setPointerWidth
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
2013-04-06 22:06:11 -07:00
* @param {Number} pointerWidth
*/
/**
* get pointer width
* @name getPointerWidth
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
2013-04-06 22:06:11 -07:00
*/
2013-05-18 22:30:57 -07:00
Kinetic.Node.addGetterSetter(Kinetic.Tag, 'pointerHeight', 0);
2013-04-06 22:06:11 -07:00
/**
* set pointer height
* @name setPointerHeight
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
* @param {Number} pointerHeight
2013-04-06 22:06:11 -07:00
*/
/**
* get pointer height
* @name getPointerHeight
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
2013-04-06 22:06:11 -07:00
*/
2013-05-18 22:30:57 -07:00
Kinetic.Node.addGetterSetter(Kinetic.Tag, 'cornerRadius', 0);
/**
* set corner radius
* @name setCornerRadius
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
* @param {Number} corner radius
2013-04-06 22:06:11 -07:00
*/
/**
* get corner radius
* @name getCornerRadius
2013-05-16 00:28:49 -07:00
* @method
2013-05-18 22:30:57 -07:00
* @memberof Kinetic.Tag.prototype
2013-04-06 22:06:11 -07:00
*/
})();