Resize HTML5 canvas to fit window

前端 未结 16 1450
遥遥无期
遥遥无期 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:15

    A pure CSS approach adding to solution of @jerseyboy above.
    Works in Firefox (tested in v29), Chrome (tested in v34) and Internet Explorer (tested in v11).

    
    
        
        
    
    
        
        
    
    
    

    Link to the example: http://temporaer.net/open/so/140502_canvas-fit-to-window.html

    But take care, as @jerseyboy states in his comment:

    Rescaling canvas with CSS is troublesome. At least on Chrome and Safari, mouse/touch event positions will not correspond 1:1 with canvas pixel positions, and you'll have to transform the coordinate systems.

提交回复
热议问题