HTML5 responsive canvas: resizing the browser canvas draw disappear

后端 未结 7 1673
一向
一向 2020-12-03 02:29

I want to make a responsive canvas using size in percentage and once user will resize the window the canvas adjust relatively. I am able to scale the canvas by using below c

7条回答
  •  眼角桃花
    2020-12-03 02:45

    I also had the same issue that your are facing in mu application, and after reading about it i came to this conclusion that the behavior of the canvas element is such that it clears itself after re-sizing, the only fix for this is to add a listener for the window re-size event and redraw the canvas when the event if fired.

    $(window).resize(function(e) {
    // function to redraw on the canvas
    });
    
    
    
    $(document).ready(function(e){
        var c = $('#example'); // getting the canvas element
        var ct = c.get(0).getContext('2d');
        var container = $(c).parent();
        $(window).resize( respondCanvas ); //handling the canvas resize
        function respondCanvas(){ 
            // makign the canvas fill its container
            c.attr('width', $(container).width() ); //max width
            c.attr('height', ($(container).height() -20 )); //max height
        }
        respondCanvas();
        make_image('images/india.png',1,1);
    }
    

    Hope it helps.

    Source

提交回复
热议问题