From "innerWidth" to Solar2D content units of the screen

Hi there,

If I get the innerWidth via Javascript in my html5 project, how can I convert that value to Solar2D content units of the screen? I mean: how to get the real contentWidth (using this ‘innerWidth’) for a mobile device if I run a html5 project in a mobile?

Hey!

I’m not sure if you’ve already solved this, but you can do this via utilising scale factor detailed in:

Thank you for your response, but the documentation does not give me the solution.

I will try to explain better the context of my code:

Let’s say I have an app for the html5 platform.
If my app runs on a mobile device browser, the dimensions of the app will be the ones I inserted into the ‘window’ table of my ‘build.settings’ file, which are the values of ‘defaultViewWidth’ and ‘defaultViewHeight’, so when it reads the ‘display.actualContentWidth’ value it won’t actually be the width of my mobile device, but the value given in ‘defaultViewWidth’.
My question is: How can I get the actual value of the width (or height) of my mobile device in solar2D content units of the screen?

In javascript I can easily get the width of my device with ‘innerWidth’, but this value does not correspond to the width of the device in Solar2D content units.

The scale factor from above tells you how much the content is being scaled.

For example, let’s say that your content area is 960x640. If you’d run your app on devices with various screen sizes, you’d get:

  • screen size = 960x640, scale factor = 1
  • screen size = 960x580, scale factor = 0.90625
  • screen size = 1136x640, scale factor = 1
  • screen size = 1334x750, scale factor = 1.171875
  • screen size = 2000x640, scale factor = 1

So, in this case, the moment that the screen gets wider or narrower, Solar2D starts to scale the display objects up or down depending on the scale factor.

These examples are from a landscape orientation, so if you were to multiply display.actualContentWidth with the scale factor, you’d get the actual width of the screen in pixels, e.g. 960 x 1.171875 = 1334.

This also works in reverse, so if you know the pixel width or height of your device, you can use the scale factor to find out what the width or height would be in Solar2D content units.