How to use Layers and Groups
mainThe core concept of @pixi/layers is to decouple the scene tree hierarchy from the rendering order.
- Create a Layer: A
LayerextendsPIXI.Containerand acts as a rendering bucket. - Assign Objects to Layers: Instead of using standard parent-child relationships for rendering order, assign a
parentLayerto aDisplayObjectorContainer. - Use Groups for Cross-Stage Logic: A
Groupdefines a logical collection of objects. ALayercan be bound to aGroup. This allows objects to maintain their logical grouping even if they move between differentStageinstances.
Critical Requirement: You must create a Stage and set it as your scene's root for the library to function correctly.
import { Layer, Group } from '@pixi/layers';
// Basic Layer usage
const layer = new Layer();
const bunnySprite = PIXI.Sprite.from('bunny.png');
// Assign sprite to render inside the layer instead of its direct parent
bunnySprite.parentLayer = layer;
// Using Groups for persistent logical grouping
const lightGroup = new Group();
bunnySprite.parentGroup = lightGroup;
const lightLayer = new Layer(lightGroup); // Only one layer per stage can be bound to the same group