How to move an object in a PixiJS application

/ #Pixi.js

Making an object move is something everyone needs to know how to do. This is one of the basic buildings blocks of game building.

Create an empty HTML file

We will start off by create a blank HTML page and an empty PixiJS application. Inside the new file, add this code:

<!DOCTYPE html>

        <meta charset="utf-8">
        <title>PixiJS Moving objects</title>

        <script src=""></script>
        <script type="text/javascript">
            // The application
            var app = new PIXI.Application();

            // Add application to the DOM

If you open up the HTML file in your browser, you should see a big black square. That means that the PixiJS application is working.

We are just using a CDN to import PixiJS, so we don't have to install anything. This is really easy to do, and it's more than enough for what we're doing in this tutorial.

Inside the <script> tag, we're creating a new Pixi application and adding it to the body.

Adding an object

In order to move an object, we need to see an object. We can make it really easy, and just add a red square. Below the line were the application is added to the body, add this code:

var square = new PIXI.Graphics();
square.drawRect(0, 0, 50, 50);
square.x = 100;
square.y = 100;

In the first line, we define a new variable called square. This is a reference to the Graphics API from PixiJS. On the next line, we set the fill color to 0xff0000 (red). We then set the position and appending it to the stage.

If you open up the file you created in a browser, a small red square will appear.

Making the object move

The next thing we need to do is to add this line below the line where we append the square to the stage:


You're not ready to see the result yet, we need to create the update function. Below this line, add this code:

function update() {
    square.position.x += 1;


This is the function where everything happens. For every possible update (60 times per second, 60 fps), this function will be called. There's a couple of different ways to to this, but "requestAnimationFrame" is really easy to use.

Every time the function is called, we add one pixel to the x position of the square. But the line below "app.render(app.stage);" is equally important, because this redraws the entire canvas every time.

I hope this was interesting. Try to play around with more objects and options like rotation, scale, etc. If you have any questions, feel free to leave a reply below.


cheap louis vuitton handbag outlet | Aug 04, 23 12:36

louis vuitton outlet is on sale today|nigo|virgil|lv|louis vuitton|monogram|virgillouis vuitton outlet

Add comment


Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.