voidmen

PIXI.Text with solid color background

Recommended Posts

hey guys,

I'm wondering if there is an easy and efficient way to add background to the PIXI.Text object. For now, my plan is to add a scaled solid color image as background and put them in the right position. Not sure if this is the recommended way.

 

Thanks

Share this post


Link to post
Share on other sites
// text
const textSprite = new PIXI.Text(`blabla`, {fill:"#ff8000"});
const txtBG = new PIXI.Sprite(PIXI.Texture.WHITE);
txtBG.width = textSprite.width,txtBG.height = textSprite.height;
// cage text
const cage = new PIXI.Container();
cage.addChild(txtBG,textSprite);
// add reference for easy debug
cage.name = "textSprite";
cage.textSprite = textSprite;
cage.txtBG = txtBG;

Share this post


Link to post
Share on other sites
17 hours ago, jonforum said:
// text
const textSprite = new PIXI.Text(`blabla`, {fill:"#ff8000"});
const txtBG = new PIXI.Sprite(PIXI.Texture.WHITE);
txtBG.width = textSprite.width,txtBG.height = textSprite.height;
// cage text
const cage = new PIXI.Container();
cage.addChild(txtBG,textSprite);
// add reference for easy debug
cage.name = "textSprite";
cage.textSprite = textSprite;
cage.txtBG = txtBG;

Thanks a lot man!

Share this post


Link to post
Share on other sites
On 4/16/2018 at 4:04 PM, jonforum said:
// text
const textSprite = new PIXI.Text(`blabla`, {fill:"#ff8000"});
const txtBG = new PIXI.Sprite(PIXI.Texture.WHITE);
txtBG.width = textSprite.width,txtBG.height = textSprite.height;
// cage text
const cage = new PIXI.Container();
cage.addChild(txtBG,textSprite);
// add reference for easy debug
cage.name = "textSprite";
cage.textSprite = textSprite;
cage.txtBG = txtBG;

Which advice would you offer if I were to need textures of a different color? 

Right now I am generating a texture for each color.

Edit: Aha that's what the tints are for. For anyone interested, this was my code for doing so before:

	generateColoredTexture: function(hexColor) {
		let painter = new PIXI.Graphics(true);
		painter.beginFill(hexColor);
		painter.drawRect(0, 0, 2, 2); //(0, 0, 1, 1); does not work
		painter.endFill();
		let texture = painter.generateCanvasTexture();
		painter.destroy();
		return texture;
	},

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.