Html PhoneGap 应用程序上的屏幕分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20511028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:25:54  来源:igfitidea点击:

Screen Resolution On A PhoneGap App

htmlcanvascordovawebviewresolution

提问by Miko Diko

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.

我正在编写一个 Phonegap (3.2) 应用程序,我使用一个 HTML5 画布,使其全屏显示,整个应用程序 UI 都在这个画布上。

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.

使用三星 Galaxy S4 (Android 4.3) 测试该应用程序,该应用程序的屏幕分辨率仅为 360X640 而不是我想要的 1080X1920。

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

我添加了屏幕截图 1) // 看起来不错,但分辨率很差

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

windowWidth = window.innerWidth; windowHeight = window.innerHeight; 在此处输入图片说明

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

2) // 占屏幕的一小部分,其余部分为白色

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

windowWidth = window.outerWidth; windowHeight = window.outerHeight; 在此处输入图片说明

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.

3) // 只有一小部分图片是可见的,好像图像是在 1080X1920 处,但屏幕对它来说“太小”了。

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

windowWidth = screen.width; windowHeight = screen.height; 在此处输入图片说明

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.

谢谢。

回答by Miko Diko

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

一个同时解决分辨率和触摸事件问题的解决方案(Ken 的帖子随附):

<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 :))

感谢肯试图帮助兄弟 :))

回答by Miko Diko

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).

(或 css 规则的绝对值)。

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.

在像素比高于典型 1:1 的设备上,您将获得更高分辨率的画布。在正常情况下,一切都是正常的。

回答by Victor Stoddard

window.innerWidthand window.innerHeightdon't necessarily give you the real screen dimensions.

window.innerWidth并且window.innerHeight不一定给你真实的屏幕尺寸。

Try this instead:

试试这个:

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

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

target-densitydpi在 Miko 帖子中的元标记中,Chrome 已弃用,不应使用。

Instead, try the following:

相反,请尝试以下操作:

index.html:

索引.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:

您还应该考虑使用 Phaser 的缩放方法,如下所示:

Boot.js:

启动.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.

为了性能,无论设备的分辨率如何,都尽可能使用最小的尺寸。让 Phaser 创建画布对象。

Game.js:

游戏.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).

您的整个画布将通过 ScaleManager 自动缩放,但这会在需要性能时减慢应用程序的速度——尤其是使用大画布时。EXACT_FIT 将拉伸画布,而 SHOW_ALL 将调整它的大小,以便整个画布适合屏幕而不改变其纵横比(这可以像您拥有的那样留下边距)。