HTML canvas - drawing disappear on resizing

前端 未结 7 1344
自闭症患者
自闭症患者 2020-12-03 17:08

I have created a basic shape in HTML canvas element which works fine.

The problem occurs when I resize the canvas, all the drawing in the canvas disappears. Is this

7条回答
  •  我在风中等你
    2020-12-03 18:05

    One thing that worked for me was to use requestAnimationFrame().

    let height = window.innerHeight;
    let width = window.innerWidth;
    
    function handleWindowResize() {
        height = window.innerHeight;
        width = window.innerWidth;
    }
    
    function render() {
        // Draw your fun shapes here
        // ...
    
        // Keep this on the bottom
        requestAnimationFrame(render);
    }
    
    // Canvas being defined at the top of the file.
    function init() {
        ctx = canvas.getContext("2d");
    
        render();
    }
    

提交回复
热议问题