Skip to content

Instantly share code, notes, and snippets.

@Chunjee
Forked from AleBles/ScaleManager.js
Created May 14, 2018 02:29
Show Gist options
  • Save Chunjee/1a00f982fee68197f7f664d818fc3a9f to your computer and use it in GitHub Desktop.
Save Chunjee/1a00f982fee68197f7f664d818fc3a9f to your computer and use it in GitHub Desktop.
Phaser v3 temp (css) scale amanger
//ScaleManager.js
class ScaleManager {
constructor(canvas, isMobile) {
this.canvas = canvas;
this.mobile = isMobile;
window.addEventListener('resize', () => {
this.resize(this.canvas);
if (this.mobile) {
if (window.innerWidth < window.innerHeight) {
this.leaveIncorrectOrientation();
} else {
this.enterIncorrectOrientation();
}
}
});
this.resize(this.canvas);
}
resize(canvas) {
let scale = Math.min(window.innerWidth / canvas.width, window.innerHeight / canvas.height);
let orientation = (this.mobile) ? 'left' : 'center';
canvas.setAttribute('style', '-ms-transform-origin: ' + orientation + ' top; -webkit-transform-origin: ' + orientation + ' top;' +
' -moz-transform-origin: ' + orientation + ' top; -o-transform-origin: ' + orientation + ' top; transform-origin: ' + orientation + ' top;' +
' -ms-transform: scale(' + scale + '); -webkit-transform: scale3d(' + scale + ', 1);' +
' -moz-transform: scale(' + scale + '); -o-transform: scale(' + scale + '); transform: scale(' + scale + ');' +
' display: block; margin: 0 auto;'
);
}
enterIncorrectOrientation() {
document.getElementById("orientation").style.display = "block";
document.getElementById("content").style.display = "none";
}
leaveIncorrectOrientation() {
document.getElementById("orientation").style.display = "none";
document.getElementById("content").style.display = "block";
}
}
export default ScaleManager;
//index.js
import ScaleManager from './ScaleManager';
var game = new Game({
type: WEBGL,
parent: 'content',
width: 720,
height: 1280,
scene: [ Preloader, MainMenu, Gameplay ],
transparent: true,
callbacks: {
postBoot: () => {
new ScaleManager(game.canvas, (!game.device.os.windows && !game.device.os.linux && !game.device.os.macOs));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment