admin管理员组文章数量:1397117
Please review the following JSFiddle: /
var stage = new PIXI.Container(),
renderer = PIXI.autoDetectRenderer(640, 480),
loader = PIXI.loader,
resources = loader.resources;
var menu = createMenu();
document.body.appendChild(renderer.view);
stage.addChild(menu);
//Render the stage
loop();
function loop(){
setInterval(() => {
renderer.render(stage);
}, 1000/10 );
}
function createMenu(){
var buttons = new PIXI.Container();
buttons.addChild(createButtonEndTurn());
return buttons;
}
function createButtonEndTurn(){
var buttonText = new PIXI.Text("End Turn",
{
fontFamily : 'Arial',
fontSize: 24,
fill : "white",
align : 'right'
});
var buttonEndTurn = new PIXI.Graphics();
buttonEndTurn.beginFill(0xFF2342);
buttonEndTurn.drawRect(400,300,150,100);
buttonEndTurn.endFill();
buttonEndTurn.interactive = true;
buttonEndTurn.on('mousedown', endTurnEvent);
buttonEndTurn.addChild(buttonText);
return buttonEndTurn;
}
function endTurnEvent(eventData){
eventData.target.children[0].setText("End Turn" + turn);
turn++;
console.log("Turn " + turn + " finished!");
}
I added the Text object to my button rectangle as a child. I also added the option align
for the text. I therefore expected the text to be centered within the buttons local - but it has been rendered to an arbitrary position in the canvas. Can you tell me what I'm not understand with the container system of PIXI.js here?
Please review the following JSFiddle: https://jsfiddle/r4yfozsw/1/
var stage = new PIXI.Container(),
renderer = PIXI.autoDetectRenderer(640, 480),
loader = PIXI.loader,
resources = loader.resources;
var menu = createMenu();
document.body.appendChild(renderer.view);
stage.addChild(menu);
//Render the stage
loop();
function loop(){
setInterval(() => {
renderer.render(stage);
}, 1000/10 );
}
function createMenu(){
var buttons = new PIXI.Container();
buttons.addChild(createButtonEndTurn());
return buttons;
}
function createButtonEndTurn(){
var buttonText = new PIXI.Text("End Turn",
{
fontFamily : 'Arial',
fontSize: 24,
fill : "white",
align : 'right'
});
var buttonEndTurn = new PIXI.Graphics();
buttonEndTurn.beginFill(0xFF2342);
buttonEndTurn.drawRect(400,300,150,100);
buttonEndTurn.endFill();
buttonEndTurn.interactive = true;
buttonEndTurn.on('mousedown', endTurnEvent);
buttonEndTurn.addChild(buttonText);
return buttonEndTurn;
}
function endTurnEvent(eventData){
eventData.target.children[0].setText("End Turn" + turn);
turn++;
console.log("Turn " + turn + " finished!");
}
I added the Text object to my button rectangle as a child. I also added the option align
for the text. I therefore expected the text to be centered within the buttons local - but it has been rendered to an arbitrary position in the canvas. Can you tell me what I'm not understand with the container system of PIXI.js here?
1 Answer
Reset to default 7The reason your text doesn't appear inside the retangle is because of the way you've drawn your rectangle. Think of the Graphics object as a container. Within that container, you've drawn a rectangle at position {x:400, y:300}
but the container itself is still sitting at position {x:0, y:0}
. So when you add your text object, it also sits inside the container at position {x:0, y:0}
. If you want your text to move within the rectangle, you need to move the position of the container.
buttonEndTurn.beginFill(0xFF2342);
buttonEndTurn.drawRect(0,0,150,100);
buttonEndTurn.endFill();
buttonEndTurn.x = 400;
buttonEndTurn.y = 300;
Now as for the alignment, you will have to manually set this up. Setting align : 'center'
won't help here because that is only really useful for multi-line text. It doesn't align text within it's parent container; it just makes it so that each line in a multi-line text object will be aligned to the middle.
The easiest way to make the object center aligned within the parent container would be to set the anchor point: buttonText.anchor.x = 0.5;
Now the text will be anchored from it's mid point, but as you can see, it is not positioned correctly within the parent rectangle. So just move it over: buttonText.x = buttonEndTurn.width/2;
Here's the plete function:
function createButtonEndTurn(){
var buttonEndTurn = new PIXI.Graphics();
buttonEndTurn.x = 400;
buttonEndTurn.y = 300;
buttonEndTurn.beginFill(0xFF2342);
buttonEndTurn.drawRect(0,0,150,100);
buttonEndTurn.endFill();
buttonEndTurn.interactive = true;
buttonEndTurn.on('mousedown', endTurnEvent);
var buttonText = new PIXI.Text("End Turn",
{
fontFamily : 'Arial',
fontSize: 24,
fill : "white",
align : 'center'
});
buttonText.anchor.x = 0.5;
buttonText.x = buttonEndTurn.width/2;
buttonEndTurn.addChild(buttonText);
return buttonEndTurn;
}
本文标签: javascripthow to center a text object within it39s containers local in PIXIjsStack Overflow
版权声明:本文标题:javascript - how to center a text object within it's containers local in PIXI.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744147419a2592903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论