How to clear the canvas for redrawing

后端 未结 23 2858
粉色の甜心
粉色の甜心 2020-11-21 11:37

After experimenting with composite operations and drawing images on the canvas I\'m now trying to remove images and compositing. How do I do this?

I need to clear th

相关标签:
23条回答
  • 2020-11-21 12:17

    in webkit you need to set the width to a different value, then you can set it back to the initial value

    0 讨论(0)
  • 2020-11-21 12:18

    If you use clearRect only, if you have it in a form to submit your drawing, you'll get a submit instead the clearing, or maybe it can be cleared first and then upload a void drawing, so you'll need to add a preventDefault at the beggining of the function:

       function clearCanvas(canvas,ctx) {
            event.preventDefault();
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }
    
    
    <input type="button" value="Clear Sketchpad" id="clearbutton" onclick="clearCanvas(canvas,ctx);">
    

    Hope it helps someone.

    0 讨论(0)
  • 2020-11-21 12:19

    Others have already done an excellent job answering the question but if a simple clear() method on the context object would be useful to you (it was to me), this is the implementation I use based on answers here:

    CanvasRenderingContext2D.prototype.clear = 
      CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
        if (preserveTransform) {
          this.save();
          this.setTransform(1, 0, 0, 1, 0, 0);
        }
    
        this.clearRect(0, 0, this.canvas.width, this.canvas.height);
    
        if (preserveTransform) {
          this.restore();
        }           
    };
    

    Usage:

    window.onload = function () {
      var canvas = document.getElementById('canvasId');
      var context = canvas.getContext('2d');
    
      // do some drawing
      context.clear();
    
      // do some more drawing
      context.setTransform(-1, 0, 0, 1, 200, 200);
      // do some drawing with the new transform
      context.clear(true);
      // draw more, still using the preserved transform
    };
    
    0 讨论(0)
  • 2020-11-21 12:19

    I always use

    cxt.fillStyle = "rgb(255, 255, 255)";
    cxt.fillRect(0, 0, canvas.width, canvas.height);
    

    For a custom color, and

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    

    For making the canvas transparent when clearing

    0 讨论(0)
  • 2020-11-21 12:22

    there are a ton of good answers here. one further note is that sometimes it's fun to only partially clear the canvas. that is, "fade out" the previous image instead of erasing it entirely. this can give nice trails effects.

    it's easy. supposing your background color is white:

    // assuming background color = white and "eraseAlpha" is a value from 0 to 1.
    myContext.fillStyle = "rgba(255, 255, 255, " + eraseAlpha + ")";
    myContext.fillRect(0, 0, w, h);
    
    0 讨论(0)
  • 2020-11-21 12:23

    If you are drawing lines, make sure you don't forget:

    context.beginPath();
    

    Otherwise the lines won't get cleared.

    0 讨论(0)
提交回复
热议问题