Size and scale
You can change a sprite’s size by setting its width
and height
properties. Here’s how to give the cat a width
of 80 pixels and a height
of 120 pixels.
cat.width = 80;
cat.height = 120;
Add those two lines of code to the setup
function, like this:
function setup() {
//Create the `cat` sprite
let cat = new Sprite(resources["images/cat.png"].texture);
//Change the sprite's position
cat.x = 96;
cat.y = 96;
//Change the sprite's size
cat.width = 80;
cat.height = 120;
//Add the cat to the stage so you can see it
app.stage.addChild(cat);
}
Here’s the result:
You can see that the cat’s position (its top left corner) didn’t change, only its width and height.
Sprites also have scale.x
and scale.y
properties that change the sprite’s width and height proportionately. Here’s how to set the cat’s scale to half size:
cat.scale.x = 0.5;
cat.scale.y = 0.5;
Scale values are numbers between 0 and 1 that represent a percentage of the sprite’s size. 1 means 100% (full size), while 0.5 means 50% (half size). You can double the sprite’s size by setting its scale values to 2, like this:
cat.scale.x = 2;
cat.scale.y = 2;
Pixi has an alternative, concise way for you set sprite’s scale in one line of code using the scale.set
method.
cat.scale.set(0.5, 0.5);
If that appeals to you, use it!