Screen Resolution On A PhoneGap App

2020-02-25 22:59发布

I'm writing a Phonegap (3.2) app, I take a HTML5 canvas, make it full screen and the whole app UI is on this canvas.

Using a Samsung Galaxy S4 (Android 4.3) to test the app, the screen resolution of the app is only 360X640 and not 1080X1920 like I want it to be.

What is needed to be done in-order to make the app use the maximum possible screen resolution ?

I've added screen shots 1) // Looks OK but bad resolution

windowWidth = window.innerWidth; windowHeight = window.innerHeight; enter image description here

2) // Takes small part of the screen, rest is white

windowWidth = window.outerWidth; windowHeight = window.outerHeight; enter image description here

3) // Only a small part of the picture is visible, as if the image is at 1080X1920 but the screen is 'too small' for it.

windowWidth = screen.width; windowHeight = screen.height; enter image description here

and here is the full code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>PhoneGapTesting</title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <!-- -->
    <script type="text/javascript">

    var windowWidth;
    var windowHeight;

    var canvasMain;
    var contextMain;

    var backgroundImage;

    $(document).ready(function() {
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
        //windowWidth = window.outerWidth; // Only 'window.innerWidth' + 'window.innerHeight' look OK but not max resolution
        //windowHeight = window.outerHeight;
        //windowWidth = screen.width;
        //windowHeight = screen.height;

        canvasMain = document.getElementById("canvasSignatureMain");
        canvasMain.width = windowWidth;
        canvasMain.height = windowHeight;
        contextMain = canvasMain.getContext("2d");

        backgroundImage = new Image();
        backgroundImage.src = 'img/landscape_7.jpg';
        backgroundImage.onload = function() {
            contextMain.drawImage(backgroundImage, 0, 0, backgroundImage.width, backgroundImage.height, 0, 0, canvasMain.width, canvasMain.height);
        };

        $("#canvasSignatureMain").fadeIn(0);
    })

    </script>
</head>
<body scroll="no" style="overflow: hidden">
    <center>
        <div id="deleteThisDivButNotItsContent">
            <canvas id="canvasSignatureMain" style="border:1px solid #000000; position:absolute; top:0;left:0;"></canvas><br>
        </div>
    </center>
</body>
</html>

Thanks.

3条回答
再贱就再见
2楼-- · 2020-02-25 23:10

A solution that is working with both resolution and touch event issue (that came with Ken's post):

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

In the < head >

Thanks Ken for trying to help bro :))

查看更多
▲ chillily
3楼-- · 2020-02-25 23:30

window.innerWidth and window.innerHeight don't necessarily give you the real screen dimensions.

Try this instead:

windowWidth = window.screen.innerWidth; 
windowHeight = window.screen.innerHeight; 

target-densitydpi in the meta tag in Miko's post is deprecated in Chrome and should not be used.

Instead, try the following:

index.html:

<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0 minimal-ui" />

You should also consider using Phaser's methods for scaling, like this:

Boot.js:

var game = new Phaser.Game(1080, 1920, Phaser.CANVAS, ''); //or AUTO mode for WebGL/DOM

For performance, use the smallest dimensions that you can regardless of the device's resolution. Let Phaser create the canvas object.

Game.js:

this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT; //or SHOW_ALL or RESIZE
this.scale.pageAlignHorizontally = true;
this.scale.pageAlignVertically = true;

Your entire canvas will scale automatically by the ScaleManager, but this slows the app down when performance is needed -- especially with a large canvas. EXACT_FIT will stretch the canvas, while SHOW_ALL will resize it so that the whole canvas fits onto the screen without changing its aspect ratio (which can leave margins like the one you have).

查看更多
Explosion°爆炸
4楼-- · 2020-02-25 23:35

Try something like this:

var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
var pixelRatio = window.devicePixelRatio || 1; /// get pixel ratio of device

canvasMain = document.getElementById("canvasSignatureMain");

canvasMain.width = windowWidth * pixelRatio;   /// resolution of canvas
canvasMain.height = windowHeight * pixelRatio;

canvasMain.style.width = windowWidth + 'px';   /// CSS size of canvas
canvasMain.style.height = windowHeight + 'px';

(or an absolute value for the css rule).

On devices where the pixel ratio is higher than the typical 1:1 you get a higher resolution canvas. For normal circumstances everything is as normal.

查看更多
登录 后发表回答