1

I tried to provide an eraser as if it was a brush, to erase distinctively. I have a black rect and a picture as canvas background. How can I erase parts of the rect?

"Erasing" in html5 canvas, did not work

my fiddle: http://jsfiddle.net/FgNQk/10/

var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    var width  = window.innerWidth;
    var height = window.innerHeight;
    canvas.height = height;
    canvas.width = width;

    ctx.fillRect(100,100,100,100);

    canvas.addEventListener('mousedown', function(e) {
        this.down = true;   
        this.X = e.pageX ;
        this.Y = e.pageY ;
        this.color = rgb();
    }, 0);
    canvas.addEventListener('mouseup', function() {
        this.down = false;          
    }, 0);
    canvas.addEventListener('mousemove', function(e) {

        if(this.down) {
             with(ctx) {
                beginPath();
                moveTo(this.X, this.Y);
                lineTo(e.pageX , e.pageY );
                strokeStyle = "rgba(255,255,255,0.0)";
                ctx.lineWidth=1;
                stroke();
             }
             this.X = e.pageX ;
             this.Y = e.pageY ;
        }
    }, 0);
Community
  • 1
  • 1
daisy
  • 615
  • 2
  • 8
  • 15

1 Answers1

1

The solution in the link actually works. Try:

  with(ctx) {
                    beginPath();
                    moveTo(this.X, this.Y);
                    lineTo(e.pageX , e.pageY );
                    globalCompositeOperation = "destination-out";
                    strokeStyle = "rgba(0,0,0,1)";
                    ctx.lineWidth=1;
                    stroke();
                 }
RST
  • 3,899
  • 2
  • 20
  • 33