Drawing App, How to Erase

280 views Asked by At

(I'm a newbie in AS3) I found a usefull code here http://www.flashandmath.com/advanced/smoothdraw/ and I'm trying to create an Erase button, I'm trying to use .cacheAsBitmap = true; but this work only if there is a mask(I saw this on some AS2 app where you can erase a bitmap then you can redraw). P.S. I changed the code slightly to change the background now the drawBackgroundfunction look like this:

    function drawBackground():void {
    //We draw a background with a very subtle gradient effect so that the canvas darkens towards the edges.
    var mesh:Shape = new Shape();
    var gradMat:Matrix = new Matrix();
    gradMat.createGradientBox(700,500,0,0,0);
    var bg:Sprite = new Sprite();
    mesh.graphics.beginBitmapFill(new lignes(), null, true, false);
    //bg.graphics.beginGradientFill("radial",[0xDDD0AA,0xC6B689],[1,1],[1,255],gradMat);
    //bg.graphics.drawRect(0,0,700,500);
    //bg.graphics.endFill();
    mesh.graphics.drawRect(0, 0, 700, 500);
    mesh.graphics.endFill();
    boardBitmapData.draw(mesh);
    mesh.cacheAsBitmap = true;
    //We clear out the undo buffer with a copy of just a blank background:
    undoStack = new Vector.<BitmapData>;
    var undoBuffer:BitmapData = new BitmapData(boardWidth, boardHeight, false);
    undoBuffer.copyPixels(boardBitmapData,undoBuffer.rect,new Point(0,0));
    undoStack.push(undoBuffer);
}

I just want to create an Erase methode, thanks for your help

Edit: I meant to erase only the clicked area

2

There are 2 answers

1
gabriel On

To clean the Shape Try:

mesh.graphics.clear();

To clean the Shape object. I don't know what else you are doing with the boardBitmapData variable, maybe will be necessary to do something about as well.

  • clear() - Clears the graphics that were drawn to this Graphics object, and resets fill and line style settings.
0
esdebon On

You need convert your canvas in bitmap, then use a mask to delete or blend mode ERASE, and to draw again create a new bitmap from the masked bitmap and finally use this as background.