javascript - Fullscreen rectangle in Canvas (Android/iOS web app)

前端 未结 2 531
再見小時候
再見小時候 2021-01-22 16:56

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, Ne

相关标签:
2条回答
  • 2021-01-22 17:42

    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;
        }
    
    0 讨论(0)
  • 2021-01-22 17:49

    You might try ensuring that your html and body elements are all the full width and height of the viewport. Include any ancestors between the canvas and body as well. See the following CSS code:

    html,
    body {
      width: 100%;
      height: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题