Skip to content

Commit

Permalink
possible fix for all our text problems
Browse files Browse the repository at this point in the history
  • Loading branch information
lavrton committed May 16, 2024
1 parent fdd0e64 commit 0ec3425
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 30 deletions.
30 changes: 25 additions & 5 deletions src/shapes/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@ export class Text extends Shape<TextConfig> {
shouldUnderline = textDecoration.indexOf('underline') !== -1,
shouldLineThrough = textDecoration.indexOf('line-through') !== -1,
n;

direction = direction === INHERIT ? context.direction : direction;

var translateY = 0;
var translateY = lineHeightPx / 2;
const baseline = this.textBaseline();
if (baseline === 'alphabetic') {
var metrics = this.measureSize('M'); // Use a sample character to get the ascent

translateY =
(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2 +
lineHeightPx / 2;
}

var lineTranslateX = 0;
var lineTranslateY = 0;
Expand All @@ -216,11 +223,10 @@ export class Text extends Shape<TextConfig> {

context.setAttr('font', this._getContextFont());

context.setAttr('textBaseline', MIDDLE);
context.setAttr('textBaseline', baseline);

context.setAttr('textAlign', LEFT);


// handle vertical alignment
if (verticalAlign === MIDDLE) {
alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2;
Expand Down Expand Up @@ -399,6 +405,18 @@ export class Text extends Shape<TextConfig> {
metrics = _context.measureText(text);
_context.restore();
return {
// copy all text metrics data:
actualBoundingBoxAscent: metrics.actualBoundingBoxAscent,
actualBoundingBoxDescent: metrics.actualBoundingBoxDescent,
actualBoundingBoxLeft: metrics.actualBoundingBoxLeft,
actualBoundingBoxRight: metrics.actualBoundingBoxRight,
alphabeticBaseline: metrics.alphabeticBaseline,
emHeightAscent: metrics.emHeightAscent,
emHeightDescent: metrics.emHeightDescent,
fontBoundingBoxAscent: metrics.fontBoundingBoxAscent,
fontBoundingBoxDescent: metrics.fontBoundingBoxDescent,
hangingBaseline: metrics.hangingBaseline,
ideographicBaseline: metrics.ideographicBaseline,
width: metrics.width,
height: fontSize,
};
Expand Down Expand Up @@ -637,6 +655,7 @@ export class Text extends Shape<TextConfig> {

direction: GetSet<string, this>;
fontFamily: GetSet<string, this>;
textBaseline: GetSet<string, this>;
fontSize: GetSet<number, this>;
fontStyle: GetSet<string, this>;
fontVariant: GetSet<string, this>;
Expand Down Expand Up @@ -703,7 +722,6 @@ Factory.overWriteSetter(Text, 'width', getNumberOrAutoValidator());

Factory.overWriteSetter(Text, 'height', getNumberOrAutoValidator());


/**
* get/set direction
* @name Konva.Text#direction
Expand Down Expand Up @@ -734,6 +752,8 @@ Factory.addGetterSetter(Text, 'direction', INHERIT);
*/
Factory.addGetterSetter(Text, 'fontFamily', 'Arial');

Factory.addGetterSetter(Text, 'textBaseline', MIDDLE);

/**
* get/set font size in pixels
* @name Konva.Text#fontSize
Expand Down
81 changes: 56 additions & 25 deletions test/sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,77 @@
<script type="module">
import Konva from '../src/index.ts';

var stageWidth = window.innerWidth;
var stageHeight = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: window.innerHeight,
height: window.innerHeight,
width: stageWidth,
height: stageHeight,
});

var layer = new Konva.Layer();
stage.add(layer);

const circle = new Konva.Circle({
x: 100,
y: 150,
radius: 50,
draggable: true,
fillLinearGradientStartPoint: { x: -50, y: -50 },
fillLinearGradientEndPoint: { x: 50, y: 50 },
fillLinearGradientColorStops: [0, 'red', 1, 'yellow'],
// Stage Background
var background = new Konva.Rect({
width: stageWidth,
height: stageHeight,
x: 0,
y: 0,
fill: 'red',
});
layer.add(background);

layer.add(circle);
// Text Item
var text = new Konva.Text({
text: 'testtest',
x: 50,
y: 25,
// width: 400,
// height: 200,
fontSize: 64,
// verticalAlign: 'middle',
align: 'center',
fontFamily: 'Times New Roman',
// textBaseline: 'alphabetic',
});
layer.add(text);

const tr = new Konva.Transformer({
nodes: [circle],
flipEnabled: false,
// Top line lining up perfect in MAC OSX CHROME, inline with top of text
var topLine = new Konva.Line({
points: [125, 103, 400, 103],
stroke: 'green',
strokeWidth: 1,
});
layer.add(tr);
layer.add(topLine);

const dot = new Konva.Circle({
x: 100,
y: 100,
radius: 2,
fill: 'blue',
// Bottom line lining up perfect in MAC OSX CHROME, inline with bottom of text
var bottomLine = new Konva.Line({
points: [125, 143, 400, 143],
stroke: 'green',
strokeWidth: 1,
});
layer.add(bottomLine);

layer.add(dot);
layer.draw();

circle.on('transform', () => {
dot.x(circle.x());
dot.y(circle.y() - circle.radius() * circle.scaleY() - 50);
});
// Get text bounding box
var textRect = text.getClientRect();

// Create overlay text
var overlayText = document.createElement('div');
overlayText.id = 'overlayText';
overlayText.style.position = 'absolute';
overlayText.style.left = textRect.x + 'px';
overlayText.style.top = textRect.y + 'px';
overlayText.style.width = textRect.width + 'px';
overlayText.style.height = textRect.height + 'px';
overlayText.style.lineHeight = textRect.height + 'px'; // Center vertically
overlayText.style.textAlign = 'center';
overlayText.style.fontSize = '64px';
overlayText.innerHTML = 'testtest';
document.getElementById('container').appendChild(overlayText);
</script>
</body>
</html>

0 comments on commit 0ec3425

Please sign in to comment.