繁体   English   中英

计算圆心以使文本在中间对齐

[英]Calculate the center of a circle to align text in the middle

我有一个绕画布移动的圆圈。

当用户在圆上单击鼠标时,半径从20扩大到100,放开时半径停止增长。

我想在圆心中显示圆的半径,并随着圆的增长对其进行更新。

我的圈子和文本代码如下。 我是否需要使圆的高度和宽度以及使文本正确居中的文本的高度和宽度,并使圆仍然正确地生长?

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

var radiusText = new Kinetic.Text({
    x : circle.getX(),
    y : circle.getY(),
    text : '10',
    fontSize : 10,
    height : (circle.getAttr('radius') * 2) / 2,
    width : (circle.getAttr('radius') * 2) /2,
    fontFamily : 'Verdana',
    fill : '#000',
    align : 'center'
});

http://jsfiddle.net/ZQEer/2/

圆的X,Y坐标为中心。 文本的X,Y坐标在左上角。 您无需在radiusText中设置宽度和高度。 您可以使用偏移量:

var radiusText = new Kinetic.Text({
    x : circle.getX(),
    y : circle.getY(),
    text : '10',
    fontSize : 10,
    fontFamily : 'Verdana',
    fill : '#000',
    align : 'center'
});
radiusText.setOffset({
    x : radiusText.getWidth()/2,
    y : radiusText.getHeight()/2
});

http://jsfiddle.net/dhH9z/3/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM