textcanvaskineticjsgeometry

Put text Inside Kinetic.Circle


I'm trying to put text inside a circle from Kinetic.Circle but i can't find the right answer. I have the following code:

var circle = new Kinetic.Circle({
    x: 30,
    y: stage.getHeight() / 2,
    radius: 20,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 2, 
});

var text = new Kinetic.Text({
    text: 'A',
    fontSize: 10,
    fontFamily: 'Calibri',
    width: 5,
    fill: '#000'
});

layer.add(circle);
layer.add(texto);
stage.add(layer);

Any ideas how to accomplish this please?


Solution

  • You can position the text like this so that you dont have to provide the x & y coordinates.

    text.setX( circle.getX() - text.getWidth()/2 );
    text.setY( circle.getY() - text.getHeight()/2 );