pixi.js
    Preparing search index...

    Class RenderLayer

    The RenderLayer API provides a way to control the rendering order of objects independently of their logical parent-child relationships in the scene graph. This allows developers to decouple how objects are transformed (via their logical parent) from how they are rendered on the screen.

    • RenderLayers define where in the render stack objects are drawn, but they do not affect an object's transformations (e.g., position, scale, rotation) or logical hierarchy.
    • RenderLayers can be added anywhere in the scene graph.
    • Objects still have a logical parent for transformations via addChild.
    • Assigning an object to a layer does not reparent it.
    • Developers assign objects to layers using renderLayer.add and remove them using renderLayer.remove.

    A RenderLayer is a lightweight object responsible for controlling render order. It has no children or transformations of its own but can be inserted anywhere in the scene graph to define its render position.

    const layer = new RenderLayer();
    app.stage.addChild(layer); // Insert the layer into the scene graph

    Use renderLayer.add to assign an object to a layer. This overrides the object's default render order defined by its logical parent.

    const rect = new Graphics();
    container.addChild(rect); // Add to logical parent
    layer.attach(rect); // Control render order via the layer

    To stop an object from being rendered in the layer, use remove.

    layer.remove(rect); // Stop rendering rect via the layer
    

    When an object is removed from its logical parent (removeChild), it is automatically removed from the layer.

    If an object is re-added to a logical parent, it does not automatically reassign itself to the layer. Developers must explicitly reassign it.

    container.addChild(rect);    // Logical parent
    layer.attach(rect); // Explicitly reassign to the layer

    A layer's position in the scene graph determines its render priority relative to other layers and objects. Layers can be inserted anywhere in the scene graph.

    const backgroundLayer = new RenderLayer();
    const uiLayer = new RenderLayer();

    app.stage.addChild(backgroundLayer);
    app.stage.addChild(world);
    app.stage.addChild(uiLayer);

    This is a new API and therefore considered experimental at this stage. While the core is pretty robust, there are still a few tricky issues we need to tackle. However, even with the known issues below, we believe this API is incredibly useful!

    Known issues:

    • Interaction may not work as expected since hit testing does not account for the visual render order created by layers. For example, if an object is visually moved to the front via a layer, hit testing will still use its original position.
    • RenderLayers and their children must all belong to the same renderGroup to work correctly
    Index

    Constructors

    Properties

    destroyed: boolean = false

    Whether this object has been destroyed. If true, the object should no longer be used. After an object is destroyed, all of its functionality is disabled and references are removed.

    // Cleanup with destroy
    sprite.destroy();
    console.log(sprite.destroyed); // true
    false
    

    Container#destroy For destroying objects

    parent: Container = null

    The display object container that contains this display object. This represents the parent-child relationship in the display tree.

    // Basic parent access
    const parent = sprite.parent;

    // Walk up the tree
    let current = sprite;
    while (current.parent) {
    console.log('Level up:', current.parent.constructor.name);
    current = current.parent;
    }
    parentRenderLayer: IRenderLayer

    The RenderLayer this container belongs to, if any. If it belongs to a RenderLayer, it will be rendered from the RenderLayer's position in the scene.

    renderLayerChildren: Container<ContainerChild>[] = []

    The list of objects that this layer is responsible for rendering. Objects in this list maintain their original parent in the scene graph but are rendered as part of this layer.

    const layer = new RenderLayer();
    const sprite = new Sprite(texture);

    // Add sprite to scene graph for transforms
    container.addChild(sprite);

    // Add to layer for render order control
    layer.attach(sprite);
    console.log(layer.renderLayerChildren.length); // 1

    // Access objects in the layer
    layer.renderLayerChildren.forEach(child => {
    console.log('Layer child:', child);
    });

    // Check if object is in layer
    const isInLayer = layer.renderLayerChildren.includes(sprite);

    // Clear all objects from layer
    layer.detachAll();
    console.log(layer.renderLayerChildren.length); // 0
    sortableChildren: boolean

    If set to true, the container will sort its children by zIndex value when the next render is called, or manually if sortChildren() is called.

    This actually changes the order of elements in the array of children, so it will affect the rendering order.

    Note

    Also be aware of that this may not work nicely with the addChildAt() function, as the zIndex sorting may cause the child to automatically sorted to another position.

    container.sortableChildren = true;
    
    false
    
    sortFunction: (a: Container, b: Container) => number

    Function used to sort layer children if sortableChildren is true

    Methods

    • Adds one or more Containers to this render layer. The Containers will be rendered as part of this layer while maintaining their original parent in the scene graph.

      If the Container already belongs to a layer, it will be removed from the old layer before being added to this one.

      Type Parameters

      Parameters

      • ...children: U

        The Container(s) to add to this layer. Can be any Container or array of Containers.

      Returns U[0]

      The first child that was added, for method chaining

      const layer = new RenderLayer();
      const container = new Container();
      const sprite1 = new Sprite(texture1);
      const sprite2 = new Sprite(texture2);

      // Add sprites to scene graph for transforms
      container.addChild(sprite1, sprite2);

      // Add sprites to layer for render order control
      layer.attach(sprite1, sprite2);

      // Add single sprite with type checking
      const typedSprite = layer.attach<Sprite>(new Sprite(texture3));
      typedSprite.tint = 'red';

      // Automatically removes from previous layer if needed
      const otherLayer = new RenderLayer();
      otherLayer.attach(sprite1); // Removes from previous layer
    • Removes one or more Containers from this render layer. The Containers will maintain their original parent in the scene graph but will no longer be rendered as part of this layer.

      Type Parameters

      Parameters

      • ...children: U

        The Container(s) to remove from this layer

      Returns U[0]

      The first child that was removed, for method chaining

      const layer = new RenderLayer();
      const container = new Container();
      const sprite1 = new Sprite(texture1);
      const sprite2 = new Sprite(texture2);

      // Add sprites to scene graph and layer
      container.addChild(sprite1, sprite2);
      layer.attach(sprite1, sprite2);

      // Remove single sprite from layer
      layer.detach(sprite1);
      // sprite1 is still child of container but not rendered in layer

      // Remove multiple sprites at once
      const otherLayer = new RenderLayer();
      otherLayer.attach(sprite3, sprite4);
      otherLayer.detach(sprite3, sprite4);

      // Type-safe detachment
      const typedSprite = layer.detach<Sprite>(spriteInLayer);
      typedSprite.texture = newTexture; // TypeScript knows this is a Sprite
    • Removes all objects from this render layer. Objects will maintain their original parent in the scene graph but will no longer be rendered as part of this layer.

      Returns void

      The RenderLayer instance for method chaining

      const layer = new RenderLayer();
      const container = new Container();

      // Add multiple sprites to scene graph and layer
      const sprites = [
      new Sprite(texture1),
      new Sprite(texture2),
      new Sprite(texture3)
      ];

      container.addChild(...sprites); // Add to scene graph
      layer.attach(...sprites); // Add to render layer

      // Later, remove all sprites from layer at once
      layer.detachAll();
      console.log(layer.renderLayerChildren.length); // 0
      console.log(container.children.length); // 3 (still in scene graph)
    • Advanced

      Computes an approximate global bounding box for the container and its children. This method is optimized for speed by using axis-aligned bounding boxes (AABBs), and uses the last render results from when it updated the transforms. This function does not update them. which may result in slightly larger bounds but never smaller than the actual bounds.

      for accurate (but less performant) results use container.getGlobalBounds

      Parameters

      • OptionalfactorRenderLayers: boolean

        A flag indicating whether to consider render layers in the calculation.

      • Optionalbounds: Bounds

        The output bounds object to store the result. If not provided, a new one is created.

      Returns Bounds

      The computed bounds.

    • Sort the layer's children using the defined sort function. This method allows manual sorting of layer children and is automatically called during rendering if sortableChildren is true.

      Returns void

      The RenderLayer instance for method chaining

      const layer = new RenderLayer();

      // Add multiple sprites at different depths
      const sprite1 = new Sprite(texture);
      const sprite2 = new Sprite(texture);
      const sprite3 = new Sprite(texture);

      sprite1.zIndex = 3;
      sprite2.zIndex = 1;
      sprite3.zIndex = 2;

      layer.attach(sprite1, sprite2, sprite3);

      // Manual sorting with default zIndex sort
      layer.sortRenderLayerChildren();
      // Order is now: sprite2 (1), sprite3 (2), sprite1 (3)

      // Custom sort by y position
      layer.sortFunction = (a, b) => a.y - b.y;
      layer.sortRenderLayerChildren();

      // Automatic sorting
      layer.sortableChildren = true; // Will sort each render