SamYan Posted October 2, 2018 Share Posted October 2, 2018 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 Quote Link to comment Share on other sites More sharing options...
Jonny Shaw Posted October 2, 2018 Share Posted October 2, 2018 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`; } SamYan 1 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 2, 2018 Author Share Posted October 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 2, 2018 Author Share Posted October 2, 2018 Nobody can help please? Quote Link to comment Share on other sites More sharing options...
jonforum Posted October 2, 2018 Share Posted October 2, 2018 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 SamYan 1 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 2, 2018 Author Share Posted October 2, 2018 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. ? Quote Link to comment Share on other sites More sharing options...
jonforum Posted October 3, 2018 Share Posted October 3, 2018 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 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 3, 2018 Author Share Posted October 3, 2018 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/ Quote Link to comment Share on other sites More sharing options...
jonforum Posted October 3, 2018 Share Posted October 3, 2018 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. Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 3, 2018 Author Share Posted October 3, 2018 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: mobile screen: Quote Link to comment Share on other sites More sharing options...
jonforum Posted October 3, 2018 Share Posted October 3, 2018 hum , i don't know, the code i share you work for my side. Example here at right, i have my app set to 1920x1080 resized to to fake mobile frame ~~767x431 and it work . i have no idea where is your issue, it strange, maybe we don't have similar context. humm... Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 3, 2018 Author Share Posted October 3, 2018 i dont understand, why resizing canvas, its not resizing the stage and children containers correcty. Quote Link to comment Share on other sites More sharing options...
jonforum Posted October 3, 2018 Share Posted October 3, 2018 yep me to, try with @ivan.popelyshev it a magic russian dev from pixiJs . But on my side i was try but i can not help you more sorry my friend. SamYan 1 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 3, 2018 Author Share Posted October 3, 2018 4 minutes ago, jonforum said: yep me to, try with @ivan.popelyshev it a magic russian dev from pixiJs . But on my side i was try but i can not help you more sorry my friend. Thank you very much! Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 3, 2018 Share Posted October 3, 2018 Your code looks correct, I don't see a problem. Can you please make a fiddle? Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 3, 2018 Author Share Posted October 3, 2018 43 minutes ago, ivan.popelyshev said: Your code looks correct, I don't see a problem. Can you please make a fiddle? https://codesandbox.io/s/n5pkq5vjpj?moduleview=1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 4, 2018 Share Posted October 4, 2018 The only mobile device I have shows me black screen. I dont have ideas how to help yet Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 4, 2018 Author Share Posted October 4, 2018 3 hours ago, ivan.popelyshev said: The only mobile device I have shows me black screen. I dont have ideas how to help yet Ivan, Do you know any way to manage the diferentes screen in pixijs ? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 4, 2018 Share Posted October 4, 2018 No, and there are no articles about that. Many people solved it on their own but nobody wanted to make a plugin. Your approach has to work, i dont know what is wrong there. You can join pixijs slack and ask people there. SamYan 1 Quote Link to comment Share on other sites More sharing options...
bubamara Posted October 4, 2018 Share Posted October 4, 2018 @SamYan is this working for you? http://jsfiddle.net/om0hskfy/8/ SamYan 1 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 4, 2018 Author Share Posted October 4, 2018 2 hours ago, bubamara said: @SamYan is this working for you? http://jsfiddle.net/om0hskfy/8/ Thank you friend! It's working!! Quote Link to comment Share on other sites More sharing options...
bubamara Posted October 5, 2018 Share Posted October 5, 2018 You're welcome. And if you want to go further, you can implement 'safe area', which is nicely explained here : http://www.williammalone.com/articles/html5-game-scaling/ SamYan 1 Quote Link to comment Share on other sites More sharing options...
SamYan Posted October 5, 2018 Author Share Posted October 5, 2018 5 hours ago, bubamara said: You're welcome. And if you want to go further, you can implement 'safe area', which is nicely explained here : http://www.williammalone.com/articles/html5-game-scaling/ Finally i got it! Thank you friend! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.