(function () {
  pc.Application.prototype.resizeCanvas = function (width, height) {
    if (!this._allowResize) return; // prevent resizing (e.g. if presenting in VR HMD)

    // prevent resizing when in XR session
    if (this.xr && this.xr.session)
      return;

    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;

    // Get MRAID max size if available
    if (window.mraid && typeof mraid.getMaxSize === 'function') {
      var mraidSize = mraid.getMaxSize();
      if (mraidSize && mraidSize.width && mraidSize.height) {
        windowWidth = mraidSize.width;
        windowHeight = mraidSize.height;
      }
    }

    if (this._fillMode === pc.FILLMODE_KEEP_ASPECT) {
      var r = this.graphicsDevice.canvas.width / this.graphicsDevice.canvas.height;
      var winR = windowWidth / windowHeight;

      if (r > winR) {
        width = windowWidth;
        height = width / r;
      } else {
        height = windowHeight;
        width = height * r;
      }
    } else if (this._fillMode === pc.FILLMODE_FILL_WINDOW) {
      width = windowWidth;
      height = windowHeight;
    } else {
      // FILLMODE_NONE: For MRAID, use KEEP_ASPECT behavior to ensure correct sizing
      var r = this.graphicsDevice.canvas.width / this.graphicsDevice.canvas.height;
      var winR = windowWidth / windowHeight;

      if (r > winR) {
        width = windowWidth;
        height = width / r;
      } else {
        height = windowHeight;
        width = height * r;
      }
    }

    this.graphicsDevice.canvas.style.width = width + 'px';
    this.graphicsDevice.canvas.style.height = height + 'px';

    this.updateCanvasSize();

    // return the final values calculated for width and height
    return {
      width: width,
      height: height
    };
  }
})();
