HTML canvas - drawing disappear on resizing

前端 未结 7 1313
自闭症患者
自闭症患者 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 17:46

    You need to redraw the scene when you resize.

    setting the width or height of a canvas, even if you are setting it to the same value as before, not only clears the canvas but resets the entire canvas context. Any set properties (fillStyle, lineWidth, the clipping region, etc) will also be reset.

    If you do not have the ability to redraw the scene from whatever data structures you might have representing the canvas, you can always save the entire canvas itself by drawing it to an in-memory canvas, setting the original width, and drawing the in-memory canvas back to the original canvas.

    Here's a really quick example of saving the canvas bitmap and putting it back after a resize:

    http://jsfiddle.net/simonsarris/weMbr/

    0 讨论(0)
  • 2020-12-03 17:48

    I also met this problem.but after a experiment, I found that Resizing the canvas element will automatically clear all drawings off the canvas! just try the code below

    <canvas id = 'canvas'></canvas>
    <script>
        var canvas1 = document.getElementById('canvas')
        console.log('canvas size',canvas1.width, canvas1.height)
        var ctx = canvas1.getContext('2d')
        ctx.font = 'Bold 48px Arial'
        var f = ctx.font
        canvas1.width = 480
        var f1 = ctx.font
        alert(f === f1) //false
    </script>
    
    0 讨论(0)
  • 2020-12-03 17:51

    I had the same problem when I had to resize the canvas to adjust it to the screen. But I solved it with this code:

    var c = document.getElementById('canvas');
    ctx = c.getContext('2d');
    ctx.fillRect(0,0,20,20);
    // Save canvas settings
    ctx.save();
    // Save canvas context
    var dataURL = c.toDataURL('image/jpeg');
    // Resize canvas
    c.width = 50;
    c.height = 50;
    // Restore canvas context
    var img = document.createElement('img');
    img.src = dataURL;
    img.onload=function(){
      ctx.drawImage(img,20,20);
    }
    // Restote canvas settings
    ctx.restore();
    <canvas id=canvas width=40 height=40></canvas>

    0 讨论(0)
  • 2020-12-03 17:57

    Everytime you resize the canvas it will reset itself to transparant black, as defined in the spec.

    You will either have to:

    • redraw when you resize the canvas, or,
    • don't resize the canvas
    0 讨论(0)
  • 2020-12-03 18:03

    I had the same problem. Try following code

    var wrapper = document.getElementById("signature-pad");
    var canvas = wrapper.querySelector("canvas");
    var ratio = Math.max(window.devicePixelRatio || 1, 1);
    canvas.width = canvas.offsetWidth * ratio;
    canvas.height = canvas.offsetHeight * ratio;
    

    It keeps the drawing as it is

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题