How to scale and center Pixi.js stage?

How to scale and center Pixi.js stage?

by Alexander Farber

HTML

<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pixi.js@5.3.3/dist/pixi.min.js"></script>
<div id="mainDiv">
  <div id="leftMenuDiv">
    <button id="btn1">Button 1</button><br>
    <button id="btn2">Button 2</button><br>
    <button id="btn3">Button 3</button><br>
  </div>

  <div id="canvasDiv">
    <canvas id="pixiCanvas"></canvas>
  </div>

  <div id="rightMenuDiv">
    <input id="scaleCheck" type="checkbox">
    <label for="scaleCheck">Scale and center Pixi stage</label><br>
    <button id="btn4">Button 4</button><br>
    <button id="btn5">Button 5</button><br>
    <button id="btn6">Button 6</button><br>
  </div>
</div>

CSS

body {
  margin: 0;
  padding: 0;
}

#mainDiv {
  width: 100%;
  height: 100vh;
  background: #FFF;
  display: flex;
}

#leftMenuDiv {
  text-align: center;
  background: #FCC;
}

#rightMenuDiv {
  text-align: center;
  background: #CFC;
  flex-shrink: 1;
}

#canvasDiv {
  flex-grow: 1;
}

#pixiCanvas {
  width: 100%;
  height: 100%;
  background: #CCF;
  box-sizing: border-box;
  border: 4px red dotted;
}

JavaScript

$(function() {
      var WIDTH = 400;
      var HEIGHT = 400;
      var RATIO = WIDTH / HEIGHT;

      var scaleX = 1,
        scaleY = 1,
        offsetX = 0,
        offsetY = 0,
        oldX, oldY;

      var app = new PIXI.Application({
        width: WIDTH,
        height: HEIGHT,
        view: document.getElementById('pixiCanvas'),
        antialias: true,
        backgroundColor: 0xFFFFFF
      });

      var stage = new PIXI.Sprite();
      stage.interactive = true;
      stage.on('mousedown', onDragStart)
        .on('touchstart', onDragStart)
        .on('mousemove', onDragMove)
        .on('touchmove', onDragMove)
        .on('mouseup', onDragEnd)
        .on('mouseupoutside', onDragEnd)
        .on('touchend', onDragEnd)
        .on('touchendoutside', onDragEnd);

      var background = new PIXI.Graphics();
      background.clear();
      background.lineStyle(8, 0x0000FF, 1);
      background.beginFill(0xCCCCFF);
      background.drawRoundedRect(0, 0, WIDTH, HEIGHT, WIDTH / 16, WIDTH / 16);
      background.endFill();
      stage.addChild(background);

      var bunny = PIXI.Sprite.from('https://pixijs.io/examples/examples/assets/bunny.png');
      stage.addChild(bunny);

      function animate() {
        requestAnimationFrame(animate);
        app.renderer.render(stage);
      }

      function onDragStart(ev) {
        var stagePoint = ev.data.getLocalPosition(stage);
        if (bunny.getBounds().contains(stagePoint.x, stagePoint.y)) {
          console.log('onDragStart ' + stagePoint.x + ', ' + stagePoint.y);
          oldX = stagePoint.x;
          oldY = stagePoint.y;
          this.data = ev.data;
        }
      }

      function onDragMove() {
        if (this.data) {
          var stagePoint = this.data.getLocalPosition(stage);
          console.log('onDragMove ' + stagePoint.x + ', ' + stagePoint.y);
          bunny.x += stagePoint.x - oldX;
          bunny.y += stagePoint.y - oldY;
          oldX = stagePoint.x;
          oldY =...