Closed Matthieu-BRANTHOME closed 6 months ago
Hello @Matthieu-BRANTHOME,
I also have similar problem with FIT mode and bootstrap. When you have a canvas in a blank page and resize you will see FIT mode is actually working perfect. When you start using bootstrap row col m-0
kinda classes I think the issue that parent container of Phaser can't actually resize cause bootstrap classes restrictions.
So I've created a small resize function to kinda make it happen and I've been using this since so long. Just give it a try.
// Hope u have jquery inside if you don't please change the selector and width(), height() methods :)
function resize(_game) {
let canvas = _game.canvas;
let par = $('.content');
let windowWidth = par.width();
let windowHeight = par.height();
let gameRatio = _game.config.width / _game.config.height;
canvas.style.width = windowWidth + 'px';
canvas.style.height = (windowWidth / gameRatio) + 'px';
}
window.addEventListener('resize', () => resize(game));
Hello @halilcakar and thank you very much for this answer. I've done that :
function resize(_game) {
var canvas = _game.canvas;
var windowWidth = window.innerWidth;
//var windowHeight = window.innerHeight;
// let par = $('.content');
// let windowWidth = par.width();
// let windowHeight = par.height();
var gameRatio = _game.config.width / _game.config.height;
canvas.style.width = windowWidth + 'px';
canvas.style.height = (windowWidth / gameRatio) + 'px';
}
window.addEventListener('resize', () => resize(game));
And it works! 👍
Hello @halilcakar and thank you very much for this answer. I've done that :
function resize(_game) { var canvas = _game.canvas; var windowWidth = window.innerWidth; //var windowHeight = window.innerHeight; // let par = $('.content'); // let windowWidth = par.width(); // let windowHeight = par.height(); var gameRatio = _game.config.width / _game.config.height; canvas.style.width = windowWidth + 'px'; canvas.style.height = (windowWidth / gameRatio) + 'px'; } window.addEventListener('resize', () => resize(game));
And it works! 👍
Oh I'm glad it works for you :)
Please note that you need to remove the event listener if the page itself doesn't really reloads.
Please note that you need to remove the event listener if the page itself doesn't really reloads.
Sorry I'm not really understand: what does that mean?
Please note that you need to remove the event listener if the page itself doesn't really reloads.
Sorry I'm not really understand: what does that mean?
This is only needed if your application is SPA(Single Page Application). Meaning that you are using some kind of virtual router.
If you are using a virtual router then you should remove the event listener.
const res = () => resize(game);
window.addEventListener('resize', res);
// somewhere in your code before unload the page
window.removeEventListener('resize', res); // should appear so that next time you don't have get 2 resize listeners :)
Otherwise it's not needed.
Ok thanks, at the moment, i'm not using virtual router.
Version
Phaser Version: 3.53.1
Operating system: windows 10, XAMPP Local Server
Browsers: Chrome, Firefox, Edge (I didn't try the others)
Description
I have a scale problem on canvas:
Example Test Code