Resize HTML5 canvas element

前端 未结 3 1568
温柔的废话
温柔的废话 2021-01-07 01:48

How can I achieve, so that the HTML5 canvas element ist resizeable?

I would like to implement this element so that you can scale it in any size. The event of the sca

3条回答
  •  不思量自难忘°
    2021-01-07 01:49

    Although it's a bit late to answer this question, I'd like to share what I found to solve the same question. Take it a look please.

    panel.css

    #Panel {
    width:  100%;
    height: 30%;
    }
    

    app.js

    var widthG = 0, height=G = 0;
    function updateScale() {
        widthG  = parseInt($('#Panel').width());
        heightG = parseInt($('#anel').height());
    }
    ...
    
    function updateCanvas() {
        ctx = $('#canvas').get(0).getContext('2d');
        ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height);
        ctx.canvas.width = widthG;
        ctx.canvas.width = heightG;
    }
    

    I also tried to re-assign these properties by css syntax, but it doesn't work.

    $('#canvas').width(panelW);
    $('#canvas').height(panelH);      
    

    Hope this helps ppl suffered from the same question.

提交回复
热议问题