Resize HTML5 canvas to fit window

前端 未结 16 1186
遥遥无期
遥遥无期 2020-11-22 12:03

How can I automatically scale the HTML5 element to fit the page?

For example, I can get a

to scale by setting th
16条回答
  •  太阳男子
    2020-11-22 12:12

    This worked for me. Pseudocode:

    // screen width and height
    scr = {w:document.documentElement.clientWidth,h:document.documentElement.clientHeight}
    canvas.width = scr.w
    canvas.height = scr.h
    

    Also, like devyn said, you can replace "document.documentElement.client" with "inner" for both the width and height:

    **document.documentElement.client**Width
    **inner**Width
    **document.documentElement.client**Height
    **inner**Height
    

    and it still works.

提交回复
热议问题