Skip to content

Instantly share code, notes, and snippets.

@wojciak
Created July 6, 2014 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wojciak/c04aeac4df103809fd3d to your computer and use it in GitHub Desktop.
Save wojciak/c04aeac4df103809fd3d to your computer and use it in GitHub Desktop.
A dead-simple interface function for adding elements to canvas in PIXI
// A reference to the top-level displayObjectContainer
var scene;
// Definition
var addToCanvas = function (options) {
var container = options.container,
object = options.object,
at = options.at;
/**
* If the graphic object should be added to a container
*/
if (container) {
/**
* Should we add the graphic object on a specific layer in the container
*/
if (!isNaN(at)) {
container.addChildAt(object, at);
} else {
container.addChild(object);
}
}
/**
* Else the graphic object is added to the scene (main container)
*/
else {
scene.addChild(object);
}
// Pass along for chaining
return object;
};
// Usage
addToCanvas(
{
object: sprite, // A PIXI.Sprite instance (can be any PIXI object)
container: options.container, // A PIXI displayObjectContainer instance (optional)
at: options.at // The z-index position in the container (optional)
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment