'Pixi.js viewport follow object

I'm making a simple game where a user walks around with a person. The person's gun will always be pointing the mouse. What I want is to allow the person to walk outside the viewport, with the viewport adapting to this. The viewport should always have the person in the middle. I already tried this library, but I couldn't get it to work. A good example of what I mean is Agar.io, where the player always sees himself in the middle of the viewport, and everything around him moves.

Does anyone have a clue how I should do this with Pixi.js?

My code:

    <!doctype html>
<html>
<head>
  <meta charset="utf-8">
    <title>Hello World</title>
      <style>
          canvas{border:2px solid #000;}
      </style>
</head>
<script src="pixi.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<script type="text/javascript">
    function keyboard(value) {
        let key = {};
        key.value = value;
        key.isDown = false;
        key.isUp = true;
        key.press = undefined;
        key.release = undefined;
        //The `downHandler`
        key.downHandler = event => {
            if (event.key === key.value) {
                if (key.isUp && key.press) key.press();
                key.isDown = true;
                key.isUp = false;
                event.preventDefault();
            }
        };

        //The `upHandler`
        key.upHandler = event => {
            if (event.key === key.value) {
                if (key.isDown && key.release) key.release();
                key.isDown = false;
                key.isUp = true;
                event.preventDefault();
            }
        };

        //Attach event listeners
        const downListener = key.downHandler.bind(key);
        const upListener = key.upHandler.bind(key);

        window.addEventListener(
            "keydown", downListener, false
        );
        window.addEventListener(
            "keyup", upListener, false
        );

        // Detach event listeners
        key.unsubscribe = () => {
            window.removeEventListener("keydown", downListener);
            window.removeEventListener("keyup", upListener);
        };
        return key;
    }
    //Aliases
    let Application = PIXI.Application,
        loader = PIXI.loader,
        resources = PIXI.loader.resources,
        Sprite = PIXI.Sprite;

    //Create a Pixi Application
    let app = new Application();
    app.renderer.backgroundColor = 0xffffff;

    //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([
            "sprite.png",
            "img/walking/walking1.png",
            "img/walking/walking2.png"
        ])
        .load(setup);

    let alienImages = ["img/walking/walking1.png","img/walking/walking2.png","img/walking/walking3.png","img/walking/walking4.png","img/walking/walking5.png","img/walking/walking6.png","img/walking/walking7.png","img/walking/walking8.png"];
    let textureArray = [];

    for (let i=0; i < 8; i++)
    {
        let texture = PIXI.Texture.fromImage(alienImages[i]);
        textureArray.push(texture);
    };

    console.log(textureArray);

    let cat;
    let pixie = new PIXI.MovieClip(textureArray);
    let radian;
    let radianLast;
    let mouseX;
    let mouseY;
    let left = keyboard("a"),
        up = keyboard("w"),
        right = keyboard("d"),
        down = keyboard("s");

    function checkIfAnimationStop(){
        if(!right.isDown && !left.isDown && !down.isDown && !up.isDown){
            pixie.gotoAndStop(0);
        }
    }

    //This `setup` function will run when the image has loaded
    function setup() {
        //Create the cat sprite
        cat = new Sprite(resources["sprite.png"].texture);
        //cat.anchor.set(84.5, 115.5);
        pixie.vx = 0;
        pixie.vy = 0;

        //Add the cat to the stage
        pixie.position.set(32, 32);
        app.stage.addChild(pixie);
        pixie.animationSpeed = 0.2;

        left.press = () => {
            //Change the cat's velocity when the key is pressed
            pixie.play();
            pixie.vx = -4;
        };

        //Left arrow key `release` method
        left.release = () => {
            //If the left arrow has been released, and the right arrow isn't down,
            //and the cat isn't moving vertically:
            //Stop the cat
            if (!right.isDown) {
                pixie.vx = 0;
            }
            left.isDown = false;
            checkIfAnimationStop();
        };

        //Up
        up.press = () => {
            pixie.play();
            pixie.vy = -4;
        };
        up.release = () => {
            if (!down.isDown) {
                pixie.vy = 0;
            }
            up.isDown = false;
            checkIfAnimationStop();
        };

        //Right
        right.press = () => {
            pixie.play();
            pixie.vx = 4;
        };
        right.release = () => {
            if (!left.isDown) {
                pixie.vx = 0;
            }
            right.isDown = false;
            checkIfAnimationStop();
        };

        //Down
        down.press = () => {
            pixie.play();
            pixie.vy = 4;
        };
        down.release = () => {
            if (!up.isDown) {
                pixie.vy = 0;
            }
            down.isDown = false;
            checkIfAnimationStop();
        };

        pixie.rotation = 0.5;

        state = play;

        app.ticker.add(delta => gameLoop(delta));
    }

    function gameLoop(delta){
        state(delta);

    }

    function play(delta){
        pixie.anchor.x = 0.9;
        pixie.anchor.y = 0.5;
        $(document).mousemove(function(e){
            mouseX = e.pageX;
            mouseY = e.pageY;
            radianLast = radian;
        });
        radian = Math.atan2(mouseX-pixie.x,-(mouseY-pixie.y));
        if((radian - pixie.rotation) > 0){
            if((radian - pixie.rotation) > 3.14){
                pixie.rotation -= 0.05;
                if(pixie.rotation < -3.1){
                    pixie.rotation = 3.13;
                }
            } else {
                pixie.rotation += 0.05;
            }
        }
        if((radian - pixie.rotation) < 0){
            if((radian - pixie.rotation) < -3.14){
                pixie.rotation += 0.05;
                if(pixie.rotation > 3.1){
                    pixie.rotation = -3.13;
                }
            } else {
                pixie.rotation -= 0.05;
            }
        }
        $("#angle").text(pixie.vx);
        $("#info").text(pixie.vy);
        if(pixie.vx != 0 && pixie.vy != 0){
            if(pixie.vx > 0){
                pixie.vx2 = 2.828;
            } else if(pixie.vx < 0){
                pixie.vx2 = -2.828;
            }
            if(pixie.vy > 0){
                pixie.vy2 = 2.828;
            } else if(pixie.vy < 0){
                pixie.vy2 = -2.828;
            }
            pixie.x += pixie.vx2;
            pixie.y += pixie.vy2;
            $("#angle").text(pixie.vx2);
            $("#info").text(pixie.vy2);
        } else {
            pixie.x += pixie.vx;
            pixie.y += pixie.vy;
        }
    }
</script>
<div id="angle"></div>
<div id="info"></div>
<div id="t"></div>
</body>
</html>


Solution 1:[1]

  1. Create a container to act as your world.
  2. Place your sprites in the container
  3. When you move your player, update the pivot of the container
    const world = new PIXI.Container();
    
    world.height = 4000;

    world.width = 4000;
    
    world.x = app.screen.width / 2;

    world.y = app.screen.height / 2;

    world.pivot.x = world.width / 2;

    world.pivot.y = world.height / 2;

    //create sprites

    world.addChild(sprite3);

    world.addChild(sprite2);
    
    world.addChild(player);

    app.stage.addChild(world);

    app.ticker.add((delta) => {
      let distance = delta * 10;
      if(Input.Left) { //Get player input however you please
        player.x -= distance;
        world.pivot.x = player.x;
      }
      else if (Input.Right) {
        player.x += distance;
        world.pivot.x = player.x;
      }
    });

You might need to tweak some placement, etc to line things up correctly, but this is the general idea.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SethWhite