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

前端 未结 2 561
再見小時候
再見小時候 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;
        }
    

提交回复
热议问题