How to resize html canvas element?

后端 未结 7 1808
慢半拍i
慢半拍i 2020-11-29 01:34

I have a canvas element defined statically in the html with a width and height. If I attempt to use JavaScript to resize it dynamically (setting a new width and height - eit

相关标签:
7条回答
  • 2020-11-29 02:19
    <div id="canvasdiv" style="margin: 5px; height: 100%; width: 100%;">
        <canvas id="mycanvas" style="border: 1px solid red;"></canvas>
    </div>
    <script>
    $(function(){
        InitContext();
    });
    function InitContext()
    {
    var $canvasDiv = $('#canvasdiv');
    
    var canvas = document.getElementById("mycanvas");
    canvas.height = $canvasDiv.innerHeight();
    canvas.width = $canvasDiv.innerWidth();
    }
    </script>
    
    0 讨论(0)
  • 2020-11-29 02:21

    You didn't publish your code, and I suspect you do something wrong. it is possible to change the size by assigning width and height attributes using numbers:

    canvasNode.width  = 200; // in pixels
    canvasNode.height = 100; // in pixels
    

    At least it works for me. Make sure you don't assign strings (e.g., "2cm", "3in", or "2.5px"), and don't mess with styles.

    Actually this is a publicly available knowledge — you can read all about it in the HTML canvas spec — it is very small and unusually informative. This is the whole DOM interface:

    interface HTMLCanvasElement : HTMLElement {
               attribute unsigned long width;
               attribute unsigned long height;
    
      DOMString toDataURL();
      DOMString toDataURL(in DOMString type, [Variadic] in any args);
    
      DOMObject getContext(in DOMString contextId);
    };
    

    As you can see it defines 2 attributes width and height, and both of them are unsigned long.

    0 讨论(0)
  • 2020-11-29 02:24

    Prototypes can be a hassle to work with, and from the _PROTO part of the error it appears your error is caused by, say, HTMLCanvasElement.prototype.width, possibly as an attempt to resize all the canvases at once.

    As a suggestion, if you are trying to resize a number of canvases at once, you could try:

    <canvas></canvas>
    <canvas></canvas>
    <canvas></canvas>
    <script type="text/javascript">
        ...
    </script>
    

    In the JavaScript, instead of invoking a prototype, try this:

    $$ = function(){
        return document.querySelectorAll.apply(document,arguments);
    }
    for(var i in $$('canvas')){
        canvas = $$('canvas')[i];
        canvas.width = canvas.width+100;
        canvas.height = canvas.height+100;
    }
    

    This would resize all the canvases by adding 100 px to their size, as is demonstrated in this example


    Hope this helped.

    0 讨论(0)
  • 2020-11-29 02:31

    Note that if your canvas is statically declared you should use the width and height attributes, not the style, eg. this will work:

    <canvas id="c" height="100" width="100" style="border:1px"></canvas>
    <script>
        document.getElementById('c').width = 200;
    </script>
    

    But this will not work:

    <canvas id="c" style="width: 100px; height: 100px; border:1px"></canvas>
    <script>
        document.getElementById('c').width = 200;
    </script>
    
    0 讨论(0)
  • 2020-11-29 02:35

    I just had the same problem as you, but found out about the toDataURL method, which proved useful.

    The gist of it is to turn your current canvas into a dataURL, change your canvas size, and then draw what you had back into your canvas from the dataURL you saved.

    So here's my code:

    var oldCanvas = canvas.toDataURL("image/png");
    var img = new Image();
    img.src = oldCanvas;
    img.onload = function (){
        canvas.height += 100;
        ctx.drawImage(img, 0, 0);
    }
    
    0 讨论(0)
  • 2020-11-29 02:35

    This worked for me just now:

    <canvas id="c" height="100" width="100" style="border:1px solid red"></canvas>
    <script>
    var c = document.getElementById('c');
    alert(c.height + ' ' + c.width);
    c.height = 200;
    c.width = 200;
    alert(c.height + ' ' + c.width);
    </script>
    
    0 讨论(0)
提交回复
热议问题