Jump to content

Recommended Posts

Hi, i'm looking to get any solution form resizing the 'game' correctly in desktop and mobile screens but no way. I get the background image (sprite) non centralized and zoomed stage. Anyone can  tell me please what's the wrong?

CSS

body {
    background-color: rgb(0, 0, 0);
    width: 100%;
    height: 100%;
    overflow: hidden;
}

#pixi-canvas {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}

 

JS

const logicalWidth = 1280 // window.innerWidth;
const logicalHeight = 720 // window.innerHeight;

// Init Application
let app: PIXI.Application = new PIXI.Application(logicalWidth, logicalHeight, { backgroundColor: 0x2c3e50, roundPixels: true, resolution: window.devicePixelRatio, autoResize: true });

app.view.id = 'pixi-canvas';

// Add canvas to DOM
document.body.appendChild(app.view);

{ ... }

let background: PIXI.Sprite = PIXI.Sprite.fromImage("bg");

rootContainer.addChild(background);
app.stage.addChild(background);

{ ... }

this.resizeHandler(app.stage, app.renderer, logicalHeight, logicalWidth);

window.addEventListener('resize', () => {
	this.resizeHandler(app.stage, app.renderer, logicalHeight, logicalWidth);
});


private resizeHandler(stage: PIXI.Container, renderer: any, logicalHeight: number, logicalWidth: number) {
	const scaleFactor = Math.min(
		window.innerWidth / logicalWidth,
		window.innerHeight / logicalHeight
	);

	let newWidth: number = Math.ceil(logicalWidth * scaleFactor);
	let newHeight: number = Math.ceil(logicalHeight * scaleFactor);

	console.log('size', {w: logicalWidth, h: logicalHeight, newW: newWidth, newH: newHeight, scaleFactor: scaleFactor});

	renderer.resize(newWidth, newHeight);
	stage.scale.set(scaleFactor);
};

 

RESULT

image.thumb.png.bbf4d44687a0ba45be36ec0d276829a9.png

Link to post
Share on other sites

Can't say this is the 100% correct way to do it, but have run it this way for the last couple of projects...
 

document.body.appendChild(this.view);
window.addEventListener('resize', this.resize.bind(this));
this.resize();


    resize() {
        let current = { width: this.renderer.width, height: this.renderer.height };
        let ratio = current.width / current.height;
        if (window.innerWidth / window.innerHeight >= ratio) {
            var width = window.innerHeight * ratio;
            var height = window.innerHeight;
        } else {
            var width = window.innerWidth;
            var height = window.innerWidth / ratio;
        }
        this.view.style.width = `${ width }px`;
        this.view.style.height = `${ height }px`;
    }
Link to post
Share on other sites
23 minutes ago, sHAYM4N said:

Can't say this is the 100% correct way to do it, but have run it this way for the last couple of projects...
 


document.body.appendChild(this.view);
window.addEventListener('resize', this.resize.bind(this));
this.resize();


    resize() {
        let current = { width: this.renderer.width, height: this.renderer.height };
        let ratio = current.width / current.height;
        if (window.innerWidth / window.innerHeight >= ratio) {
            var width = window.innerHeight * ratio;
            var height = window.innerHeight;
        } else {
            var width = window.innerWidth;
            var height = window.innerWidth / ratio;
        }
        this.view.style.width = `${ width }px`;
        this.view.style.height = `${ height }px`;
    }

Thanks for response but i get the same result. Not working.

Link to post
Share on other sites

i dont know if this can help you because am not fully understand, maybe more information can help.
But here how i add zoom engine with pixijs and how the game are responsive to the node js app resolution and redimention

https://github.com/djmisterjon/PIXI.ProStageEditor/blob/master/js/plugins/core_Camera.js
 

Link to post
Share on other sites
10 minutes ago, jonforum said:

i dont know if this can help you because am not fully understand, maybe more information can help.
But here how i add zoom engine with pixijs and how the game are responsive to the node js app resolution and redimention

https://github.com/djmisterjon/PIXI.ProStageEditor/blob/master/js/plugins/core_Camera.js
 

Thanks for response but still not working. I'm looing and it's the stage scale problem (app.stage.scale). I can't calculate the aspect ratio for stage.

 

IVAN I NEED YOUR HELP. 😅

Link to post
Share on other sites

to get ratio of scale based on your resolution you can create a custom  global method 

 

    // Get a ratio for resize in a bounds
    function getRatio(obj, w, h) {
        let r = Math.min(w / obj.width, h / obj.height);
        return r;
    };

 

this will based on you max resolution game. In 2d sprite game, alway make ressource more resolution from what you really need for take zoom.

Also Example triks on my side is 1920x1080 max resolution screen, but i build all my ressource for a 8k game, so when i zoom, i don't have pixel issue or need compute on another textures.

 

so 

app.stage.scale.set ( getRatio(app.stage,maxWidth,maxHeight) ); 

should help you 

Link to post
Share on other sites
7 hours ago, jonforum said:

to get ratio of scale based on your resolution you can create a custom  global method 

 


    // Get a ratio for resize in a bounds
    function getRatio(obj, w, h) {
        let r = Math.min(w / obj.width, h / obj.height);
        return r;
    };

 

this will based on you max resolution game. In 2d sprite game, alway make ressource more resolution from what you really need for take zoom.

Also Example triks on my side is 1920x1080 max resolution screen, but i build all my ressource for a 8k game, so when i zoom, i don't have pixel issue or need compute on another textures.

 

so 


app.stage.scale.set ( getRatio(app.stage,maxWidth,maxHeight) ); 

should help you 

 

 

Thanks  for response but not working.

 

I have tried to give the max size of the background to my view and now it's working, but when i try in mobile screen, fails again.

 

http://jsfiddle.net/om0hskfy/

Link to post
Share on other sites
35 minutes ago, jonforum said:

Are you able to send a printScreen or preview of result what you target?
Your sprite does not mean much, not knowing what it should represent.
If you can send a target result vs current result , this can help to understand what your target behavior.

It's background:

 

desktop screen:

image.thumb.png.ce4995a03f1e049c16ff3b41c6a9a4d6.png

 

mobile screen:

image.thumb.png.669ad1cbbf9fd38981374a922fbdef3b.png

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...