2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Text
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Text constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Text = function(config) {
|
2012-04-29 03:55:18 +08:00
|
|
|
this.setDefaultAttrs({
|
|
|
|
fontFamily: 'Calibri',
|
|
|
|
text: '',
|
|
|
|
fontSize: 12,
|
|
|
|
align: 'left',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
padding: 0,
|
2012-05-10 17:15:29 +08:00
|
|
|
fontStyle: 'normal',
|
2012-06-02 15:21:49 +08:00
|
|
|
width: 'auto',
|
|
|
|
detectionType: 'pixel'
|
2012-04-29 03:55:18 +08:00
|
|
|
});
|
2012-04-06 14:48:58 +08:00
|
|
|
|
|
|
|
this.shapeType = "Text";
|
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
config.drawFunc = function() {
|
|
|
|
var context = this.getContext();
|
2012-04-06 14:48:58 +08:00
|
|
|
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
2012-03-18 01:28:25 +08:00
|
|
|
context.textBaseline = 'middle';
|
2012-04-01 06:40:27 +08:00
|
|
|
var textHeight = this.getTextHeight();
|
2012-05-13 08:49:01 +08:00
|
|
|
var textWidth = this.attrs.width === 'auto' ? this.getTextWidth() : this.attrs.width;
|
2012-04-06 14:48:58 +08:00
|
|
|
var p = this.attrs.padding;
|
2012-03-07 13:45:48 +08:00
|
|
|
var x = 0;
|
|
|
|
var y = 0;
|
2012-05-21 01:47:28 +08:00
|
|
|
var that = this;
|
2012-03-07 13:45:48 +08:00
|
|
|
|
2012-04-06 14:48:58 +08:00
|
|
|
switch (this.attrs.align) {
|
2012-03-18 01:28:25 +08:00
|
|
|
case 'center':
|
2012-03-07 13:45:48 +08:00
|
|
|
x = textWidth / -2 - p;
|
|
|
|
break;
|
2012-03-18 01:28:25 +08:00
|
|
|
case 'right':
|
2012-03-07 13:45:48 +08:00
|
|
|
x = -1 * textWidth - p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-06 14:48:58 +08:00
|
|
|
switch (this.attrs.verticalAlign) {
|
2012-03-18 01:28:25 +08:00
|
|
|
case 'middle':
|
2012-03-07 13:45:48 +08:00
|
|
|
y = textHeight / -2 - p;
|
|
|
|
break;
|
2012-03-18 01:28:25 +08:00
|
|
|
case 'bottom':
|
2012-03-07 13:45:48 +08:00
|
|
|
y = -1 * textHeight - p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw path
|
|
|
|
context.save();
|
|
|
|
context.beginPath();
|
|
|
|
context.rect(x, y, textWidth + p * 2, textHeight + p * 2);
|
|
|
|
context.closePath();
|
2012-05-21 01:47:28 +08:00
|
|
|
|
2012-05-27 11:34:36 +08:00
|
|
|
this.fill();
|
|
|
|
this.stroke();
|
2012-05-21 01:47:28 +08:00
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
context.restore();
|
|
|
|
|
|
|
|
var tx = p + x;
|
|
|
|
var ty = textHeight / 2 + p + y;
|
|
|
|
|
2012-05-13 08:11:57 +08:00
|
|
|
// clipping region for max width
|
|
|
|
context.save();
|
2012-05-13 08:49:01 +08:00
|
|
|
if(this.attrs.width !== 'auto') {
|
2012-05-13 08:11:57 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(x, y, textWidth + p, textHeight + p * 2);
|
|
|
|
context.closePath();
|
|
|
|
context.clip();
|
|
|
|
}
|
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
// draw text
|
2012-05-27 11:34:36 +08:00
|
|
|
this.fillText(this.attrs.text, tx, ty);
|
2012-06-02 15:21:49 +08:00
|
|
|
this.strokeText(this.attrs.text, tx, ty);
|
|
|
|
|
2012-05-13 08:11:57 +08:00
|
|
|
context.restore();
|
2012-03-07 13:45:48 +08:00
|
|
|
};
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.apply(this, [config]);
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Text methods
|
|
|
|
*/
|
|
|
|
Kinetic.Text.prototype = {
|
2012-04-01 06:40:27 +08:00
|
|
|
/**
|
|
|
|
* 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();
|
2012-05-13 06:45:04 +08:00
|
|
|
|
2012-05-13 06:32:27 +08:00
|
|
|
/**
|
|
|
|
* if the text hasn't been added a layer yet there
|
|
|
|
* will be no associated context. Will have to create
|
|
|
|
* a dummy context
|
|
|
|
*/
|
2012-05-13 06:45:04 +08:00
|
|
|
if(!context) {
|
|
|
|
var dummyCanvas = document.createElement('canvas');
|
|
|
|
context = dummyCanvas.getContext('2d');
|
2012-05-13 06:32:27 +08:00
|
|
|
}
|
2012-05-13 06:45:04 +08:00
|
|
|
|
2012-04-01 06:40:27 +08:00
|
|
|
context.save();
|
2012-04-06 14:48:58 +08:00
|
|
|
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
|
|
|
var metrics = context.measureText(this.attrs.text);
|
2012-04-01 06:40:27 +08:00
|
|
|
context.restore();
|
|
|
|
return {
|
|
|
|
width: metrics.width,
|
2012-04-06 14:48:58 +08:00
|
|
|
height: parseInt(this.attrs.fontSize, 10)
|
2012-04-01 06:40:27 +08:00
|
|
|
};
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// extend Shape
|
2012-03-12 14:01:23 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Text, Kinetic.Shape);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
|
|
|
// add setters and getters
|
|
|
|
Kinetic.GlobalObject.addSetters(Kinetic.Text, ['fontFamily', 'fontSize', 'fontStyle', 'textFill', 'textStroke', 'textStrokeWidth', 'padding', 'align', 'verticalAlign', 'text', 'width']);
|
|
|
|
Kinetic.GlobalObject.addGetters(Kinetic.Text, ['fontFamily', 'fontSize', 'fontStyle', 'textFill', 'textStroke', 'textStrokeWidth', 'padding', 'align', 'verticalAlign', 'text', 'width']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font family
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setFontFamily
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} fontFamily
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font size
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setFontSize
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {int} fontSize
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font style. Can be "normal", "italic", or "bold". "normal" is the default.
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setFontStyle
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} fontStyle
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text fill color
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setFontFill
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} textFill
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text stroke color
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setFontStroke
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} textStroke
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text stroke width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setTextStrokeWidth
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {int} textStrokeWidth
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set padding
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setPadding
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {int} padding
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set horizontal align of text
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setAlign
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} align align can be 'left', 'center', or 'right'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set vertical align of text
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setVerticalAlign
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} verticalAlign verticalAlign can be "top", "middle", or "bottom"
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setText
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setWidth
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font family
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getFontFamily
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font size
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getFontSize
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font style
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getFontStyle
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text fill color
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getTextFill
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text stroke color
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getTextStroke
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text stroke width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getTextStrokeWidth
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get padding
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getPadding
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get horizontal align
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getAlign
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get vertical align
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getVerticalAlign
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getText
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-06-14 17:19:51 +08:00
|
|
|
* get total width including padding and borders
|
|
|
|
* @name getWidth
|
|
|
|
* @methodOf Kinetic.Text.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|