How to clear the canvas for redrawing

后端 未结 23 2813
粉色の甜心
粉色の甜心 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:00

    Use clearRect method by passing x,y co-ordinates and height and width of canvas. ClearRect will clear whole canvas as :

    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    0 讨论(0)
  • 2020-11-21 12:01

    This is 2018 and still there is no native method to completely clear canvas for redrawing. clearRect() does not clear the canvas completely. Non-fill type drawings are not cleared out (eg. rect())

    1.To completely clear canvas irrespective of how you draw:

    context.clearRect(0, 0, context.canvas.width, context.canvas.height);
    context.beginPath();
    

    Pros: Preserves strokeStyle, fillStyle etc.; No lag;

    Cons: Unnecessary if you are already using beginPath before drawing anything

    2.Using the width/height hack:

    context.canvas.width = context.canvas.width;
    

    OR

    context.canvas.height = context.canvas.height;
    

    Pros: Works with IE Cons: Resets strokeStyle, fillStyle to black; Laggy;

    I was wondering why a native solution does not exist. Actually, clearRect() is considered as the single line solution because most users do beginPath() before drawing any new path. Though beginPath is only to be used while drawing lines and not closed path like rect().

    This is the reason why the accepted answer did not solve my problem and I ended up wasting hours trying different hacks. Curse you mozilla

    0 讨论(0)
  • 2020-11-21 12:03
    context.clearRect(0,0,w,h)   
    

    fill the given rectangle with RGBA values :
    0 0 0 0 : with Chrome
    0 0 0 255 : with FF & Safari

    But

    context.clearRect(0,0,w,h);    
    context.fillStyle = 'rgba(0,0,0,1)';  
    context.fillRect(0,0,w,h);  
    

    let the rectangle filled with
    0 0 0 255
    no matter the browser !

    0 讨论(0)
  • 2020-11-21 12:06
    • Chrome responds well to: context.clearRect ( x , y , w , h ); as suggested by @Pentium10 but IE9 seems to completely ignore this instruction.
    • IE9 seems to respond to: canvas.width = canvas.width; but it doesn't clear lines, just shapes, pictures and other objects unless you also use @John Allsopp's solution of first changing the width.

    So if you have a canvas and context created like this:

    var canvas = document.getElementById('my-canvas');
    var context = canvas.getContext('2d');
    

    You can use a method like this:

    function clearCanvas(context, canvas) {
      context.clearRect(0, 0, canvas.width, canvas.height);
      var w = canvas.width;
      canvas.width = 1;
      canvas.width = w;
    }
    
    0 讨论(0)
  • 2020-11-21 12:06

    the shortest way:

    canvas.width += 0
    
    0 讨论(0)
  • 2020-11-21 12:07

    Use: context.clearRect(0, 0, canvas.width, canvas.height);

    This is the fastest and most descriptive way to clear the entire canvas.

    Do not use: canvas.width = canvas.width;

    Resetting canvas.width resets all canvas state (e.g. transformations, lineWidth, strokeStyle, etc.), it is very slow (compared to clearRect), it doesn't work in all browsers, and it doesn't describe what you are actually trying to do.

    Dealing with transformed coordinates

    If you have modified the transformation matrix (e.g. using scale, rotate, or translate) then context.clearRect(0,0,canvas.width,canvas.height) will likely not clear the entire visible portion of the canvas.

    The solution? Reset the transformation matrix prior to clearing the canvas:

    // Store the current transformation matrix
    context.save();
    
    // Use the identity matrix while clearing the canvas
    context.setTransform(1, 0, 0, 1, 0, 0);
    context.clearRect(0, 0, canvas.width, canvas.height);
    
    // Restore the transform
    context.restore();
    

    Edit: I've just done some profiling and (in Chrome) it is about 10% faster to clear a 300x150 (default size) canvas without resetting the transform. As the size of your canvas increases this difference drops.

    That is already relatively insignificant, but in most cases you will be drawing considerably more than you are clearing and I believe this performance difference be irrelevant.

    100000 iterations averaged 10 times:
    1885ms to clear
    2112ms to reset and clear
    
    0 讨论(0)
提交回复
热议问题