mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
vertical align for the text. close #440
This commit is contained in:
parent
83385fa522
commit
98efbca18f
@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
### Added
|
### Added
|
||||||
|
|
||||||
* new methods `path.getLength()` and `path.getPointAtLength(val)`
|
* new methods `path.getLength()` and `path.getPointAtLength(val)`
|
||||||
|
* `verticalAlign` for `Konva.Text`
|
||||||
|
|
||||||
## [2.2.2][2018-08-21]
|
## [2.2.2][2018-08-21]
|
||||||
|
|
||||||
|
36
konva.js
36
konva.js
@ -14952,6 +14952,8 @@
|
|||||||
LEFT = 'left',
|
LEFT = 'left',
|
||||||
TEXT = 'text',
|
TEXT = 'text',
|
||||||
TEXT_UPPER = 'Text',
|
TEXT_UPPER = 'Text',
|
||||||
|
TOP = 'top',
|
||||||
|
BOTTOM = 'bottom',
|
||||||
MIDDLE = 'middle',
|
MIDDLE = 'middle',
|
||||||
NORMAL = 'normal',
|
NORMAL = 'normal',
|
||||||
PX_SPACE = 'px ',
|
PX_SPACE = 'px ',
|
||||||
@ -14968,6 +14970,7 @@
|
|||||||
'fontVariant',
|
'fontVariant',
|
||||||
'padding',
|
'padding',
|
||||||
'align',
|
'align',
|
||||||
|
'verticalAlign',
|
||||||
'lineHeight',
|
'lineHeight',
|
||||||
'text',
|
'text',
|
||||||
'width',
|
'width',
|
||||||
@ -14999,6 +15002,7 @@
|
|||||||
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
|
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
|
||||||
* @param {String} config.text
|
* @param {String} config.text
|
||||||
* @param {String} [config.align] can be left, center, or right
|
* @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.padding]
|
||||||
* @param {Number} [config.lineHeight] default is 1
|
* @param {Number} [config.lineHeight] default is 1
|
||||||
* @param {String} [config.wrap] can be word, char, or none. Default is word
|
* @param {String} [config.wrap] can be word, char, or none. Default is word
|
||||||
@ -15135,6 +15139,8 @@
|
|||||||
lineHeightPx = this.getLineHeight() * textHeight,
|
lineHeightPx = this.getLineHeight() * textHeight,
|
||||||
textArr = this.textArr,
|
textArr = this.textArr,
|
||||||
textArrLen = textArr.length,
|
textArrLen = textArr.length,
|
||||||
|
verticalAlign = this.getVerticalAlign(),
|
||||||
|
alignY = 0,
|
||||||
align = this.getAlign(),
|
align = this.getAlign(),
|
||||||
totalWidth = this.getWidth(),
|
totalWidth = this.getWidth(),
|
||||||
letterSpacing = this.getLetterSpacing(),
|
letterSpacing = this.getLetterSpacing(),
|
||||||
@ -15147,11 +15153,20 @@
|
|||||||
|
|
||||||
context.setAttr('textBaseline', MIDDLE);
|
context.setAttr('textBaseline', MIDDLE);
|
||||||
context.setAttr('textAlign', LEFT);
|
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) {
|
if (padding) {
|
||||||
context.translate(padding, 0);
|
context.translate(padding, 0);
|
||||||
context.translate(0, padding + lineHeightPx / 2);
|
context.translate(0, alignY + padding + lineHeightPx / 2);
|
||||||
} else {
|
} else {
|
||||||
context.translate(0, lineHeightPx / 2);
|
context.translate(0, alignY + lineHeightPx / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw text lines
|
// draw text lines
|
||||||
@ -15609,6 +15624,23 @@
|
|||||||
* text.align('right');
|
* 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.Factory.addGetterSetter(
|
||||||
Konva.Text,
|
Konva.Text,
|
||||||
'lineHeight',
|
'lineHeight',
|
||||||
|
2
konva.min.js
vendored
2
konva.min.js
vendored
File diff suppressed because one or more lines are too long
@ -14,6 +14,8 @@
|
|||||||
LEFT = 'left',
|
LEFT = 'left',
|
||||||
TEXT = 'text',
|
TEXT = 'text',
|
||||||
TEXT_UPPER = 'Text',
|
TEXT_UPPER = 'Text',
|
||||||
|
TOP = 'top',
|
||||||
|
BOTTOM = 'bottom',
|
||||||
MIDDLE = 'middle',
|
MIDDLE = 'middle',
|
||||||
NORMAL = 'normal',
|
NORMAL = 'normal',
|
||||||
PX_SPACE = 'px ',
|
PX_SPACE = 'px ',
|
||||||
@ -30,6 +32,7 @@
|
|||||||
'fontVariant',
|
'fontVariant',
|
||||||
'padding',
|
'padding',
|
||||||
'align',
|
'align',
|
||||||
|
'verticalAlign',
|
||||||
'lineHeight',
|
'lineHeight',
|
||||||
'text',
|
'text',
|
||||||
'width',
|
'width',
|
||||||
@ -61,6 +64,7 @@
|
|||||||
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
|
* @param {String} [config.fontVariant] can be normal or small-caps. Default is normal
|
||||||
* @param {String} config.text
|
* @param {String} config.text
|
||||||
* @param {String} [config.align] can be left, center, or right
|
* @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.padding]
|
||||||
* @param {Number} [config.lineHeight] default is 1
|
* @param {Number} [config.lineHeight] default is 1
|
||||||
* @param {String} [config.wrap] can be word, char, or none. Default is word
|
* @param {String} [config.wrap] can be word, char, or none. Default is word
|
||||||
@ -128,6 +132,8 @@
|
|||||||
lineHeightPx = this.getLineHeight() * textHeight,
|
lineHeightPx = this.getLineHeight() * textHeight,
|
||||||
textArr = this.textArr,
|
textArr = this.textArr,
|
||||||
textArrLen = textArr.length,
|
textArrLen = textArr.length,
|
||||||
|
verticalAlign = this.getVerticalAlign(),
|
||||||
|
alignY = 0,
|
||||||
align = this.getAlign(),
|
align = this.getAlign(),
|
||||||
totalWidth = this.getWidth(),
|
totalWidth = this.getWidth(),
|
||||||
letterSpacing = this.getLetterSpacing(),
|
letterSpacing = this.getLetterSpacing(),
|
||||||
@ -140,11 +146,20 @@
|
|||||||
|
|
||||||
context.setAttr('textBaseline', MIDDLE);
|
context.setAttr('textBaseline', MIDDLE);
|
||||||
context.setAttr('textAlign', LEFT);
|
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) {
|
if (padding) {
|
||||||
context.translate(padding, 0);
|
context.translate(padding, 0);
|
||||||
context.translate(0, padding + lineHeightPx / 2);
|
context.translate(0, alignY + padding + lineHeightPx / 2);
|
||||||
} else {
|
} else {
|
||||||
context.translate(0, lineHeightPx / 2);
|
context.translate(0, alignY + lineHeightPx / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw text lines
|
// draw text lines
|
||||||
@ -602,6 +617,23 @@
|
|||||||
* text.align('right');
|
* 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.Factory.addGetterSetter(
|
||||||
Konva.Text,
|
Konva.Text,
|
||||||
'lineHeight',
|
'lineHeight',
|
||||||
|
@ -621,6 +621,46 @@ suite('Text', function() {
|
|||||||
assert(text.getWidth() > width, 'width should have increased');
|
assert(text.getWidth() > width, 'width should have increased');
|
||||||
assert(text.getHeight() > height, 'height 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() {
|
test('get text width', function() {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
var layer = new Konva.Layer();
|
var layer = new Konva.Layer();
|
||||||
|
Loading…
Reference in New Issue
Block a user