<tfoot id='5gmnS'></tfoot>

  1. <small id='5gmnS'></small><noframes id='5gmnS'>

        <bdo id='5gmnS'></bdo><ul id='5gmnS'></ul>
    1. <i id='5gmnS'><tr id='5gmnS'><dt id='5gmnS'><q id='5gmnS'><span id='5gmnS'><b id='5gmnS'><form id='5gmnS'><ins id='5gmnS'></ins><ul id='5gmnS'></ul><sub id='5gmnS'></sub></form><legend id='5gmnS'></legend><bdo id='5gmnS'><pre id='5gmnS'><center id='5gmnS'></center></pre></bdo></b><th id='5gmnS'></th></span></q></dt></tr></i><div id='5gmnS'><tfoot id='5gmnS'></tfoot><dl id='5gmnS'><fieldset id='5gmnS'></fieldset></dl></div>
      <legend id='5gmnS'><style id='5gmnS'><dir id='5gmnS'><q id='5gmnS'></q></dir></style></legend>

      javascript - Canvas 中的全屏矩形(Android/iOS 网络应用程序)

      javascript - Fullscreen rectangle in Canvas (Android/iOS web app)(javascript - Canvas 中的全屏矩形(Android/iOS 网络应用程序))

      <small id='JSXkL'></small><noframes id='JSXkL'>

      <i id='JSXkL'><tr id='JSXkL'><dt id='JSXkL'><q id='JSXkL'><span id='JSXkL'><b id='JSXkL'><form id='JSXkL'><ins id='JSXkL'></ins><ul id='JSXkL'></ul><sub id='JSXkL'></sub></form><legend id='JSXkL'></legend><bdo id='JSXkL'><pre id='JSXkL'><center id='JSXkL'></center></pre></bdo></b><th id='JSXkL'></th></span></q></dt></tr></i><div id='JSXkL'><tfoot id='JSXkL'></tfoot><dl id='JSXkL'><fieldset id='JSXkL'></fieldset></dl></div>
    2. <tfoot id='JSXkL'></tfoot>
    3. <legend id='JSXkL'><style id='JSXkL'><dir id='JSXkL'><q id='JSXkL'></q></dir></style></legend>
            <tbody id='JSXkL'></tbody>

                <bdo id='JSXkL'></bdo><ul id='JSXkL'></ul>

                本文介绍了javascript - Canvas 中的全屏矩形(Android/iOS 网络应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在尝试使用 Canvas 在整个浏览器视口上渲染矩形时遇到问题.下面的代码在桌面上运行良好,但在移动设备上(在 iPad mini、Nexus 7 和 Galaxy S4 上测试),矩形只填充了屏幕的一部分(在 iPad 上大约是一半,在其他设备上是三分之二).

                I have a problem trying to render a rectangle over the entire browser viewport using Canvas. The code below works fine on desktop, but on mobile devices (tested on iPad mini, Nexus 7 and Galaxy S4), the rectangle only fills a part of the screen (roughly a half on iPad, two thirds on other devices).

                ctx.beginPath();
                ctx.rect(0, 0, window.innerWidth, window.innerHeight);
                ctx.closePath();
                
                ctx.lineWidth = 5;
                ctx.strokeStyle = 'rgba(0,0,0,0.5)';
                ctx.stroke();
                ctx.fillStyle = 'rgba(0,0,0,0.5)';
                ctx.fill();
                

                我试过 ctx.scale(1, 1);但这没有用.window.innerWidth 和 window.innerHeight 的值是正确的.即使我使用硬编码值作为矩形的宽度/高度,我也会得到相同的结果.

                I tried ctx.scale(1, 1); but that did not work. The values of window.innerWidth and window.innerHeight are correct. And I get the same result even if I use a hardcoded value as the rectangle's width/height.

                解决方案:

                基于 Ken Fyrstenberg

                var ratio = window.devicePixelRatio || 1;
                var width = window.innerWidth * ratio;
                var height = window.innerHeight * ratio;
                
                ctx.beginPath();
                ctx.rect(0, 0, width, height);
                ctx.closePath();
                
                ctx.lineWidth = 5;
                ctx.strokeStyle = 'rgba(0,0,0,0.5)';
                ctx.stroke();
                ctx.fillStyle = 'rgba(0,0,0,0.5)';
                ctx.fill();
                

                推荐答案

                在计算画布大小时,可以使用像素纵横比作为一个因素.

                You can use pixel aspect ratio as a factor when calculating the canvas size.

                var ratio = window.devicePixelRatio || 1;
                var width = window.innerWidth * ratio;
                var height = window.innerHeight * ratio;
                
                canvas.width = width;
                canvas.height = height;
                

                CSS:

                #canvasID {
                    position: fixed;
                    left: 0;
                    top: 0;
                    }
                

                这篇关于javascript - Canvas 中的全屏矩形(Android/iOS 网络应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)

                  • <legend id='Sues1'><style id='Sues1'><dir id='Sues1'><q id='Sues1'></q></dir></style></legend>
                      <bdo id='Sues1'></bdo><ul id='Sues1'></ul>
                    • <tfoot id='Sues1'></tfoot>

                    • <small id='Sues1'></small><noframes id='Sues1'>

                      <i id='Sues1'><tr id='Sues1'><dt id='Sues1'><q id='Sues1'><span id='Sues1'><b id='Sues1'><form id='Sues1'><ins id='Sues1'></ins><ul id='Sues1'></ul><sub id='Sues1'></sub></form><legend id='Sues1'></legend><bdo id='Sues1'><pre id='Sues1'><center id='Sues1'></center></pre></bdo></b><th id='Sues1'></th></span></q></dt></tr></i><div id='Sues1'><tfoot id='Sues1'></tfoot><dl id='Sues1'><fieldset id='Sues1'></fieldset></dl></div>

                            <tbody id='Sues1'></tbody>