Jump to content

why is canvas width is the same after setuserScale?


Henryk
 Share

Recommended Posts

Well, I scale my game (1280x720) with setUserScale(scaleFactor, scaleFactor) to the device width (for example: 480x320).

 

When accessing the canvas width it still gives me the original width. How do I get the new size of the canvas?

Link to comment
Share on other sites

This was not exactly my problem. Basically I just wanted to center the canvas element vertically and no obvious way worked!

 

I now have this work around:

// set scaleMode and alignthis.game.scale.scaleMode = Phaser.ScaleManager.USER_SCALE;this.game.scale.compatibility.forceMinimumDocumentHeight = true; // seems to do nothingthis.game.scale.pageAlignHorizontally = true; // seems like I would not even need thisthis.game.scale.pageAlignVeritcally = true; // seems to do nothing// calculate the scale factorscaleFactor = deviceW / origW;// set scalethis.game.scale.setUserScale(scaleFactor, scaleFactor);this.scale.refresh();// calculate the scaled dimensions of the canvascanvasW = origW * scaleFactor;canvasH = origH * scaleFactor;// calculate the Top DistancecanvasTop = (deviceH - canvasH) / 2;// make it a relevant stringcanvasTop = canvasTop.toString() + 'px';// get the after setUserScale() applied style attributesthis.currentStyle = this.game.canvas.getAttribute("style");// add the marginTop to itthis.newStyle = this.currentStyle + 'margin-top: ' + canvasTop;// set the attributethis.game.canvas.setAttribute('style', this.newStyle);

I hope my variable names are speaking for themself.

 

Anyway, in my opinion there should be an easier way, shouldn't it?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...