Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to erase an area in a BitmapData object?

Flex 3, ActionScript 3, Flash player 9.

I have a picture in a BitmapData object. And an array of points. I nead to erase the part of the picture inside a polygon specified by the points. In other words, draw a polygon specified by the points and fill it with transparency.

Any ideas on how it can be done?

like image 499
artemb Avatar asked Oct 11 '09 09:10

artemb


2 Answers

Got it working with the following code:

        var shape:Shape = new Shape();
        shape.graphics.beginFill(0x000000, 1); // solid black
        shape.graphics.moveTo(points[0].x, points[0].y);

        points.forEach(function (p:Point, i:int, a:Array):void {
                shape.graphics.lineTo(p.x, p.y);
            });
        shape.graphics.endFill();
        data.draw(shape, null, null, "erase");
like image 121
artemb Avatar answered Sep 26 '22 23:09

artemb


For a rectangle, you can use fillRect. For a polygon you are gonna have to draw the polygon in a totally different color (than other colors in the bitmap) and use floodFill - but I don't know how to draw a polygon. There is no method in bitmap data class to draw lines. Another option would be to write your own logic to find pixels inside the polygon and use setPixel32 method to set their alphas to zero.

This wikipedia page describes algorithms to find if a point is inside a given polygon. You might find it useful.

like image 27
Amarghosh Avatar answered Sep 23 '22 23:09

Amarghosh