konva/src/shapes/Text.js

276 lines
6.1 KiB
JavaScript
Raw Normal View History

///////////////////////////////////////////////////////////////////////
// Text
///////////////////////////////////////////////////////////////////////
/**
* Text constructor
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
Kinetic.Text = function(config) {
this.setDefaultAttrs({
fontFamily: 'Calibri',
text: '',
fontSize: 12,
align: 'left',
verticalAlign: 'top',
padding: 0,
fontStyle: 'normal',
width: 'auto',
detectionType: 'pixel'
});
this.shapeType = "Text";
config.drawFunc = function() {
var context = this.getContext();
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
context.textBaseline = 'middle';
var textHeight = this.getTextHeight();
var textWidth = this.attrs.width === 'auto' ? this.getTextWidth() : this.attrs.width;
var p = this.attrs.padding;
var x = 0;
var y = 0;
var that = this;
switch (this.attrs.align) {
case 'center':
x = textWidth / -2 - p;
break;
case 'right':
x = -1 * textWidth - p;
break;
}
switch (this.attrs.verticalAlign) {
case 'middle':
y = textHeight / -2 - p;
break;
case 'bottom':
y = -1 * textHeight - p;
break;
}
// draw path
context.save();
context.beginPath();
context.rect(x, y, textWidth + p * 2, textHeight + p * 2);
context.closePath();
this.fill();
this.stroke();
context.restore();
var tx = p + x;
var ty = textHeight / 2 + p + y;
// clipping region for max width
context.save();
if(this.attrs.width !== 'auto') {
context.beginPath();
context.rect(x, y, textWidth + p, textHeight + p * 2);
context.closePath();
context.clip();
}
// draw text
this.fillText(this.attrs.text, tx, ty);
this.strokeText(this.attrs.text, tx, ty);
context.restore();
};
// call super constructor
Kinetic.Shape.apply(this, [config]);
};
/*
* Text methods
*/
Kinetic.Text.prototype = {
/**
* 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();
/**
* 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);
context.restore();
return {
width: metrics.width,
height: parseInt(this.attrs.fontSize, 10)
};
}
};
// extend Shape
Kinetic.GlobalObject.extend(Kinetic.Text, Kinetic.Shape);
// 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
* @param {String} fontFamily
*/
/**
* set font size
2012-06-14 17:19:51 +08:00
* @name setFontSize
* @methodOf Kinetic.Text.prototype
* @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
* @param {String} fontStyle
*/
/**
* set text fill color
* @name setTextFill
2012-06-14 17:19:51 +08:00
* @methodOf Kinetic.Text.prototype
* @param {String} textFill
*/
/**
* set text stroke color
2012-06-14 17:19:51 +08:00
* @name setFontStroke
* @methodOf Kinetic.Text.prototype
* @param {String} textStroke
*/
/**
* set text stroke width
2012-06-14 17:19:51 +08:00
* @name setTextStrokeWidth
* @methodOf Kinetic.Text.prototype
* @param {int} textStrokeWidth
*/
/**
* set padding
2012-06-14 17:19:51 +08:00
* @name setPadding
* @methodOf Kinetic.Text.prototype
* @param {int} padding
*/
/**
* set horizontal align of text
2012-06-14 17:19:51 +08:00
* @name setAlign
* @methodOf Kinetic.Text.prototype
* @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
* @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
* @param {String} text
*/
/**
* set width
2012-06-14 17:19:51 +08:00
* @name setWidth
* @methodOf Kinetic.Text.prototype
* @param {Number} width
*/
/**
* get font family
2012-06-14 17:19:51 +08:00
* @name getFontFamily
* @methodOf Kinetic.Text.prototype
*/
/**
* get font size
2012-06-14 17:19:51 +08:00
* @name getFontSize
* @methodOf Kinetic.Text.prototype
*/
/**
* get font style
2012-06-14 17:19:51 +08:00
* @name getFontStyle
* @methodOf Kinetic.Text.prototype
*/
/**
* get text fill color
2012-06-14 17:19:51 +08:00
* @name getTextFill
* @methodOf Kinetic.Text.prototype
*/
/**
* get text stroke color
2012-06-14 17:19:51 +08:00
* @name getTextStroke
* @methodOf Kinetic.Text.prototype
*/
/**
* get text stroke width
2012-06-14 17:19:51 +08:00
* @name getTextStrokeWidth
* @methodOf Kinetic.Text.prototype
*/
/**
* get padding
2012-06-14 17:19:51 +08:00
* @name getPadding
* @methodOf Kinetic.Text.prototype
*/
/**
* get horizontal align
2012-06-14 17:19:51 +08:00
* @name getAlign
* @methodOf Kinetic.Text.prototype
*/
/**
* get vertical align
2012-06-14 17:19:51 +08:00
* @name getVerticalAlign
* @methodOf Kinetic.Text.prototype
*/
/**
* get text
2012-06-14 17:19:51 +08:00
* @name getText
* @methodOf Kinetic.Text.prototype
*/
/**
2012-06-14 17:19:51 +08:00
* get total width including padding and borders
* @name getWidth
* @methodOf Kinetic.Text.prototype
*/