1

我试图提供一个橡皮擦,就好像它是一把刷子一样,以独特地擦除。我有一个黑色矩形和一张图片作为画布背景。如何擦除部分矩形?

html5画布中的“擦除”,不起作用

我的小提琴: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);
4

1 回答 1

1

链接中的解决方案确实有效。尝试:

  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();
                 }
于 2013-05-13T15:21:32.513 回答