gpt4 book ai didi

javascript - 容器中的中心文本 (EaselJS)

转载 作者:数据小太阳 更新时间:2023-10-29 05:05:57 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com