How to resize html canvas element?

后端 未结 7 1843
慢半拍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: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:

    
    
    
    
    

    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.

提交回复
热议问题