PIXI.Container
class Container extends PIXI.DisplayObject
Container is a general-purpose display object that holds children. It also adds built-in support for advanced rendering features like masking and filtering.
It is the base class of all display objects that act as a container for other objects, including Graphics and Sprite.
import { BlurFilter } from '@pixi/filter-blur';
import { Container } from '@pixi/display';
import { Graphics } from '@pixi/graphics';
import { Sprite } from '@pixi/sprite';
let container = new Container();
let sprite = Sprite.from("https://s3-us-west-2.amazonaws.com/s.cdpn.io/693612/IaUrttj.png");
sprite.width = 512;
sprite.height = 512;
// Adds a sprite as a child to this container. As a result, the sprite will be rendered whenever the container
// is rendered.
container.addChild(sprite);
// Blurs whatever is rendered by the container
container.filters = [new BlurFilter()];
// Only the contents within a circle at the center should be rendered onto the screen.
container.mask = new Graphics()
.beginFill(0xffffff)
.drawCircle(sprite.width / 2, sprite.height / 2, Math.min(sprite.width, sprite.height) / 2)
.endFill();
Summary
Properties from Container
T[] |
|
number |
The height of the Container, setting this will actually modify the scale to achieve the value set. |
boolean |
|
boolean |
|
boolean |
Should children be sorted by zIndex at the next updateTransform call. |
number |
The width of the Container, setting this will actually modify the scale to achieve the value set. |
Methods from Container
Events from Container
Properties inherited from DisplayObject
Methods inherited from DisplayObject
Inherited Events from DisplayObject
|
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
Fired when the mouse pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the mouse pointer exits a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Fired when the pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the pointer leaves the hit testing boundaries of a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
Capture phase equivalent of |
|
PIXI.FederatedWheelEvent |
Fired when a the user scrolls with the mouse cursor over a DisplayObject. |
PIXI.FederatedWheelEvent |
Capture phase equivalent of |
Public Properties
height: number
The height of the Container, setting this will actually modify the scale to achieve the value set.
interactiveChildren: boolean = true
Determines if the children to the displayObject can be clicked/touched
Setting this to false allows PixiJS to bypass a recursive hitTest
function
sortableChildren: boolean
If set to true, the container will sort its children by zIndex value when updateTransform() is called, or manually if sortChildren() is called.
This actually changes the order of elements in the array, so should be treated as a basic solution that is not performant compared to other solutions, such as @link https://github.com/pixijs/pixi-display
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.
sortDirty: boolean
Should children be sorted by zIndex at the next updateTransform call.
Will get automatically set to true if a new child is added, or if a child's zIndex changes.
width: number
The width of the Container, setting this will actually modify the scale to achieve the value set.
Public Methods
addChild(children: PIXI.DisplayObject) → {PIXI.DisplayObject}
Adds one or more children to the container.
Multiple items can be added like so: myContainer.addChild(thingOne, thingTwo, thingThree)
Name | Type | Description |
---|---|---|
children | PIXI.DisplayObject |
The DisplayObject(s) to add to the container |
Type | Description |
---|---|
PIXI.DisplayObject |
|
addChildAt(child: PIXI.DisplayObject, index: number) → {PIXI.DisplayObject}
Adds a child to the container at a specified index. If the index is out of bounds an error will be thrown
Name | Type | Description |
---|---|---|
child | PIXI.DisplayObject |
The child to add |
index | number |
The index to place the child in |
Type | Description |
---|---|
PIXI.DisplayObject |
The child that was added. |
calculateBounds() → {void}
Recalculates the bounds of the container.
This implementation will automatically fit the children's bounds into the calculation. Each child's bounds is limited to its mask's bounds or filterArea, if any is applied.
Type | Description |
---|---|
void |
containerUpdateTransform() → {}
Container default updateTransform, does update children of container. Will crash if there's no parent element.
destroy(options: IDestroyOptions | boolean) → {void}
Removes all internal references and listeners as well as removes children from the display list.
Do not use a Container after calling destroy
.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
options | IDestroyOptions | boolean |
<optional> |
Options parameter. A boolean will act as if all options have been set to that value |
|
options.children | boolean |
<optional> |
false |
if set to true, all the children will have their destroy method called as well. 'options' will be passed on to those calls. |
options.texture | boolean |
<optional> |
false |
Only used for child Sprites if options.children is set to true Should it destroy the texture of the child sprite |
options.baseTexture | boolean |
<optional> |
false |
Only used for child Sprites if options.children is set to true Should it destroy the base texture of the child sprite |
Type | Description |
---|---|
void |
getChildAt(index: number) → {T}
Returns the child at the specified index
Name | Type | Description |
---|---|---|
index | number |
The index to get the child at |
Type | Description |
---|---|
T |
|
getChildByName(name: string, deep: boolean) → {PIXI.DisplayObject}
Returns the display object in the container.
Recursive searches are done in a preorder traversal.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name | string |
Instance name. |
||
deep | boolean |
<optional> |
false |
Whether to search recursively |
Type | Description |
---|---|
PIXI.DisplayObject |
The child with the specified name. |
getChildIndex(child: T) → {number}
Returns the index position of a child DisplayObject instance
Name | Type | Description |
---|---|---|
child | T |
The DisplayObject instance to identify |
Type | Description |
---|---|
number |
|
getLocalBounds(rect: PIXI.Rectangle, skipChildrenUpdate: boolean) → {PIXI.Rectangle}
Retrieves the local bounds of the displayObject as a rectangle object.
Calling getLocalBounds
may invalidate the _bounds
of the whole subtree below. If using it inside a render()
call, it is advised to call getBounds()
immediately after to recalculate the world bounds of the subtree.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
rect | PIXI.Rectangle |
<optional> |
Optional rectangle to store the result of the bounds calculation. |
|
skipChildrenUpdate | boolean | false |
Setting to |
Type | Description |
---|---|
PIXI.Rectangle |
|
removeChild(children: PIXI.DisplayObject) → {PIXI.DisplayObject}
Removes one or more children from the container.
Name | Type | Description |
---|---|---|
children | PIXI.DisplayObject |
The DisplayObject(s) to remove |
Type | Description |
---|---|
PIXI.DisplayObject |
The first child that was removed. |
removeChildAt(index: number) → {T}
Removes a child from the specified index position.
Name | Type | Description |
---|---|---|
index | number |
The index to get the child from |
Type | Description |
---|---|
T |
The child that was removed. |
removeChildren(beginIndex: number, endIndex) → {T[]}
Removes all children from this container that are within the begin and end indexes.
Name | Type | Default | Description |
---|---|---|---|
beginIndex | number | 0 |
The beginning position. |
endIndex |
The ending position. Default value is size of the container. |
Type | Description |
---|---|
T[] |
|
render(renderer: PIXI.Renderer) → {void}
Renders the object using the WebGL renderer.
The _render method is be overriden for rendering the contents of the
container itself. This render
method will invoke it, and also invoke the render
methods of all
children afterward.
If renderable
or visible
is false or if worldAlpha
is not positive or if cullable
is true and
the bounds of this object are out of frame, this implementation will entirely skip rendering.
See PIXI.DisplayObject for choosing between renderable
or visible
. Generally,
setting alpha to zero is not recommended for purely skipping rendering.
When your scene becomes large (especially when it is larger than can be viewed in a single screen), it is advised to employ culling to automatically skip rendering objects outside of the current screen. See cullable and cullArea. Other culling methods might be better suited for a large number static objects; see @pixi-essentials/cull and pixi-cull.
The renderAdvanced method is internally used when when masking or filtering is applied on a container. This does, however, break batching and can affect performance when masking and filtering is applied extensively throughout the scene graph.
Name | Type | Description |
---|---|---|
renderer | PIXI.Renderer |
The renderer |
Type | Description |
---|---|
void |
renderCanvas(renderer: PIXI.CanvasRenderer) → {void}
Renders the object using the Canvas renderer
Name | Type | Description |
---|---|---|
renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
renderCanvas(_renderer: PIXI.CanvasRenderer) → {void}
Renders the object using the Canvas renderer
Name | Type | Description |
---|---|---|
_renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
setChildIndex(child: T, index: number) → {void}
Changes the position of an existing child in the display object container
Name | Type | Description |
---|---|---|
child | T |
The child DisplayObject instance for which you want to change the index number |
index | number |
The resulting index number for the child display object |
Type | Description |
---|---|
void |
sortChildren() → {void}
Sorts children by zIndex. Previous order is maintained for 2 children with the same zIndex.
Type | Description |
---|---|
void |
swapChildren(child: T, child2: T) → {void}
Swaps the position of 2 Display Objects within this container.
Name | Type | Description |
---|---|---|
child | T |
First display object to swap |
child2 | T |
Second display object to swap |
Type | Description |
---|---|
void |
updateTransform() → {void}
Updates the transform on all children of this container for rendering.
Type | Description |
---|---|
void |
Protected Methods
protected _calculateBounds() → {void}
Recalculates the content bounds of this object. This should be overriden to calculate the bounds of this specific object (not including children).
Type | Description |
---|---|
void |
protected _render(_renderer: PIXI.Renderer) → {void}
To be overridden by the subclasses.
Name | Type | Description |
---|---|---|
_renderer | PIXI.Renderer |
The renderer |
Type | Description |
---|---|
void |
protected _renderCanvas(_renderer: PIXI.CanvasRenderer) → {void}
To be overridden by the subclass
Name | Type | Description |
---|---|---|
_renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
protected _renderWithCulling(renderer: PIXI.Renderer) → {void}
Renders this object and its children with culling.
Name | Type | Description |
---|---|---|
renderer | PIXI.Renderer |
The renderer |
Type | Description |
---|---|
void |
protected onChildrenChange(_length: number) → {void}
Overridable method that can be used by Container subclasses whenever the children array is modified.
Name | Type | Attributes | Description |
---|---|---|---|
_length | number |
<optional> |
Type | Description |
---|---|
void |
protected renderAdvanced(renderer: PIXI.Renderer) → {void}
Render the object using the WebGL renderer and advanced features.
Name | Type | Description |
---|---|---|
renderer | PIXI.Renderer |
The renderer |
Type | Description |
---|---|
void |
Events
Fired when a DisplayObject is added to this Container.
Name | Type | Description |
---|---|---|
child | PIXI.DisplayObject |
The child added to the Container. |
container | PIXI.Container |
The container that added the child. |
index | number |
The children's index of the added child. |
Fired when a pointer device button (usually a mouse left-button) is pressed and released on
the display object. DisplayObject's interactive
property must be set to true
to fire event.
A click
event fires after the pointerdown
and pointerup
events, in that
order. If the mouse is moved over another DisplayObject after the pointerdown
event, the
click
event is fired on the most specific common ancestor of the two target DisplayObjects.
The detail
property of the event is the number of clicks that occurred within a 200ms
window of each other upto the current click. For example, it will be 2
for a double click.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a mouse button (usually a mouse left-button) is pressed on the display.
object. DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
The mousedown event. |
Fired when a pointer device (usually a mouse) is moved while over the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device (usually a mouse) is moved off the display object.
DisplayObject's interactive
property must be set to true
to fire event.
This may be fired on a DisplayObject that was removed from the scene graph immediately after
a mouseover
event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device (usually a mouse) is moved onto the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button (usually a mouse left-button) is released over the display
object. DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button (usually a mouse left-button) is released outside the
display object that initially registered a
mousedown.
DisplayObject's interactive
property must be set to true
to fire event.
This event is specific to the Federated Events API. It does not have a capture phase, unlike most of the
other events. It only bubbles to the most specific ancestor of the targets of the corresponding pointerdown
and pointerup
events, i.e. the target of the click
event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when the operating system cancels a pointer event.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button is pressed on the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device is moved while over the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device is moved off the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device is moved onto the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button is pressed and released on the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button is released over the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device button is released outside the display object that initially
registered a pointerdown.
DisplayObject's interactive
property must be set to true
to fire event.
This event is specific to the Federated Events API. It does not have a capture phase, unlike most of the
other events. It only bubbles to the most specific ancestor of the targets of the corresponding pointerdown
and pointerup
events, i.e. the target of the click
event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device secondary button (usually a mouse right-button) is pressed
and released on the display object. DisplayObject's interactive
property must be set to true
to fire event.
This event follows the semantics of click
.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device secondary button (usually a mouse right-button) is pressed
on the display object. DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device secondary button (usually a mouse right-button) is released
over the display object. DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a pointer device secondary button (usually a mouse right-button) is released
outside the display object that initially registered a
rightdown.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a touch point is placed and removed from the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when the operating system cancels a touch.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a touch point is removed from the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a touch point is removed outside of the display object that initially
registered a touchstart.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a touch point is moved along the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |
Fired when a touch point is placed on the display object.
DisplayObject's interactive
property must be set to true
to fire event.
These events are propagating from the EventSystem in @pixi/events.
Name | Type | Description |
---|---|---|
event | PIXI.FederatedPointerEvent |
Event |