0

I've been working on a game in pure JavaScript/CSS/HTML and I've encountered the problem where my 1920x1080 phone displays my game as 586x330 with a pixel display ratio of 3. What I want to know is how to have my game run on the screen in HD since I don't want to have to a low-res game when it could be higher-res.

When I run the game on a web browser (desktop or mobile) the pixel density is 1 so everything is fine, the problem occurs when I run it as an android app using PhoneGap.

Any help would be appreciated, I'm pretty stuck here.

Thanks,

  • 1
    Possible duplicate of [drawImage generates bad quality compared to img tag](http://stackoverflow.com/questions/41528103/drawimage-generates-bad-quality-compared-to-img-tag) – Kaiido Feb 01 '17 at 01:21
  • Don't dismiss dPR, use it : scale your canvas size by the dPR, then downscale it via CSS. You'll get the correct dPR for your canvas. (don't forget to always respect your canvas ratio between CSS and properties sizes !) – Kaiido Feb 01 '17 at 01:24

1 Answers1

1

I'm assuming you're using the <canvas> element as it's the graphical foundation for most games.

If that's the case you need to ensure that you only ever resize it using its inline width and height attributes. By inline, I mean the ones are literally embedded in the html:

<canvas width="--your width--" height="--your height--">
</canvas>

What you want to make sure is that there is no other sizing being applied to your canvas.

None of this:

/* style.css */
canvas {
    transform: scale(1.5, 1.5);
}

And even none of this:

/* style.css */
canvas {
    width: 100%;
    height: 100%;
}

The thing is that the number of pixels of resolution that a canvas has to work with is defined only by its inline width and height attributes. If you want your canvas to have the same resolution as your phone screen you need to use javascript to modify its width and height whenever the screen size changes:

/* thing.js */
var resizeCanvas = function() {
    var canvasElem = getYourCanvasElement();

    canvasElem.width = getWidthOfScreen();
    canvasElem.height = getHeightOfScreen();
}

// Make sure canvas fits initially...
whenPageLoads(resizeCanvas);

// And then resize it whenever needed
whenScreenSizeChanges(resizeCanvas);

Of course I don't know what frameworks you're using so that code is just a template. You need to decide how to implement getYourCanvasElement, getWidthOfScreen, getHeightOfScreen, whenPageLoads, and whenScreenSizeChanges.

EDIT: One other thing that may be affect the size of your canvas is the browser viewport scaling. There's good insight into that here.

Community
  • 1
  • 1
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
  • Thanks for the help! I am using the element. I do not have any css styling apart from a 10px border and absolute positioning. What I am using to define the width and height is that I am using window.innerWidth and window.innerHeight, I'm not sure if that may be the root of this problem, I have seen others use window.screen.height – Athavan Karunakaran Feb 01 '17 at 00:44
  • No problem! You could be running into viewport scaling. Check my edit at the bottom of my answer :) – Gershom Maes Feb 01 '17 at 01:17