Phaser Manual Scale
Uses transforms to scale a container to its maximum size based on aspect
by mattstyles
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.min.js"></script>
<div class="js-container container">
<button class="js-click btn">
Click Me
</button>
</div>
CSS
html,
body {
min-height: 100vh;
min-width: 100vw;
}
body {
margin: 0;
}
.container {
position: absolute;
left: 50%;
top: 50%;
width: 800px;
height: 600px;
transform-origin: left top;
transform: translate( -50%, -50% );
}
.btn {
position: relative;
z-index: 1000;
font-size: 24px;
line-height:2;
border: none;
background: blue;
color: white;
padding: 0 18px;
}
JavaScript
const WIDTH = 800
const HEIGHT = 600
var container = document.querySelector( '.js-container' )
var game = new Phaser.Game(WIDTH, HEIGHT, Phaser.AUTO, container, { create: create });
function create() {
resize()
Object.assign( game.canvas.style, {
position: 'absolute',
left: 0,
top: 0,
zIndex: 0
})
var text = "- phaser -\n with a sprinkle of \n pixi dust.";
var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
var t = game.add.text(game.world.centerX-300, 0, text, style);
}
function resize( event ) {
let vw = window.innerWidth
let vh = window.innerHeight
let scale = Math.min( vw / WIDTH, vh / HEIGHT )
container.style.transform = `scale( ${ scale } ) translate( -50%, -50% )`
}
window.addEventListener( 'resize', resize )
var btn = document.querySelector( '.js-click' )
btn.addEventListener( 'click', event => {
alert( 'clickety click' )
})