vertical align for the text. close #440

This commit is contained in:
Anton Lavrenov 2018-08-30 15:18:09 +03:00
parent 83385fa522
commit 98efbca18f
5 changed files with 110 additions and 5 deletions

View File

@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
* new methods `path.getLength()` and `path.getPointAtLength(val)`
* `verticalAlign` for `Konva.Text`
## [2.2.2][2018-08-21]

View File

@ -14952,6 +14952,8 @@
LEFT = 'left',
TEXT = 'text',
TEXT_UPPER = 'Text',
TOP = 'top',
BOTTOM = 'bottom',
MIDDLE = 'middle',
NORMAL = 'normal',
PX_SPACE = 'px ',
@ -14968,6 +14970,7 @@
'fontVariant',
'padding',
'align',
'verticalAlign',
'lineHeight',
'text',
'width',
@ -14999,6 +15002,7 @@
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
* @param {String} config.text
* @param {String} [config.align] can be left, center, or right
* @param {String} [config.verticalAlign] can be top, middle or bottom
* @param {Number} [config.padding]
* @param {Number} [config.lineHeight] default is 1
* @param {String} [config.wrap] can be word, char, or none. Default is word
@ -15135,6 +15139,8 @@
lineHeightPx = this.getLineHeight() * textHeight,
textArr = this.textArr,
textArrLen = textArr.length,
verticalAlign = this.getVerticalAlign(),
alignY = 0,
align = this.getAlign(),
totalWidth = this.getWidth(),
letterSpacing = this.getLetterSpacing(),
@ -15147,11 +15153,20 @@
context.setAttr('textBaseline', MIDDLE);
context.setAttr('textAlign', LEFT);
// handle vertical alignment
if (verticalAlign === MIDDLE) {
alignY =
(this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
} else if (verticalAlign === BOTTOM) {
alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
}
if (padding) {
context.translate(padding, 0);
context.translate(0, padding + lineHeightPx / 2);
context.translate(0, alignY + padding + lineHeightPx / 2);
} else {
context.translate(0, lineHeightPx / 2);
context.translate(0, alignY + lineHeightPx / 2);
}
// draw text lines
@ -15609,6 +15624,23 @@
* text.align('right');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'verticalAlign', TOP);
/**
* get/set vertical align of text. Can be 'top', 'middle', 'bottom'.
* @name verticalAlign
* @method
* @memberof Konva.Text.prototype
* @param {String} verticalAlign
* @returns {String}
* @example
* // get text vertical align
* var verticalAlign = text.verticalAlign();
*
* // center text
* text.verticalAlign('center');
*/
Konva.Factory.addGetterSetter(
Konva.Text,
'lineHeight',

2
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -14,6 +14,8 @@
LEFT = 'left',
TEXT = 'text',
TEXT_UPPER = 'Text',
TOP = 'top',
BOTTOM = 'bottom',
MIDDLE = 'middle',
NORMAL = 'normal',
PX_SPACE = 'px ',
@ -30,6 +32,7 @@
'fontVariant',
'padding',
'align',
'verticalAlign',
'lineHeight',
'text',
'width',
@ -61,6 +64,7 @@
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
* @param {String} config.text
* @param {String} [config.align] can be left, center, or right
* @param {String} [config.verticalAlign] can be top, middle or bottom
* @param {Number} [config.padding]
* @param {Number} [config.lineHeight] default is 1
* @param {String} [config.wrap] can be word, char, or none. Default is word
@ -128,6 +132,8 @@
lineHeightPx = this.getLineHeight() * textHeight,
textArr = this.textArr,
textArrLen = textArr.length,
verticalAlign = this.getVerticalAlign(),
alignY = 0,
align = this.getAlign(),
totalWidth = this.getWidth(),
letterSpacing = this.getLetterSpacing(),
@ -140,11 +146,20 @@
context.setAttr('textBaseline', MIDDLE);
context.setAttr('textAlign', LEFT);
// handle vertical alignment
if (verticalAlign === MIDDLE) {
alignY =
(this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
} else if (verticalAlign === BOTTOM) {
alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2;
}
if (padding) {
context.translate(padding, 0);
context.translate(0, padding + lineHeightPx / 2);
context.translate(0, alignY + padding + lineHeightPx / 2);
} else {
context.translate(0, lineHeightPx / 2);
context.translate(0, alignY + lineHeightPx / 2);
}
// draw text lines
@ -602,6 +617,23 @@
* text.align('right');
*/
Konva.Factory.addGetterSetter(Konva.Text, 'verticalAlign', TOP);
/**
* get/set vertical align of text. Can be 'top', 'middle', 'bottom'.
* @name verticalAlign
* @method
* @memberof Konva.Text.prototype
* @param {String} verticalAlign
* @returns {String}
* @example
* // get text vertical align
* var verticalAlign = text.verticalAlign();
*
* // center text
* text.verticalAlign('center');
*/
Konva.Factory.addGetterSetter(
Konva.Text,
'lineHeight',

View File

@ -621,6 +621,46 @@ suite('Text', function() {
assert(text.getWidth() > width, 'width should have increased');
assert(text.getHeight() > height, 'height should have increased');
});
test('text vertical align', function() {
var stage = addStage();
var layer = new Konva.Layer();
var rect = new Konva.Rect({
x: 10,
y: 10,
width: 200,
height: 100,
stroke: 'black'
});
layer.add(rect);
var text = new Konva.Text({
x: rect.x(),
y: rect.y(),
width: rect.width(),
height: rect.height(),
text: 'Some awesome text',
fontSize: 16,
fill: '#555',
align: 'center',
padding: 10,
draggable: true
});
assert.equal(text.verticalAlign(), 'top');
text.verticalAlign('middle');
layer.add(text);
stage.add(layer);
var trace =
'clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);beginPath();rect(0,0,200,100);closePath();lineWidth=2;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,10,10);font=normal normal 16px Arial;textBaseline=middle;textAlign=left;translate(10,0);translate(0,50);save();translate(17.523,0);fillStyle=#555;fillText(Some awesome text,0,0);restore();restore();';
assert.equal(layer.getContext().getTrace(), trace);
});
test('get text width', function() {
var stage = addStage();
var layer = new Konva.Layer();