在容器中居中放置文本(EaselJS)

瑙尔

我是EaselJS的新手,我正在尝试创建一个带有居中文本的彩色容器。这是我的代码:

var width = 100, height = 100;
canvas.width = width + 10;
canvas.height = height + 10;
var container = new c.Container();
container.x = 10;
container.y = 10;
container.setBounds(0, 0, width, height);

var rect = new c.Shape();
rect.graphics.beginFill('#6e7e8e').drawRect(0, 0, width, height);
container.addChild(rect);

var text = new c.Text();
text.set({
    text: 'hello',
    textAlign: 'center'
});
container.addChild(text);
stage.addChild(container);
stage.update();

由于某些原因,文本不在容器中居中,但一半的文本不在容器中。我的代码有什么问题?

D先生

您的文本在添加的x位置上水平居中(在您的情况下,x = 0),这就是为什么它超出容器一半的原因。如果要在容器中居中放置文本,则必须自己定义位置:

text.set({
    text: 'hello',
});
var b = text.getBounds();
text.x = (width/2) - (b.width/2); 
text.y = (height/2) - (b.height/2);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章