Created
July 6, 2014 20:51
-
-
Save wojciak/c04aeac4df103809fd3d to your computer and use it in GitHub Desktop.
A dead-simple interface function for adding elements to canvas in PIXI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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