我是 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();
出于某种原因,文本没有在容器中居中,但有一半文本在容器之外。我的代码有什么问题?
最佳答案
您的文本水平居中于它添加的 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);
关于javascript - 容器中的中心文本 (EaselJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863135/