Jump to content

Better way to bring child to front?


Stan.
 Share

Recommended Posts

Currently working on a project where I often have to bring an object to the front in a container. Currently I achieve this with the following code:

bringContainerToFront(container) {
	const parent = container.parent
    parent.removeChild(container)
    parent.addChild(container)
}

Is the a more succinct way that just removing the child and adding it back again?

Any advice would be greatly appreciated.

Link to comment
Share on other sites

About adding/removing performance: there's an array inside. Guess the cost. https://github.com/pixijs/pixijs/blob/9c1861b83f2d2bdb75235ada648d76d465ba0071/packages/display/src/Container.ts#L144-L181

There is different way: use "@pixi/layers" , https://pixijs.io/examples/#/plugin-layers/bringToTop.js . It goes through all the tree every frame and marks which objects have to be rendered in which containers, temporarily, without changing actual parent.

Create a top layer, make sure its always on top of your stage (the last child, put all other objects in other container maybe). Do "sprite.parentLayer = topLayer" every time you want to bring a sprite to front. This operation wont actually move anything anywhere.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...