How to clear the canvas for redrawing

后端 未结 23 2801
粉色の甜心
粉色の甜心 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: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;
    }
    

提交回复
热议问题