Displaying sprites
After you’ve loaded an image, and used it to make a sprite, you need to add the sprite to Pixi’s stage
with the stage.addChild
method, like this:
app.stage.addChild(cat);
Remember that the stage
is the main container that holds all of your sprites.
Important: you won’t be able to see any of your sprites sprites unless you add them to the stage
!
Before we continue, let’s look at a practical example of how to use what you’ve just learnt to display a single image. In the examples/images
folder you’ll find a 64 by 64 pixel PNG image of a cat.
Here’s all the JavaScript code you need to load the image, create a sprite, and display it on Pixi’s stage:
//Create a Pixi Application
let app = new PIXI.Application({
width: 256,
height: 256,
antialias: true,
transparent: false,
resolution: 1
}
);
//Add the canvas that Pixi automatically created for you to the HTML document
document.body.appendChild(app.view);
//load an image and run the `setup` function when it's done
PIXI.loader
.add("images/cat.png")
.load(setup);
//This `setup` function will run when the image has loaded
function setup() {
//Create the cat sprite
let cat = new PIXI.Sprite(PIXI.loader.resources["images/cat.png"].texture);
//Add the cat to the stage
app.stage.addChild(cat);
}
When this code runs, here’s what you’ll see:
Now we’re getting somewhere!
If you ever need to remove a sprite from the stage, use the removeChild
method:
app.stage.removeChild(anySprite)
But usually setting a sprite’s visible
property to false
will be a simpler and more efficient way of making sprites disappear.
anySprite.visible = false;
Using aliases
You can save yourself a little typing and make your code more readable by creating short-form aliases for the Pixi objects and methods that you use frequently. For example, is prefixing PIXI
to all of Pixi’s objects starting to bog you down? If you think so, create a shorter alias that points to it. For example, here’s how you can create an alias to the TextureCache
object:
let TextureCache = PIXI.utils.TextureCache
Then, use that alias in place of the original, like this:
let texture = TextureCache["images/cat.png"];
In addition to letting you write more slightly succinct code, using aliases has an extra benefit: it helps to buffer you from Pixi’s frequently changing API. If Pixi’s API changes in future versions - which it will! - you just need to update these aliases to Pixi objects and methods in one place, at the beginning of your program, instead of every instance where they’re used throughout your code. So when Pixi’s development team decides they want to rearrange the furniture a bit, you’ll be one step ahead of them!
To see how to do this, let’s re-write the code we wrote to load an image and display it, using aliases for all the Pixi objects and methods.
//Aliases
let Application = PIXI.Application,
loader = PIXI.loader,
resources = PIXI.loader.resources,
Sprite = PIXI.Sprite;
//Create a Pixi Application
let app = new Application({
width: 256,
height: 256,
antialias: true,
transparent: false,
resolution: 1
}
);
//Add the canvas that Pixi automatically created for you to the HTML document
document.body.appendChild(app.view);
//load an image and run the `setup` function when it's done
loader
.add("images/cat.png")
.load(setup);
//This `setup` function will run when the image has loaded
function setup() {
//Create the cat sprite
let cat = new Sprite(resources["images/cat.png"].texture);
//Add the cat to the stage
app.stage.addChild(cat);
}
Most of the examples in this tutorial will use aliases for Pixi objects that follow this same model. Unless otherwise stated, you can assume that all the code examples that follow use aliases like these.
This is all you need to know to start loading images and creating sprites.
A little more about loading things
The format I’ve shown you above is what I suggest you use as your standard template for loading images and displaying sprites. So, you can safely ignore the next few paragraphs and jump straight to the next section, “Positioning sprites.” But Pixi’s loader
object is quite sophisticated and includes a few features that you should be aware of, even if you don’t use them on a regular basis. Let’s look at some of the most useful.
Make a sprite from an ordinary JavaScript Image object or Canvas
For optimization and efficiency it’s always best to make a sprite from a texture that’s been pre-loaded into Pixi’s texture cache. But if for some reason you need to make a texture from a regular JavaScript Image
object, you can do that using Pixi’s BaseTexture
and Texture
classes:
let base = new PIXI.BaseTexture(anyImageObject),
texture = new PIXI.Texture(base),
sprite = new PIXI.Sprite(texture);
You can use BaseTexture.fromCanvas
if you want to make a texture from any existing canvas element:
let base = new PIXI.BaseTexture.fromCanvas(anyCanvasElement),
If you want to change the texture the sprite is displaying, use the texture
property. Set it to any Texture
object, like this:
anySprite.texture = PIXI.utils.TextureCache["anyTexture.png"];
You can use this technique to interactively change the sprite’s appearance if something significant happens to it in the game.
Assigning a name to a loading file
It’s possible to assign a unique name to each resource you want to load. Just supply the name (a string) as the first argument in the add
method. For example, here’s how to name an image of a cat as catImage
.
PIXI.loader
.add("catImage", "images/cat.png")
.load(setup);
This creates an object called catImage
in loader.resources
. That means you can create a sprite by referencing the catImage
object, like this:
let cat = new PIXI.Sprite(PIXI.loader.resources.catImage.texture);
However, I recommend you don’t use this feature! That’s because you’ll have to remember all names you gave each loaded files, as well as make sure you don’t accidentally use the same name more than once. Using the file path name, as we’ve done in previous examples is simpler and less error prone.
Monitoring load progress
Pixi’s loader has a special progress
event that will call a customizable function that will run each time a file loads. progress
events are called by the loader
‘s on
method, like this:
PIXI.loader.on("progress", loadProgressHandler);
Here’s how to include the on
method in the loading chain, and call a user-definable function called loadProgressHandler
each time a file loads.
PIXI.loader
.add([
"images/one.png",
"images/two.png",
"images/three.png"
])
.on("progress", loadProgressHandler)
.load(setup);
function loadProgressHandler() {
console.log("loading");
}
function setup() {
console.log("setup");
}
Each time one of the files loads, the progress event calls loadProgressHandler
to display “loading” in the console. When all three files have loaded, the setup
function will run. Here’s the output of the above code in the console:
loading
loading
loading
setup
That’s neat, but it gets better. You can also find out exactly which file has loaded and what percentage of overall files are have currently loaded. You can do this by adding optional loader
and resource
parameters to the loadProgressHandler
, like this:
function loadProgressHandler(loader, resource) { /*...*/ }
You can then use resource.url
to find the file that’s currently loaded. (Use resource.name
if you want to find the optional name that you might have assigned to the file, as the first argument in the add
method.) And you can use loader.progress
to find what percentage of total resources have currently loaded. Here’s some code that does just that.
PIXI.loader
.add([
"images/one.png",
"images/two.png",
"images/three.png"
])
.on("progress", loadProgressHandler)
.load(setup);
function loadProgressHandler(loader, resource) {
//Display the file `url` currently being loaded
console.log("loading: " + resource.url);
//Display the percentage of files currently loaded
console.log("progress: " + loader.progress + "%");
//If you gave your files names as the first argument
//of the `add` method, you can access them like this
//console.log("loading: " + resource.name);
}
function setup() {
console.log("All files loaded");
}
Here’s what this code will display in the console when it runs:
loading: images/one.png
progress: 33.333333333333336%
loading: images/two.png
progress: 66.66666666666667%
loading: images/three.png
progress: 100%
All files loaded
That’s really cool, because you could use this as the basis for creating a loading progress bar.
(Note: There are additional properties you can access on the resource
object. resource.error
will tell you of any possible error that happened while trying to load a file. resource.data
lets you access the file’s raw binary data.)
More about Pixi’s loader
Pixi’s loader is ridiculously feature-rich and configurable. Let’s take a quick bird’s-eye view of its usage to get you started.
The loader’s chainable add
method takes 4 basic arguments:
add(name, url, optionObject, callbackFunction)
Here’s what the loader’s source code documentation has to say about these parameters:
name
(string): The name of the resource to load. If it’s not passed, the url
is used.url
(string): The url for this resource, relative to the baseUrl
of the loader.options
(object literal): The options for the load.options.crossOrigin
(Boolean): Is the request cross-origin? The default is to determine automatically.options.loadType
: How should the resource be loaded? The default value is Resource.LOAD_TYPE.XHR
.options.xhrType
: How should the data being loaded be interpreted when using XHR? The default value is Resource.XHR_RESPONSE_TYPE.DEFAULT
callbackFunction
: The function to call when this specific resource completes loading.
The only one of these arguments that’s required is the url
(the file that you want to load.)
Here are some examples of some ways you could use the add
method to load files. These first ones are what the docs call the loader’s “normal syntax”:
.add('key', 'http://...', function () {})
.add('http://...', function () {})
.add('http://...')
And these are examples of the loader’s “object syntax”:
.add({
name: 'key2',
url: 'http://...'
}, function () {})
.add({
url: 'http://...'
}, function () {})
.add({
name: 'key3',
url: 'http://...'
onComplete: function () {}
})
.add({
url: 'https://...',
onComplete: function () {},
crossOrigin: true
})
You can also pass the add
method an array of objects, or urls, or both:
.add([
{name: 'key4', url: 'http://...', onComplete: function () {} },
{url: 'http://...', onComplete: function () {} },
'http://...'
]);
(Note: If you ever need to reset the loader to load a new batch of files, call the loader’s reset
method: PIXI.loader.reset();
)
Pixi’s loader has many more advanced features, including options to let you load and parse binary files of all types. This is not something you’ll need to do on a day-to-day basis, and way outside the scope of this tutorial, so make sure to check out the loader’s GitHub repository for more information.