Skip to content

Why isn't Text Centered Vertically in KonvaJS? #1946

Open
@nijatmursali

Description

@nijatmursali

We are working on an application where we use Konva to draw in canvas, but I have found an issue with text is not centered correctly inside another element even we set correct width and height for the parent.

You can check on this fiddle.

<link
  href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap"
  rel="stylesheet"
/>
<script src="https://cdn.rawgit.com/konvajs/konva/2.3.0/konva.min.js"></script>

<div id="container"></div>
const stage = new Konva.Stage({
  container: "container",
  width: window.innerWidth,
  height: window.innerHeight,
})

const layer = new Konva.Layer()
stage.add(layer)

const rect = new Konva.Rect({
  width: 140,
  height: 140,
  stroke: "green",
})
layer.add(rect)

const text = new Konva.Text({
  text: "22",
  fontFamily: "Roboto",
  fontSize: 61,
  width: rect.width(),
  height: rect.height(),
  align: "center",
  verticalAlign: "middle",
})

layer.add(text)

const circle = new Konva.Circle({
  x: rect.width() / 2,
  y: rect.height() / 2,
  radius: 5,
  fill: "red",
})

layer.add(circle)

layer.draw()

I have also tried that on Figma to see if it is correctly centered, and this is the result.

Image

Circle is centered correctly, but Text is not. Green text from screenshot is what it should be like.

This issue happens with platforms that is not IOS (strange). Following screenshots are from IOS and Android:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions