[英]Calculate the center of a circle to align text in the middle
I have a circle that moves around a canvas. 我有一个绕画布移动的圆圈。
When the user mouses down on the circle, the radius expands from 20 to 100 and when they release it it stops growing. 当用户在圆上单击鼠标时,半径从20扩大到100,放开时半径停止增长。
I want to display the radius of the circle in the center of it and have it update as it grows. 我想在圆心中显示圆的半径,并随着圆的增长对其进行更新。
My circle and text code is below. 我的圈子和文本代码如下。 Do I need a height and width for the circle and text for the text to center properly, and still have the circle grow properly? 我是否需要使圆的高度和宽度以及使文本正确居中的文本的高度和宽度,并使圆仍然正确地生长?
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'
});
X, Y coords of circle is center. 圆的X,Y坐标为中心。 X,Y coords of text is top left corner. 文本的X,Y坐标在左上角。 You don't need to set width and height in radiusText. 您无需在radiusText中设置宽度和高度。 You can use offset: 您可以使用偏移量:
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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.